Ejemplo n.º 1
0
        public virtual ActionResult ApplyPermissions(PermissionOperationsModel model)
        {
            var role = roleService.Find(model.RoleId);
            if (role == null || role.NotPermissible)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, Translate("Messages.CouldNotFoundEntity"));
            }

            RoleHelper.ApplyRolePermissions(model);
            Success(Translate("Messages.SuccessfullyApplyPermissions"));
            return RedirectToAction(MVC.Admin.Role.Permissions(model.RoleId, String.Format("{0}_{1}", model.ResourceId, (int)model.Area)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Applies the role permissions.
        /// </summary>
        /// <param name="model">The model.</param>
        public static void ApplyRolePermissions(PermissionOperationsModel model)
        {
            var permissionService = ServiceLocator.Current.GetInstance<IPermissionService>();
            var permissions = permissionService.GetPermission(model.RoleId, model.ResourceId, null);

            if (permissions == null)
            {
                permissions = new Permission
                {
                    Role =
                    {
                        Id = model.RoleId
                    },
                    EntityType =
                    {
                        Id = model.ResourceId
                    },
                };
                if (model.OperationIds != null)
                {
                    foreach (var operation in model.OperationIds)
                    {
                        permissions.Permissions = permissions.Permissions | operation;
                    }
                }
            }
            else
            {
                var objectTypeService = ServiceLocator.Current.GetInstance<IEntityTypeService>();
                var resourceType = objectTypeService.Find(model.ResourceId);
                if (resourceType != null)
                {
                    var operations = GetResourceOperations(resourceType, model.Area);

                    foreach (var operation in operations)
                    {
                        if (model.OperationIds == null || !model.OperationIds.Contains(operation.Key))
                        {
                            permissions.Permissions = permissions.Permissions & (~operation.Key);
                        }
                        else
                        {
                            permissions.Permissions = permissions.Permissions | operation.Key;
                        }
                    }
                }
            }

            permissionService.Save(permissions);
        }