/// <summary> /// 初始化模块 /// </summary> public void Initialize() { using (ISession session = AuthoritySessionProvider.GetSession()) { session.SetBatchSize(200); using (ITransaction transaction = session.BeginTransaction()) { var modules = ReadJsonData(); foreach (var item in modules) { if (!item.IsCommon) { AuthorityModule authorityModule = new AuthorityModule { Name = item.Name, Icon = item.Icon, Enable = true }; InitAuthorityPage(ref authorityModule, item.Children); session.SaveOrUpdate(authorityModule); } } transaction.Commit(); } } }
/// <summary> /// 更新角色授权门店数据范围 /// </summary> /// <param name="roleId"></param> /// <param name="stores"></param> public void UpdateRoleStores(int roleId, int[] stores) { if (!stores.Any()) { throw new Exception("授权数据不能为空!"); } if (roleId <= 0) { throw new Exception("非法的角色!"); } using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { session.SetBatchSize(200); //删除历史记录 string hql = "DELETE FROM RoleStores WHERE RoleId = :p1"; session.CreateQuery(hql) .SetParameter("p1", roleId) .ExecuteUpdate(); foreach (var item in stores) { RoleStores roleStores = new RoleStores { RoleId = roleId, StoreId = item }; session.SaveOrUpdate(roleStores); } transaction.Commit(); } } }
/// <summary> /// 查询角色授权模块 /// </summary> /// <returns></returns> public RoleModuleDto QueryRoleEnableAuthorityModule(int roleId) { using (ISession session = AuthoritySessionProvider.GetSession()) { Role role = session .QueryOver <Role>() .And(x => x.Id == roleId) .Fetch() .Left .JoinQueryOver <RoleModule>(x => x.RoleModules) .Fetch() .Left .JoinQueryOver(x => x.AuthorityModule) .List() .FirstOrDefault(); IList <AuthorityModule> authorityModules = session .QueryOver <AuthorityModule>() .And(x => x.Enable) .List(); if (role != null) { IList <AuthorityModule> roleModules = role.RoleModules.Select(x => x.AuthorityModule).ToList(); IList <AuthorityModule> intersectModules = roleModules.Intersect(authorityModules).Distinct().ToList(); foreach (var item in authorityModules) { if (intersectModules.Contains(item)) { item.Checked = true; } else { item.Checked = false; } } RoleModuleDto roleModuleDto = new RoleModuleDto { RoleDto = AutoMapper.Mapper.Map <RoleDto>(role), AuthorityModuleDtos = AutoMapper.Mapper.Map <IList <AuthorityModuleDto> >(authorityModules) }; return(roleModuleDto); } else { return(null); } } }
/// <summary> /// 查询角色授权门店 /// </summary> /// <param name="roleId"></param> /// <returns></returns> public IList <int> QueryRoleStores(int roleId) { using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { var result = session.QueryOver <RoleStores>() .And(x => x.RoleId == roleId) .List() .Select(x => x.StoreId) .ToList(); transaction.Commit(); return(result); } } }
/// <summary> /// 更新角色权限 /// </summary> /// <param name="roleId"></param> /// <param name="permissionIds"></param> public void UpdateRolePermission(int roleId, int[] permissionIds) { using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { Role role = session .QueryOver <Role>() .And(x => x.Id == roleId) .List() .FirstOrDefault(); if (role != null) { if (role.RolePermissions.Any()) { role.RolePermissions.Clear(); } IList <Permission> permissions = session .QueryOver <Permission>() .And(Restrictions.In("Id", permissionIds)) .Fetch(SelectMode.Fetch, x => x.AuthorityPage) .JoinQueryOver <AuthorityPage>(x => x.AuthorityPage) .List(); foreach (var item in permissions) { RolePermission rolePermission = new RolePermission { AuthorityPage = item.AuthorityPage, Permission = item, Role = role }; role.RolePermissions.Add(rolePermission); } session.SaveOrUpdate(role); } transaction.Commit(); } } }
/// <summary> /// 构建角色Vue动态菜单 /// </summary> /// <param name="roleId"></param> /// <returns></returns> public IList <VueMenuDto> GenerateVueMenu(int roleId, bool isSuper) { using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { IList <VueMenuDto> vueMenuDtos = new List <VueMenuDto>(); if (!isSuper) { RolePermission rolePermission = null; Permission permission = null; Role role = session .QueryOver <Role>() .And(x => x.Id == roleId) .JoinQueryOver(x => x.RolePermissions, () => rolePermission) .JoinAlias(x => x.Permission, () => permission) .JoinQueryOver(x => x.AuthorityPage) .List() .FirstOrDefault(); if (role != null) { //构建授权模块 foreach (var item in role.RoleModules) { var module = item.AuthorityModule; if (role.RolePermissions.Select(x => x.AuthorityPage).Any(x => x.AuthorityModule.Name == module.Name)) { VueMenuDto menuDto = new VueMenuDto { Name = module.Name, Icon = module.Icon }; var pages = role.RolePermissions.Select(x => x.AuthorityPage).Where(x => x.AuthorityModule.Name == module.Name).Distinct(); //合并不需要验证的页面 pages = pages.Union(module.AuthorityPages.Where(x => x.NotAuth).ToList()); foreach (var page in pages) { if (page.Enable) { VueMenuPageDto pageDto = new VueMenuPageDto { Name = page.Name, Permission = role.RolePermissions.Where(x => x.AuthorityPage.Name == page.Name).Select(x => x.Permission.Value).Sum() }; menuDto.Pages.Add(pageDto); } } vueMenuDtos.Add(menuDto); } } } } else { IList <AuthorityModule> modules = session .QueryOver <AuthorityModule>() .And(x => x.Enable) .List(); foreach (var module in modules) { VueMenuDto menuDto = new VueMenuDto { Name = module.Name, Icon = module.Icon }; foreach (var page in module.AuthorityPages) { if (page.Enable) { VueMenuPageDto pageDto = new VueMenuPageDto { Name = page.Name, Permission = page.Permissions.Select(x => x.Value).Sum() }; menuDto.Pages.Add(pageDto); } } vueMenuDtos.Add(menuDto); } } transaction.Commit(); return(vueMenuDtos); } } }
/// <summary> /// 查询角色权限树 /// </summary> /// <param name="roleId"></param> /// <returns></returns> public IList <PermissionTreeDto> QueryRolePermissionForTree(int roleId) { using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { List <PermissionTreeDto> trees = new List <PermissionTreeDto>(); PermissionTreeDto tree = new PermissionTreeDto { Title = "系统权限", Expand = true, Category = "root", Id = 0, Children = new List <PermissionTreeDto>() }; Role role = session .QueryOver <Role>() .And(x => x.Id == roleId) .Fetch(SelectMode.ChildFetch, x => x.RoleModules) .Fetch(SelectMode.ChildFetch, x => x.RolePermissions) .List() .FirstOrDefault(); if (role.RoleModules.Any()) { foreach (var module in role.RoleModules) { PermissionTreeDto childModule = new PermissionTreeDto { Title = module.AuthorityModule.Name, Expand = false, Id = module.AuthorityModule.Id, Category = "module", Children = new List <PermissionTreeDto>() }; AuthorityModule authorityModule = module.AuthorityModule; if (authorityModule.AuthorityPages.Any()) { foreach (var page in authorityModule.AuthorityPages) { PermissionTreeDto childPage = new PermissionTreeDto { Title = page.Name, Expand = true, Id = page.Id, Category = "page", Children = new List <PermissionTreeDto>() }; if (page.Permissions.Any()) { var rolePermissions = role.RolePermissions.Select(x => x.Permission); foreach (var permission in page.Permissions) { if (page.Id == permission.AuthorityPage.Id) { bool isChecked = rolePermissions.Contains(permission); PermissionTreeDto childPermission = new PermissionTreeDto { Title = permission.Name, Expand = true, Id = permission.Id, Category = "per", Checked = isChecked, Children = new List <PermissionTreeDto>() }; childPage.Children.Add(childPermission); } } } childModule.Children.Add(childPage); } } tree.Children.Add(childModule); } } trees.Add(tree); transaction.Commit(); return(trees); } } }
/// <summary> /// 职务模块授权 /// </summary> /// <param name="roleId"></param> /// <param name="moduleIds"></param> public void UpdateRoleModule(int roleId, int[] moduleIds) { using (ISession session = AuthoritySessionProvider.GetSession()) { using (ITransaction transaction = session.BeginTransaction()) { Role role = session .QueryOver <Role>() .And(x => x.Id == roleId) .List() .FirstOrDefault(); if (role.RoleModules.Any()) { role.RoleModules.Clear(); } if (role.RolePermissions.Any()) { role.RolePermissions.Clear(); } IList <AuthorityModule> authorityModules = session .QueryOver <AuthorityModule>() .And(Restrictions.In("Id", moduleIds)) .And(x => x.Enable) .List(); foreach (var item in authorityModules) { RoleModule roleModule = new RoleModule { Role = role, AuthorityModule = item }; if (item.AuthorityPages.Any()) { foreach (var page in item.AuthorityPages) { foreach (var permission in page.Permissions) { RolePermission rolePermission = new RolePermission { Permission = permission, AuthorityPage = page, Role = role }; role.RolePermissions.Add(rolePermission); } } } role.RoleModules.Add(roleModule); } session.SaveOrUpdate(role); transaction.Commit(); } } }