public PolicyResult Enforce(ISecurityContext context)
        {
            if (context.CurrentUserIsAuthenticated() == false)
                return PolicyResult.CreateFailureResult(this, "Anonymous access denied");

            var currentUserRoles = context.CurrentUserRoles().EnsureIsList();
            if (currentUserRoles.Any() == false)
                return PolicyResult.CreateFailureResult(this, "Access denied");

            if (currentUserRoles.Any(role => _requiredRoles.Contains(role)) == false)
            {
                const string message = "Access requires one of the following roles: {0}.";
                var formattedMessage = string.Format(message, GetRoles());
                return PolicyResult.CreateFailureResult(this, formattedMessage);
            }

            return PolicyResult.CreateSuccessResult(this);
        }
Beispiel #2
0
        public PolicyResult Enforce(ISecurityContext context)
        {
            if (context.CurrentUserIsAuthenticated() == false)
            {
                return(PolicyResult.CreateFailureResult(this, "Anonymous access denied"));
            }

            var currentUserRoles = context.CurrentUserRoles().EnsureIsList();

            if (currentUserRoles.Any() == false)
            {
                return(PolicyResult.CreateFailureResult(this, "Access denied"));
            }

            if (_requiredRoles.Any(requiredRole => currentUserRoles.Contains(requiredRole) == false))
            {
                const string message          = "Access requires all of the following roles: {0}.";
                var          formattedMessage = string.Format(message, GetRoles());
                return(PolicyResult.CreateFailureResult(this, formattedMessage));
            }

            return(PolicyResult.CreateSuccessResult(this));
        }
 public PolicyResult Enforce(ISecurityContext context)
 {
     // NOTE: OK to leave like this as tests depends on it.
     var authenticated = context.CurrentUserIsAuthenticated();
     var roles = context.CurrentUserRoles();
     return PolicyResult.CreateSuccessResult(this);
 }
 public IEnumerable <object> CurrentUserRoles()
 {
     return(_securityContext.CurrentUserRoles());
 }