public void UpdatePermission(AjaxEditPermissionViewModel ajaxEditPermissionViewModel)
        {
            using (var unitOfWork = _unitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    var mappedItem = new CategoryPermission
                    {
                        CategoryId    = ajaxEditPermissionViewModel.Category,
                        MemberGroupId = ajaxEditPermissionViewModel.MemberGroup,
                        Permission    = ServiceFactory.PermissionService.Get(ajaxEditPermissionViewModel.Permission),
                        IsTicked      = ajaxEditPermissionViewModel.HasPermission
                    };
                    ServiceFactory.CategoryPermissionService.UpdateOrCreateNew(mappedItem);

                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    AppHelpers.LogError("Error updating permissions on category", ex);
                    throw;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Get permissions for roles other than those specially treated in this class
        /// </summary>
        /// <param name="category"></param>
        /// <param name="memberGroup"></param>
        /// <returns></returns>
        private PermissionSet GetOtherPermissions(Category category, List <IMemberGroup> memberGroups)
        {
            // Get all permissions
            var permissionList = GetAll();

            // Get the known permissions for this role and category
            var permissions = new List <CategoryPermission>();

            foreach (var memberGroup in memberGroups)
            {
                var categoryRow = ServiceFactory.CategoryPermissionService.GetCategoryRow(memberGroup.Id, category.Id);
                //var categoryRowPermissions = categoryRow.ToDictionary(catRow => catRow.Permission);

                // Load up the results with the permisions for this role / cartegory. A null entry for a permissions results in a new
                // record with a false value

                foreach (var permission in permissionList)
                {
                    if (categoryRow.ContainsKey(permission))
                    {
                        var existingPermission = permissions.Where(x => x.Permission.Name == permission.Name).FirstOrDefault();

                        if (existingPermission == null)
                        {
                            permissions.Add(categoryRow[permission]);
                        }
                        else
                        {
                            existingPermission.IsTicked = true;
                        }
                    }
                    else
                    {
                        var newPermision = new CategoryPermission {
                            Category = category, MemberGroup = memberGroup, IsTicked = false, Permission = permission
                        };

                        var existingPermission = permissions.Where(x => x.Permission.Name == permission.Name).FirstOrDefault();

                        if (existingPermission == null)
                        {
                            permissions.Add(newPermision);
                        }
                    }

                    //permissions.Add(categoryRow.ContainsKey(permission)
                    //					? categoryRow[permission]
                    //					: new CategoryPermission { Category = category, MemberGroup = memberGroup, IsTicked = false, Permission = permission });
                }
            }


            var permissionSet = new PermissionSet(permissions);

            return(permissionSet);
        }
        /// <summary>
        /// Check the category permission for role actually exists
        /// </summary>
        /// <param name="categoryPermission"></param>
        /// <returns></returns>
        public CategoryPermission CheckExists(CategoryPermission categoryPermission)
        {
            if (categoryPermission.Permission != null)
            {
                return(GetByPermissionRoleCategoryId(categoryPermission.Permission.Id,
                                                     categoryPermission.MemberGroupId,
                                                     categoryPermission.CategoryId));
            }

            return(null);
        }
        /// <summary>
        /// Either updates a CPFR if exists or creates a new one
        /// </summary>
        /// <param name="categoryPermission"></param>
        public void UpdateOrCreateNew(CategoryPermission categoryPermission)
        {
            // Firstly see if this exists already
            var permission = CheckExists(categoryPermission);

            // if it exists then just update it
            if (permission != null)
            {
                permission.IsTicked = categoryPermission.IsTicked;
            }
            else
            {
                Add(categoryPermission);
            }
        }
 public void Delete(CategoryPermission categoryPermission)
 {
     ContextPerRequest.Db.CategoryPermission.Remove(categoryPermission);
 }
 public CategoryPermission Add(CategoryPermission categoryPermission)
 {
     return(ContextPerRequest.Db.CategoryPermission.Add(categoryPermission));
 }
示例#7
0
        public async Task <bool> HasPermission(ServerPlanetMember member, Permission permission, ValourDB db = null)
        {
            Planet planet = await GetPlanetAsync(db);

            if (planet.Owner_Id == member.User_Id)
            {
                return(true);
            }

            var roles = await member.GetRolesAsync(db);

            CategoryPermission    catPerm  = null;
            ChatChannelPermission chatPerm = null;

            catPerm  = permission as CategoryPermission;
            chatPerm = permission as ChatChannelPermission;

            // Starting from the most important role, we stop once we hit the first clear "TRUE/FALSE".
            // If we get an undecided, we continue to the next role down
            foreach (var role in roles)
            {
                var node = await ServerPlanetRole.FromBase(role).GetCategoryNodeAsync(this);

                // If we are dealing with the default role and the behavior is undefined, we fall back to the default permissions
                if (node == null)
                {
                    if (role.Id == planet.Default_Role_Id)
                    {
                        if (catPerm != null)
                        {
                            return(Permission.HasPermission(CategoryPermissions.Default, permission));
                        }
                        else if (chatPerm != null)
                        {
                            return(Permission.HasPermission(ChatChannelPermissions.Default, permission));
                        }
                    }

                    continue;
                }

                PermissionState state = PermissionState.Undefined;

                if (catPerm != null)
                {
                    state = node.GetPermissionState(permission);
                }
                else if (chatPerm != null)
                {
                    state = node.GetChatChannelPermissionState(permission);
                }

                if (state == PermissionState.Undefined)
                {
                    continue;
                }
                else if (state == PermissionState.True)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            // No roles ever defined behavior: resort to false.
            return(false);
        }