using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SolarPower.Models; using SolarPower.Models.Role; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class RoleController : MyBaseController { private readonly IRoleRepository roleRepository; public RoleController(IRoleRepository roleRepository) : base() { this.roleRepository = roleRepository; } public IActionResult Index() { return View(); } /// /// 取得下拉式公司選單,須為Deleted: 0 /// /// [HttpGet] public async Task>> GetRoleSelectOptionListAsync(int companyId) { ApiResult> apiResult = new ApiResult>(); try { var roleSelectItemLists = await roleRepository.GetRoleSelectOptionListAsync(companyId); apiResult.Code = "0000"; apiResult.Data = roleSelectItemLists; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 角色管理列表 /// /// /// [HttpPost] public async Task RoleList(PostRoleFilter post) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List roles = null; try { roles = await roleRepository.GetAllByFilterAsync(post); totalRecords = roles.Count(); recFilter = roles.Count(); foreach(var role in roles) { if(role.Layer == (int)RoleLayerEnum.PlatformAdmin || role.Layer == (int)RoleLayerEnum.CompanyAdmin) { //管理階層的角色無法被刪除 role.Function = ""; } else { role.Function = @" "; } } apiResult.Code = "0000"; apiResult.Data = roles; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); var result = Json(new { recordsTotal = totalRecords, recordsFiltered = recFilter, data = apiResult }); return result; } /// /// 取得單一公司角色 /// /// /// [HttpPost] public async Task> GetOneRole(int id) { ApiResult apiResult = new ApiResult(); Role role = null; try { role = await roleRepository.GetOneRoleAsync(id); if (role == null) { apiResult.Code = "9994"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } apiResult.Code = "0000"; apiResult.Data = role; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 新增 / 修改 公司角色 /// /// /// [HttpPost] public async Task> SaveRole(PostRole post) { ApiResult apiResult = new ApiResult(); Role role = null; try { role = await roleRepository.GetOneRoleAsync(post.Id); if (role == null) { if (post.Id != 0) { apiResult.Code = "9994"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } if(myUser.IsGod != 1 && !IsPlatformLayer(myUser.Role.Layer) && myUser.CompanyId != post.SelectedCompanyId) { //非超級使用者或平台人員,就只能新增自己公司的角色 apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } #region 新增公司角色 role = new Role() { CompanyId = post.SelectedCompanyId, Name = post.Name, CreatedBy = myUser.Id, }; if (IsPlatformLayer(myUser.Role.Layer)) { //平台新增角色 Layer,為平台使用者階層 role.Layer = 1; } else { //公司新增角色 Layer,為公司使用者階層 role.Layer = 3; } List properties = new List() { "CompanyId", "Name", "Layer", "CreatedBy", }; await roleRepository.AddAsync(role, properties); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion } else { #region 修改公司角色 if (myUser.IsGod != 1 && !IsPlatformLayer(myUser.Role.Layer) && myUser.CompanyId != post.SelectedCompanyId) { //非超級使用者或平台人員,就只能修改自己公司的角色 apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } UpdateRole update = new UpdateRole() { Id = post.Id, Name = post.Name, UpdatedBy = myUser.Id, }; List properties = new List() { "Id", "Name", "UpdatedBy", }; await roleRepository.UpdateRoleAsync(update, properties); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 軟刪除單一公司角色 /// /// /// [HttpPost] public async Task> DeleteOneRole(int id) { ApiResult apiResult = new ApiResult(); Role role = null; try { role = await roleRepository.GetOneRoleAsync(id); if (role == null) { apiResult.Code = "9994"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await roleRepository.DeleteOne(role.Id); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 取得公司擁有的權限池 /// /// /// [HttpPost] public async Task GetCompanyAuthPageList(int companyId) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List companyAuthPages = null; try { companyAuthPages = await roleRepository.GetAllCompanyAuthPageAsync(companyId); totalRecords = companyAuthPages.Count(); recFilter = companyAuthPages.Count(); apiResult.Code = "0000"; apiResult.Data = companyAuthPages; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + companyId); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); var result = Json(new { recordsTotal = totalRecords, recordsFiltered = recFilter, data = apiResult }); return result; } /// /// 角色權限管理列表 /// /// /// [HttpPost] public async Task RoleAuthList(PostRoleAuthFilter post) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List roleAuths = null; try { roleAuths = await roleRepository.GetAllAuthByRoleIdAsync(post.SelectedRoleId); totalRecords = roleAuths.Count(); recFilter = roleAuths.Count(); apiResult.Code = "0000"; apiResult.Data = roleAuths; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); var result = Json(new { recordsTotal = totalRecords, recordsFiltered = recFilter, data = apiResult }); return result; } /// /// 取得該公司角色尚未加入的權限 /// /// /// public async Task GetRoleNotAuthPageList(PostRoleAuthFilter post) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List roleAuths = null; try { roleAuths = await roleRepository.GetRoleNotAuthPageAsync(post); totalRecords = roleAuths.Count(); recFilter = roleAuths.Count(); apiResult.Code = "0000"; apiResult.Data = roleAuths; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); var result = Json(new { recordsTotal = totalRecords, recordsFiltered = recFilter, data = apiResult }); return result; } /// /// 儲存公司角色的權限 /// /// /// [HttpPost] public async Task> SaveRoleAuth(PostRoleAuth post) { ApiResult apiResult = new ApiResult(); Role role = null; try { role = await roleRepository.GetOneRoleAsync(post.SelectedRoleId); if (role == null) { apiResult.Code = "9994"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } List roleAuths = new List(); foreach (var checkAuth in post.CheckAuths) { RoleAuth roleAuth = new RoleAuth(); roleAuth.Id = role.Id; roleAuth.AuthCode = checkAuth; roleAuth.CreatedBy = myUser.Id; roleAuths.Add(roleAuth); } List properties = new List() { "Id", "AuthCode", "CreatedBy", }; await roleRepository.AddRoleAuthAsync(roleAuths, properties); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } [HttpPost] public async Task> DeleteOneRoleAuth(PostDeleteRoleAuth post) { ApiResult apiResult = new ApiResult(); Role role = null; try { role = await roleRepository.GetOneRoleAsync(post.RoleId); if (role == null) { apiResult.Code = "9994"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await roleRepository.PurgeOneRoleAuthAsync(post.RoleId, post.AuthCode); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }