/// <summary> /// Get permission from the list by its permission value. /// </summary> /// <param name="permissionValue"></param> /// <returns></returns> public static ApplicationPermission GetPermissionByValue(string permissionValue) { return(AllPermissions.FirstOrDefault(p => p.Value == permissionValue)); }
private Result RemoveWildcardPermission(SocketRole role, string perm) { var splitPerm = perm.Split('.'); var rolesSplitPerms = new List <string[]>(Permissions[role.Id].Select(x => x.Split('.'))); if (splitPerm.All(x => x != "*")) { return new Result { Type = ResultType.Fail, Message = "Invalid permission format." } } ; // Catch foo.bar* if (splitPerm.Length > 2) { return new Result { Type = ResultType.Fail, Message = "Invalid permission format." } } ; // Catch *.foo.bar var parent = splitPerm[0]; var child = splitPerm[1]; if (parent == "*") { if (child == "*") { // Remove all existing permissions for all parents and children Permissions.Remove(role.Id); return(new Result { Type = ResultType.Success, Message = "Removed all permissions from role." }); } else { // Add all child permission matches for all existing parent permissions foreach (var p in rolesSplitPerms.Where(x => x[1] == child)) { Permissions[role.Id].Remove(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}")); } if (Permissions[role.Id].Count == 0) { Permissions.Remove(role.Id); } return(new Result { Type = ResultType.Success, Message = $"Removed all child permissions `{child}` from role." }); } } else { // Add all child permissions for the given parent permission foreach (var p in rolesSplitPerms.Where(x => x[0] == parent)) { Permissions[role.Id].Remove(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}")); } if (Permissions[role.Id].Count == 0) { Permissions.Remove(role.Id); } return(new Result { Type = ResultType.Success, Message = $"Removed all child permissions of `{parent}` from role." }); } }
private Result AddWildcardPermission(SocketRole role, string perm) { var allSplitPerms = AllPermissions.Select(x => x.Split('.')); var splitPerm = perm.Split('.'); if (splitPerm.All(x => x != "*")) { return new Result { Type = ResultType.Fail, Message = "Invalid permission format." } } ; // Catch foo.bar* if (splitPerm.Length > 2) { return new Result { Type = ResultType.Fail, Message = "Invalid permission format." } } ; // Catch *.foo.bar var parent = splitPerm[0]; var child = splitPerm[1]; bool setCreated = false; if (!Permissions.ContainsKey(role.Id)) { Permissions.Add(role.Id, new HashSet <string>()); setCreated = true; } if (parent == "*") { if (child == "*") { // Add all existing permissions for all parents and children foreach (var p in AllPermissions) { Permissions[role.Id].Add(p); } return(new Result { Type = ResultType.Success, Message = setCreated ? "Created permission set for role and added all existing permissions." : "Added all existing permissions to role." }); } else { // Add all child permission matches for all existing parent permissions foreach (var p in allSplitPerms.Where(x => x[1] == child)) { Permissions[role.Id].Add(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}")); } return(new Result { Type = ResultType.Success, Message = setCreated ? $"Created permission set for role and added all child permissions matching `{child}`." : $"Added all child permissions `{child}` to role." }); } } else { // Add all child permissions for the given parent permission foreach (var p in allSplitPerms.Where(x => x[0] == parent)) { Permissions[role.Id].Add(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}")); } return(new Result { Type = ResultType.Success, Message = setCreated ? $"Created permission set for role and added all child permissions of `{parent}`." : $"Added all child permissions of `{parent}` to role." }); } }
//保存权限 public JsonResult SaveRight(int permissionId, string rightName, string rightDesc, string controllerName, string actionName, string iconimg, int rightType, int parentRight, int showOrder = -1, string areaName = "") { try { SysPermission menu = AllPermissions.FirstOrDefault(p => p.PermissionId == permissionId); if (menu == null) { //新增 menu = new SysPermission { PermissionName = rightName, PermissionCode = rightDesc, LinkUrl = "", Controller = controllerName, Action = actionName, ParentId = parentRight, Description = rightDesc, Sort = showOrder, Icon = iconimg, PermissionType = rightType, AreaName = areaName }; _permissionManager.AddPermission(menu); AllPermissions.Add(menu); } else { menu.PermissionName = rightName; menu.Description = rightDesc; menu.PermissionCode = rightDesc; menu.LinkUrl = ""; menu.Controller = controllerName; menu.Action = actionName; menu.Sort = showOrder; menu.Icon = iconimg; menu.PermissionType = rightType; menu.AreaName = areaName; if (menu.Sort == int.MinValue) { menu.Sort = 0; } int newParentId = parentRight; //修改前的判断 if (menu.PermissionId == newParentId) { return(Json(new { result = 0, content = RetechWing.LanguageResources.Right.UpRightFails }, JsonRequestBehavior.AllowGet)); } var childs = new List <int>(); GetChildRights(menu.PermissionId, childs); if (childs.IndexOf(newParentId) >= 0) { return(Json(new { result = 0, content = RetechWing.LanguageResources.Right.UpRightFailsOne }, JsonRequestBehavior.AllowGet)); } menu.ParentId = newParentId; _permissionManager.UpdatePermission(menu); } if (menu.PermissionId > 0) { return(Json(new { result = 1, content = RetechWing.LanguageResources.Common.SaveSuccess }, JsonRequestBehavior.AllowGet)); } return(Json(new { result = 0, content = RetechWing.LanguageResources.Common.SaveFailed }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { result = 0, content = RetechWing.LanguageResources.Common.SaveFailed + ex.Message }, JsonRequestBehavior.AllowGet)); } }