コード例 #1
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var ability = AbilityConfiguration.CreateAbility();

            if (ability == null)
            {
                throw new Exception("CanIMvcConfiguration has not been configured.");
            }

            var action  = filterContext.ActionDescriptor.ActionName;
            var subject = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

            var attributes =
                filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeForAttribute), false)
                .OfType <AuthorizeForAttribute>();

            if (ability.Allows(action, subject) || attributes.Any(a => ability.Allows(a.RequestedActionOnSubject)))
            {
                return;
            }

            filterContext.Result = resultOnFailedAuthorization(filterContext);
        }
コード例 #2
0
        public static bool Can(string action, object subject)
        {
            var ability = AbilityConfiguration.CreateAbility();

            return(ability.Allows(action, subject));
        }
コード例 #3
0
        public static bool CanExecute(object command)
        {
            var ability = AbilityConfiguration.CreateAbility();

            return(ability.AllowsExecutionOf(command));
        }