using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WIDESEA_Core.Controllers.Basic; using WIDESEA_Core.Enums; using WIDESEA_Core.Extensions; using WIDESEA_Core.Filters; using WIDESEA_Core.ManageUser; using WIDESEA_Core.UserManager; using WIDESEA_Core.Utilities; using WIDESEA_Entity.AttributeManager; using WIDESEA_Entity.DomainModels; using WIDESEA_System.IRepositories; using WIDESEA_System.IServices; using WIDESEA_System.Repositories; using WIDESEA_System.Services; namespace WIDESEA_System.Controllers { [Route("api/role")] public partial class Sys_RoleController { private readonly ISys_RoleService _service;//访问业务代ç private readonly ISys_RoleRepository _repository; private readonly IHttpContextAccessor _httpContextAccessor; [ActivatorUtilitiesConstructor] public Sys_RoleController( ISys_RoleService service, ISys_RoleRepository repository, IHttpContextAccessor httpContextAccessor ) : base(service) { _service = service; _repository = repository; _httpContextAccessor = httpContextAccessor; } [HttpPost, Route("getCurrentTreePermission")] [ApiActionPermission(ActionPermissionOptions.Search)] public async Task<IActionResult> GetCurrentTreePermission() { return Json(await Service.GetCurrentTreePermission()); } [HttpPost, Route("getUserTreePermission")] [ApiActionPermission(ActionPermissionOptions.Search)] public async Task<IActionResult> GetUserTreePermission(int roleId) { return Json(await Service.GetUserTreePermission(roleId)); } [HttpPost, Route("savePermission")] [ApiActionPermission(ActionPermissionOptions.Update)] public async Task<IActionResult> SavePermission([FromBody] List<UserPermissions> userPermissions, int roleId) { return Json(await Service.SavePermission(userPermissions, roleId)); } /// <summary> /// 获å–当å‰è§’色下的所有角色 /// </summary> /// <returns></returns> [HttpPost, Route("getUserChildRoles")] [ApiActionPermission(ActionPermissionOptions.Search)] public IActionResult GetUserChildRoles() { int roleId = UserContext.Current.RoleId; var data = RoleContext.GetAllChildren(UserContext.Current.RoleId); if (UserContext.Current.IsSuperAdmin) { return Json(WebResponseContent.Instance.OK(null, data)); } //䏿˜¯è¶…级管ç†ï¼Œå°†è‡ªå·±çš„角色查出æ¥ï¼Œåœ¨æ ‘å½¢èœå•ä¸Šä½œä¸ºæ ¹èŠ‚ç‚¹ var self = _repository.FindAsIQueryable(x => x.Role_Id == roleId) .Select(s => new WIDESEA_Core.UserManager.RoleNodes() { Id = s.Role_Id, ParentId = 0,//将自己的角色作为root节点 RoleName = s.RoleName }).ToList(); data.AddRange(self); return Json(WebResponseContent.Instance.OK(null, data)); } /// <summary> /// treetable 获å–å节点数æ®(2021.05.02) /// </summary> /// <param name="loadData"></param> /// <returns></returns> [ApiActionPermission(ActionPermissionOptions.Search)] [HttpPost, Route("GetPageData")] public override ActionResult GetPageData([FromBody] PageDataOptions loadData) { //èŽ·å–æ ¹èŠ‚ç‚¹æ•°æ®(对应Sys_Role1.jsä¸searchBefore方法) if (loadData.Value.GetInt() == 1) { return GetTreeTableRootData(loadData).Result; } return base.GetPageData(loadData); } /// <summary> /// treetable 获å–å节点数æ®(2021.05.02) /// </summary> /// <returns></returns> [HttpPost, Route("getTreeTableRootData")] [ApiActionPermission(ActionPermissionOptions.Search)] public async Task<ActionResult> GetTreeTableRootData([FromBody] PageDataOptions options) { //页é¢åŠ è½½æ ¹èŠ‚ç‚¹æ•°æ®æ¡ä»¶x => x.ParentId == 0,è‡ªå·±æ ¹æ®éœ€è¦è®¾ç½® var query = _repository.FindAsIQueryable(x => true); if (UserContext.Current.IsSuperAdmin) { query = query.Where(x => x.ParentId == 0); } else { int roleId = UserContext.Current.RoleId; query = query.Where(x => x.Role_Id == roleId); } var queryChild = _repository.FindAsIQueryable(x => true); var rows = await query.TakeOrderByPage(options.Page, options.Rows) .OrderBy(x => x.Role_Id).Select(s => new { s.Role_Id, // ParentId=0, s.ParentId, s.RoleName, s.DeptName, s.Dept_Id, s.Enable, s.CreateDate, s.Creator, s.Modifier, s.ModifyDate, s.OrderNo, hasChildren = queryChild.Any(x => x.ParentId == s.Role_Id) }).ToListAsync(); return JsonNormal(new { total = await query.CountAsync(), rows }); } /// <summary> ///treetable 获å–å节点数æ®(2021.05.02) /// </summary> /// <returns></returns> [HttpPost, Route("getTreeTableChildrenData")] [ApiActionPermission(ActionPermissionOptions.Search)] public async Task<ActionResult> GetTreeTableChildrenData(int roleId) { if (!UserContext.Current.IsSuperAdmin && roleId != UserContext.Current.RoleId && !RoleContext.GetAllChildren(UserContext.Current.RoleId).Any(x => x.Id == roleId)) { return JsonNormal(new { rows = new object[] { } }); } //ç‚¹å‡»èŠ‚ç‚¹æ—¶ï¼ŒåŠ è½½åèŠ‚ç‚¹æ•°æ® var roleRepository = Sys_RoleRepository.Instance.FindAsIQueryable(x => true); var rows = await roleRepository.Where(x => x.ParentId == roleId) .Select(s => new { s.Role_Id, s.ParentId, s.RoleName, s.DeptName, s.Dept_Id, s.Enable, s.CreateDate, s.Creator, s.Modifier, s.ModifyDate, s.OrderNo, hasChildren = roleRepository.Any(x => x.ParentId == s.Role_Id) }).ToListAsync(); return JsonNormal(new { rows }); } } }