コード例 #1
0
        /// <summary>
        /// Resolve ServiceStack Validator in external ServiceStack provider class like ServiceStackController
        /// </summary>
        public static IValidator <T> ResolveValidator <T>(this IHasServiceStackProvider provider)
        {
            var validator = provider.ServiceStackProvider.TryResolve <IValidator <T> >();

            if (validator is IRequiresRequest requiresReq)
            {
                requiresReq.Request = provider.ServiceStackProvider.Request;
            }
            return(validator);
        }
コード例 #2
0
        public static bool IsAuthorized(this IHasServiceStackProvider hasProvider, AuthenticateAttribute authAttr)
        {
            if (authAttr == null)
            {
                return(true);
            }

            var authSession = hasProvider.ServiceStackProvider.GetSession();

            return(authSession != null && authSession.IsAuthenticated);
        }
コード例 #3
0
        public static bool HasAccess(
            this IHasServiceStackProvider hasProvider,
            ICollection <RequiredRoleAttribute> roleAttrs,
            ICollection <RequiresAnyRoleAttribute> anyRoleAttrs,
            ICollection <RequiredPermissionAttribute> permAttrs,
            ICollection <RequiresAnyPermissionAttribute> anyPermAttrs)
        {
            if (roleAttrs.Count + anyRoleAttrs.Count + permAttrs.Count + anyPermAttrs.Count == 0)
            {
                return(true);
            }

            var authSession = hasProvider.ServiceStackProvider.GetSession();

            if (authSession == null || !authSession.IsAuthenticated)
            {
                return(false);
            }

            var httpReq      = hasProvider.ServiceStackProvider.Request;
            var userAuthRepo = HostContext.AppHost.GetAuthRepository(hasProvider.ServiceStackProvider.Request);

            using (userAuthRepo as IDisposable)
            {
                var hasRoles = roleAttrs.All(x => x.HasAllRoles(httpReq, authSession, userAuthRepo));
                if (!hasRoles)
                {
                    return(false);
                }

                var hasAnyRole = anyRoleAttrs.All(x => x.HasAnyRoles(httpReq, authSession, userAuthRepo));
                if (!hasAnyRole)
                {
                    return(false);
                }

                var hasPermissions = permAttrs.All(x => x.HasAllPermissions(httpReq, authSession, userAuthRepo));
                if (!hasPermissions)
                {
                    return(false);
                }

                var hasAnyPermission = anyPermAttrs.All(x => x.HasAnyPermissions(httpReq, authSession, userAuthRepo));
                if (!hasAnyPermission)
                {
                    return(false);
                }

                return(true);
            }
        }