Exemple #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="PermissionModel"/> for Controller
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="controllerType">Type of the controller.</param>
        public PermissionModel(IPermissionAttribute attribute, Type controllerType)
        {
            this.Permission = attribute.Permission;
            this.Area       = attribute.Area.IsNullOrWhiteSpace() ?
                              ActiveRoleEngineHelper.GetControllerArea(controllerType) :
                              attribute.Area;
            this.Controller = attribute.Controller.IsNullOrWhiteSpace() ?
                              ActiveRoleEngineHelper.GetControllerName(controllerType) :
                              attribute.Controller;
            this.Action = attribute.Action;

            this.Group          = attribute.Group;
            this.Description    = attribute.Description;
            this.PermissionType = attribute.PermissionType;
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PermissionModel" /> class for Action
        /// </summary>
        /// <param name="attribute">The attribute</param>
        /// <param name="controllerAttribute">The controller attribute if any</param>
        /// <param name="controllerType">Type of the controller</param>
        /// <param name="methodInfo">The method information</param>
        public PermissionModel(IPermissionAttribute attribute,
                               IPermissionAttribute controllerAttribute,
                               Type controllerType,
                               MemberInfo methodInfo)
        {
            // if the Action is decorated by ActivePermissionAttribute
            // it will be considered a new permission (unless it map to an existing permission)


            // get permission decoration from ActivePermissionAttribute
            // if ActivePermissionAttribute does not provide the information
            // then get from MethodInfo or controllerAttribute


            // not inherit from controller, the action must define its own
            this.Permission = attribute.Permission;

            // inherit from controller if not defined
            // Area can be "" (root area) => here we check null only
            this.Area = attribute.Area ?? ActiveRoleEngineHelper.GetControllerArea(controllerType);

            // inherit from controller if not defined
            // Controller must be specified

            if (attribute.Controller.IsNotNullOrEmpty())
            {
                this.Controller = attribute.Controller;
            }
            else
            {
                this.Controller = controllerAttribute == null || controllerAttribute.Controller.IsNullOrWhiteSpace() ?
                                  ActiveRoleEngineHelper.GetControllerName(controllerType) :
                                  controllerAttribute.Controller;
            }

            if (attribute.Action.IsNotNullOrEmpty())
            {
                this.Action = attribute.Action;
            }
            else
            {
                string actionName = null;

                if (methodInfo.IsDefined(typeof(ActionNameAttribute)))
                {
                    actionName = methodInfo.GetCustomAttribute <ActionNameAttribute>()?.Name;
                }

                if (actionName.IsNullOrEmpty())
                {
                    actionName = methodInfo.Name;
                }

                this.Action = actionName;
            }

            // the Action does not define the group => inherit from controller
            this.Group = attribute.Group.IsNullOrWhiteSpace() ? controllerAttribute?.Group : attribute.Group;

            this.Description = attribute.Description;

            this.PermissionType = attribute.PermissionType;

            //// if AllowSuperAdminOnlyEnum.Inherit then get from controller
            //this.PermissionType = attribute.PermissionType == PermissionType.Inherit ?
            //    (controllerAttribute?.PermissionType ?? PermissionType.Inherit) :
            //    attribute.PermissionType;
        }