using AutoMapper; using Microsoft.AspNetCore.DataProtection.KeyManagement; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.HttpContextUser; using WIDESEA_DTO.System; using WIDESEA_IRepository; using WIDESEA_IServices; using WIDESEA_Model.Models; using WIDESEA_Model.Models.System; using WIDESEA_Repository; namespace WIDESEA_Services { public class Sys_RoleService : ServiceBase, ISys_RoleService { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly ISys_MenuRepository _MenuRepository; private readonly ISys_MenuService _MenuService; private readonly ISys_RoleAuthRepository _RoleAuthRepository; private readonly IMapper _mapper; public Sys_RoleService(ISys_RoleRepository BaseDal, ISys_MenuRepository MenuRepository, ISys_MenuService MenuService, ISys_RoleAuthRepository roleAuthRepository, IUnitOfWorkManage unitOfWorkManage, IMapper mapper) : base(BaseDal) { _unitOfWorkManage = unitOfWorkManage; _MenuRepository = MenuRepository; _MenuService = MenuService; _RoleAuthRepository = roleAuthRepository; _mapper = mapper; } public List GetAllChildren(int roleId) { if (roleId <= 0) return new List() { }; var roles = GetAllRoleId(); if (App.User.IsRoleIdSuperAdmin(roleId)) return roles; var list = GetChildren(roles, roleId); return list; } public List GetAllRoleId() { List roles = BaseDal.QueryData().Select(s => new RoleNodes() { Id = s.RoleId, ParentId = s.ParentId, RoleName = s.RoleName }).ToList(); return roles; } /// /// 获取所有子节点 /// /// private List GetChildren(List roles, int roleId) { List rolesChildren = roles.Where(x => x.Id == roleId).Distinct().ToList(); for (int i = 0; i < rolesChildren.Count; i++) { RoleNodes node = rolesChildren[i]; var children = roles.Where(x => x.ParentId == node.Id && !rolesChildren.Any(c => c.Id == x.Id)).Distinct().ToList(); rolesChildren.AddRange(children); } return rolesChildren; } /// /// 编辑权限时获取当前用户下的所有角色与当前用户的菜单权限 /// /// public WebResponseContent GetCurrentTreePermission() { WebResponseContent content = GetCurrentUserTreePermission(); int roleId = App.User.RoleId; return WebResponseContent.Instance.OK(null, new { tree = content.Data, roles = GetAllChildren(roleId) }); } /// /// 编辑权限时,获取当前用户的所有菜单权限 /// /// public WebResponseContent GetCurrentUserTreePermission() { return GetUserTreePermission(App.User.RoleId); } /// /// 编辑权限时,获取指定角色的所有菜单权限 /// /// /// public WebResponseContent GetUserTreePermission(int roleId) { if (!App.User.IsRoleIdSuperAdmin(roleId) && App.User.RoleId != roleId) { if (!GetAllChildren(App.User.RoleId).Exists(x => x.Id == roleId)) { return WebResponseContent.Instance.Error("没有权限获取此角色的权限信息"); } } //获取用户权限 List permissions = _MenuRepository.GetPermissions(roleId); //权限用户权限查询所有的菜单信息 List menus = _MenuService.GetUserMenuList(roleId); //获取当前用户权限如:(Add,Search)对应的显示文本信息如:Add:添加,Search:查询 var data = menus.Select(x => new { Id = x.MenuId, Pid = x.ParentId, Text = x.MenuName, IsApp = x.MenuType == 1, Actions = _MenuService.GetActions(x.MenuId, x.Actions, permissions, roleId) }); return WebResponseContent.Instance.OK(null, data); } /// /// 保存角色权限 /// /// /// /// public WebResponseContent SavePermission(List userPermissions, int roleId) { WebResponseContent content = new WebResponseContent(); string message = ""; try { UserInfo user = App.User.GetCurrentUserInfo(); if (!GetAllChildren(user.RoleId).Exists(x => x.Id == roleId)) return WebResponseContent.Instance.Error("没有权限修改此角色的权限信息"); //当前用户的权限 List permissions = _MenuRepository.GetPermissions(App.User.RoleId); List originalMeunIds = new List(); //被分配角色的权限 List roleAuths = _RoleAuthRepository.QueryData(x => x.RoleId == roleId); List updateAuths = new List(); foreach (UserPermissionDTO x in userPermissions) { Permissions per = permissions.Where(p => p.MenuId == x.Id).FirstOrDefault(); //不能分配超过当前用户的权限 if (per == null) continue; //per.UserAuthArr.Contains(a.Value)校验权限范围 string[] arr = x.Actions == null || x.Actions.Count == 0 ? new string[0] : x.Actions.Where(a => per.UserAuthArr.Contains(a.Value)) .Select(s => s.Value).ToArray(); //如果当前权限没有分配过,设置Auth_Id默认为0,表示新增的权限 var auth = roleAuths.Where(r => r.MenuId == x.Id).Select(s => new { s.AuthId, s.AuthValue, s.MenuId }).FirstOrDefault(); string newAuthValue = string.Join(",", arr); //权限没有发生变化则不处理 if (auth == null || auth.AuthValue != newAuthValue) { updateAuths.Add(new Sys_RoleAuth() { RoleId = roleId, MenuId = x.Id, AuthValue = string.Join(",", arr), AuthId = auth == null ? 0 : auth.AuthId, ModifyDate = DateTime.Now, Modifier = user.UserName, CreateDate = DateTime.Now, Creater = user.UserName }); } else { originalMeunIds.Add(auth.MenuId); } } //更新权限 _RoleAuthRepository.UpdateData(updateAuths); //新增的权限 _RoleAuthRepository.AddData(updateAuths); //获取权限取消的权限 int[] authIds = roleAuths.Where(x => userPermissions.Select(u => u.Id) .ToList().Contains(x.MenuId) || originalMeunIds.Contains(x.MenuId)) .Select(s => s.AuthId) .ToArray(); List delAuths = roleAuths.Where(x => x.AuthValue != "" && !authIds.Contains(x.AuthId)).ToList(); delAuths.ForEach(x => { x.AuthValue = ""; }); //将取消的权限设置为"" _RoleAuthRepository.UpdateData(delAuths); int addCount = updateAuths.Where(x => x.AuthId <= 0).Count(); int updateCount = updateAuths.Where(x => x.AuthId > 0).Count(); string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff"); content.OK($"保存成功:新增加配菜单权限{addCount}条,更新菜单{updateCount}条,删除权限{delAuths.Count()}条"); } catch (Exception ex) { message = "异常信息:" + ex.Message + ex.StackTrace + ","; } return content; } } }