/// <summary> /// 查询所有权限字典 /// </summary> /// <param name="dbContext">DbContext</param> /// <param name="role">角色</param> /// <param name="rights">权限列表</param> public static void SelectAllRights(WarehouseEntryDbContext dbContext, SecurityRole role, List<SystemRight> rights) { if (dbContext == null) { throw new ArgumentNullException("dbContext"); } if (role == null) { throw new ArgumentNullException("role"); } if (string.IsNullOrWhiteSpace(role.RoleName)) { throw new InvalidEnumArgumentException("role"); } rights.ForEach(r => r.Enabled = dbContext.SystemRight.Any(dr => dr.SecurityRole.RoleName == role.RoleName && dr.SystemMenu.MenuName == r.SystemMenu.MenuName && dr.Enabled)); }
/// <summary> /// 查询用户权限字典 /// </summary> /// <param name="dbContext">DbContext</param> /// <param name="role">角色实体</param> /// <param name="menus">菜单名称列表</param> /// <returns></returns> public static List<SystemRight> SelectRights(WarehouseEntryDbContext dbContext, SecurityRole role, string[] menus) { if (dbContext == null) { throw new ArgumentNullException("dbContext"); } if (role == null) { throw new ArgumentNullException("role"); } if (string.IsNullOrWhiteSpace(role.RoleName)) { throw new InvalidEnumArgumentException("role"); } if (menus == null) { throw new ArgumentNullException("menus"); } List<SystemMenu> dataMenus = dbContext.SystemMenu.Where(m => menus.Contains(m.MenuName)).ToList(); return dataMenus.Select(dataMenu => dbContext.SystemRight.FirstOrDefault(r => r.SecurityRole.RoleName == role.RoleName && r.SystemMenu.MenuName == dataMenu.MenuName) ?? new SystemRight { SecurityRole = role, SystemMenu = dataMenu }).ToList(); }
/// <summary> /// 创建角色 /// </summary> /// <param name="roleName">角色名</param> public static BusinessRoleModel CreateRole(string roleName) { roleName = CheckRoleName(roleName); using (WarehouseEntryDbContext dbContext = DbContextHelper.CreateDbContext()) { SecurityRole role = SecurityData.SelectRole(dbContext, roleName); if (role != null) { throw new ErrorException("RoleNameNotUnique", roleName); } role = new SecurityRole { RoleName = roleName, Enabled = true }; dbContext.SecurityRole.Add(role); dbContext.SaveChanges(); return new BusinessRoleModel { RoleName = roleName, Enabled = role.Enabled }; } }