From a4fa913713d801fa6a8d46f0edf9ce82e0ea46cf Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 11 Jun 2021 15:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E5=B8=B3=E8=99=9F?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E3=80=81=E5=85=AC=E5=8F=B8=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Controllers/CompanyController.cs | 90 ++- SolarPower/Controllers/MyBaseController.cs | 11 +- SolarPower/Controllers/RoleController.cs | 435 ++++++++++++ SolarPower/Controllers/UserController.cs | 19 +- SolarPower/Models/Company.cs | 12 + SolarPower/Models/ErrorCode.cs | 1 + SolarPower/Models/MyBaseModel.cs | 17 + SolarPower/Models/Role.cs | 89 +++ SolarPower/Models/User.cs | 2 + .../Repository/Implement/CompanyRepository.cs | 69 +- .../Repository/Implement/RepositoryBase.cs | 27 + .../Repository/Implement/RoleRepository.cs | 267 +++++++- .../Repository/Implement/UserRepository.cs | 11 +- .../Interface/ICompanyRepository.cs | 21 +- .../Repository/Interface/IRepositoryBase.cs | 1 + .../Repository/Interface/IRoleRepository.cs | 55 ++ .../Repository/Interface/IUserRepository.cs | 7 +- SolarPower/SolarPower.csproj | 1 + SolarPower/Views/Company/Index.cshtml | 124 +++- SolarPower/Views/Shared/_Layout.cshtml | 2 +- SolarPower/Views/User/Index.cshtml | 644 +++++++++++++++++- SolarPower/Views/User/_RoleAuth.cshtml | 81 +-- SolarPower/Views/User/_RoleManager.cshtml | 69 +- SolarPower/Views/User/_UserManager.cshtml | 14 +- .../upload/company_logo/20210611090212.png | Bin 0 -> 907 bytes 25 files changed, 1912 insertions(+), 157 deletions(-) create mode 100644 SolarPower/wwwroot/upload/company_logo/20210611090212.png diff --git a/SolarPower/Controllers/CompanyController.cs b/SolarPower/Controllers/CompanyController.cs index ddac375..7773f67 100644 --- a/SolarPower/Controllers/CompanyController.cs +++ b/SolarPower/Controllers/CompanyController.cs @@ -80,7 +80,7 @@ namespace SolarPower.Controllers //if(mySimpleCompany.Id == 1) //{ company.Function = @" - 權限池 + 權限池 "; //} @@ -199,7 +199,7 @@ namespace SolarPower.Controllers Phone = post.Phone, Address = post.Address, RegisterUpperLimit= post.RegisterUpperLimit, - CreatedBy = mySimpleUser.Id + CreatedBy = myUser.Id }; List properties = new List() @@ -239,7 +239,7 @@ namespace SolarPower.Controllers Phone = post.Phone, Address = post.Address, RegisterUpperLimit = post.RegisterUpperLimit, - UpdatedBy = mySimpleUser.Id, + UpdatedBy = myUser.Id, }; @@ -311,5 +311,89 @@ namespace SolarPower.Controllers return apiResult; } + + /// + /// 透過公司編號,取得公司權限池 + /// + /// + /// + public async Task GetCompanyAuthByCompanyId(int id) + { + ApiResult> apiResult = new ApiResult>(); + + int totalRecords = 0; //總資料筆數 + int recFilter = 0; //過濾後資料筆數 + + List companyAuths = null; + + try + { + + companyAuths = await companyRepository.GetCompanyAuthByCompanyId(id); + + totalRecords = companyAuths.Count(); + recFilter = companyAuths.Count(); + + apiResult.Code = "0000"; + apiResult.Data = companyAuths; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); + 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> GetRemainingRegisterNumber(int id) + { + ApiResult apiResult = new ApiResult(); + + Company company = null; + + try + { + company = await companyRepository.GetOneCompany(id); + + if (company == null) + { + apiResult.Code = "9996"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + var registerNumber = await companyRepository.GetRegisterNumberByCompanyId(id); + + + + apiResult.Code = "0000"; + apiResult.Msg = errorCode.GetString(apiResult.Code); + apiResult.Data = company.RegisterUpperLimit - registerNumber; + } + 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; + } } } diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index 89b28e4..00357ff 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -32,7 +32,7 @@ namespace SolarPower.Controllers private ICompanyRepository companyRepository => HttpContext?.RequestServices.GetService(); private IOperatorLogRepository operatorLogRepository => HttpContext?.RequestServices.GetService(); - protected SimpleUser mySimpleUser = null; + protected MyUser myUser = null; protected SimpleCompany mySimpleCompany = null; public string controllerName; public string actionName; @@ -59,10 +59,11 @@ namespace SolarPower.Controllers return; } - mySimpleUser = userRepository.GetOneNormalSimpleUserByAccount(myAccount); - mySimpleCompany = companyRepository.GetOneNormalSimpleCompanyById(mySimpleUser.CompanyId); + //取得當前登入使用者資訊 + myUser = userRepository.GetMyUserInfoByAccount(myAccount); + myUser.Company = companyRepository.GetMyCompanyInfoById(myUser.CompanyId); - ViewBag.systemAdminName = mySimpleUser.Name; + ViewBag.myUser = myUser; #region 記錄人員操作紀錄 var content = JsonConvert.SerializeObject(filterContext.ActionArguments); @@ -72,7 +73,7 @@ namespace SolarPower.Controllers ControllerName = controllerName, ActionName = actionName, Parameter = content.CompareTo("{}") == 0? null : content, - CreatedBy = mySimpleUser.Id, + CreatedBy = myUser.Id, }; List properties = new List() diff --git a/SolarPower/Controllers/RoleController.cs b/SolarPower/Controllers/RoleController.cs index 6516cbe..62e6a9e 100644 --- a/SolarPower/Controllers/RoleController.cs +++ b/SolarPower/Controllers/RoleController.cs @@ -47,5 +47,440 @@ namespace SolarPower.Controllers 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(); + + 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; + } + + #region 新增公司角色 + EDFunction edFunction = new EDFunction(); + + role = new Role() + { + CompanyId = post.SelectedCompanyId, + Name = post.Name, + Layer = 3, + CreatedBy = myUser.Id, + }; + + List properties = new List() + { + "CompanyId", + "Name", + "Layer", + "CreatedBy", + }; + + await roleRepository.AddAsync(role, properties); + + apiResult.Code = "0000"; + apiResult.Msg = "儲存成功"; + #endregion + } + else + { + #region 修改使用者 + 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; + } } } diff --git a/SolarPower/Controllers/UserController.cs b/SolarPower/Controllers/UserController.cs index 6d67194..09f8784 100644 --- a/SolarPower/Controllers/UserController.cs +++ b/SolarPower/Controllers/UserController.cs @@ -41,7 +41,7 @@ namespace SolarPower.Controllers try { - var user = await userRepository.GetOneAsync(mySimpleUser.Id); + var user = await userRepository.GetOneAsync(myUser.Id); apiResult.Code = "0000"; apiResult.Data = user; @@ -69,7 +69,7 @@ namespace SolarPower.Controllers User user = null; try { - user = await userRepository.GetOneAsync(mySimpleUser.Id); + user = await userRepository.GetOneAsync(myUser.Id); if (user == null) { @@ -83,7 +83,7 @@ namespace SolarPower.Controllers Name = post.Name, Email = post.Email, Phone = post.Phone, - UpdatedBy = mySimpleUser.Id, + UpdatedBy = myUser.Id, Id = user.Id }; @@ -126,7 +126,7 @@ namespace SolarPower.Controllers User user = null; try { - user = await userRepository.GetOneAsync(mySimpleUser.Id); + user = await userRepository.GetOneAsync(myUser.Id); if (user == null) { @@ -154,7 +154,7 @@ namespace SolarPower.Controllers UpdatePassword update = new UpdatePassword() { Password = edFunction.GetSHA256Encryption(post.NewPassword), - UpdatedBy = mySimpleUser.Id, + UpdatedBy = myUser.Id, Id = user.Id }; @@ -300,8 +300,9 @@ namespace SolarPower.Controllers Email = post.Email, Account = post.Account, Password = edFunction.GetSHA256Encryption(post.Account), + RoleId = post.RoleId, Phone = post.Phone, - CreatedBy = mySimpleUser.Id, + CreatedBy = myUser.Id, }; List properties = new List() @@ -311,6 +312,7 @@ namespace SolarPower.Controllers "Email", "Account", "Password", + "RoleId", "Phone", "CreatedBy", }; @@ -330,7 +332,7 @@ namespace SolarPower.Controllers Name = post.Name, Email = post.Email, Phone = post.Phone, - UpdatedBy = mySimpleUser.Id, + UpdatedBy = myUser.Id, }; @@ -338,7 +340,6 @@ namespace SolarPower.Controllers { "Id", "Name", - "Status", "Email", "Phone", "UpdatedBy", @@ -364,7 +365,7 @@ namespace SolarPower.Controllers } /// - /// 軟刪除單一系統管理員 + /// 軟刪除單一使用者 /// /// /// diff --git a/SolarPower/Models/Company.cs b/SolarPower/Models/Company.cs index 870a865..2f80274 100644 --- a/SolarPower/Models/Company.cs +++ b/SolarPower/Models/Company.cs @@ -80,4 +80,16 @@ namespace SolarPower.Models.Company public string Address { get; set; } //地址 public int RegisterUpperLimit { get; set; } //註冊上限 } + + /// + /// 公司權限池 + /// + public class CompanyAuth + { + public string AuthCode { get; set; } + public string MainName { get; set; } + public string SubName { get; set; } + public string ControlName { get; set; } + public byte CheckAuth { get; set; } + } } diff --git a/SolarPower/Models/ErrorCode.cs b/SolarPower/Models/ErrorCode.cs index a3a15ca..4d70362 100644 --- a/SolarPower/Models/ErrorCode.cs +++ b/SolarPower/Models/ErrorCode.cs @@ -17,6 +17,7 @@ namespace SolarPower.Models { { "0000", "OK" }, { "0001", "傳入參數錯誤。" }, + { "9994", "查無該公司角色"}, { "9995", "該統一編號已被使用。" }, { "9996", "查無該公司資訊。" }, { "9997", "帳號或密碼輸入錯誤。"}, diff --git a/SolarPower/Models/MyBaseModel.cs b/SolarPower/Models/MyBaseModel.cs index cd83144..1eda4e8 100644 --- a/SolarPower/Models/MyBaseModel.cs +++ b/SolarPower/Models/MyBaseModel.cs @@ -34,6 +34,12 @@ namespace SolarPower.Models public class MyUser { public int Id { get; set; } //編號 + public byte Status { get; set; } //狀態 + public string Name { get; set; } //姓名 + public byte IsGod { get; set; } //神級使用者 + public int CompanyId { get; set; } //公司編號 + public int RoleId { get; set; } //角色編號 + public string Email { get; set; } public MyCompany Company { get; set; } //公司資訊 } @@ -42,6 +48,17 @@ namespace SolarPower.Models /// public class MyCompany { + public int Id { get; set; } + public byte Status { get; set; } //狀態 + public string Name { get; set; } //名稱 + public string Logo { get; set; } + } + //當前登入使用者的角色權限 + public class MyRole + { + public int Id { get; set; } + public string Name { get; set; } //名稱 + public List Auth { get; set; } //可操作頁面 } } diff --git a/SolarPower/Models/Role.cs b/SolarPower/Models/Role.cs index 52b12b1..e857cf4 100644 --- a/SolarPower/Models/Role.cs +++ b/SolarPower/Models/Role.cs @@ -14,6 +14,15 @@ namespace SolarPower.Models.Role public byte Layer { get; set; } } + /// + /// 使用者DataTable + /// + public class RoleDateTable : Role + { + public string CompanyName { get; set; } + public string CreatorName { get; set; } + } + /// /// 角色下拉式選單 /// @@ -22,4 +31,84 @@ namespace SolarPower.Models.Role public string Text { get; set; } public string Value { get; set; } } + + public class PostRoleFilter + { + public int SelectedCompanyId { get; set; } + public string Name { get; set; } + } + + public class PostRole + { + public int Id { get; set; } + public int SelectedCompanyId { get; set; } + public string Name { get; set; } + } + + public class UpdateRole : Updated + { + public int SelectedCompanyId { get; set; } + public string Name { get; set; } + } + + /// + /// 賦予公司的權限池 + /// + public class CompanyAuthPage + { + public int Id { get; set; } + public int CompanyId { get; set; } + public string AuthCode { get; set; } + public string AuthPageMainName { get; set; } + public string AuthPageSubName { get; set; } + } + + /// + /// 角色權限DataTable + /// + public class RoleAuth: Created + { + public int Id { get; set; } + public string AuthCode { get; set; } + } + + /// + /// 角色權限DataTable + /// + public class RoleAuthDataTable : RoleAuth + { + public string CompanyName { get; set; } //公司名稱 + public string RoleName { get; set; } //角色名稱 + public string AuthPageSubName { get; set; } //權限功能名稱 + public string CreatorName { get; set; } //建立者名稱 + } + + /// + /// 角色權限頁面搜尋條件 + /// + public class PostRoleAuthFilter + { + public int SelectedCompanyId { get; set; } + public int SelectedRoleId { get; set; } + } + + public class AuthPage + { + public string AuthCode { get; set; } + public string MainName { get; set; } + public string SubName { get; set; } + } + + public class PostRoleAuth + { + public int SelectedRoleId { get; set; } + + public List CheckAuths { get; set; } + } + + public class PostDeleteRoleAuth + { + public int RoleId { get; set; } + public string AuthCode { get; set; } + } } diff --git a/SolarPower/Models/User.cs b/SolarPower/Models/User.cs index 2bbd788..5bfba2b 100644 --- a/SolarPower/Models/User.cs +++ b/SolarPower/Models/User.cs @@ -36,6 +36,7 @@ namespace SolarPower.Models.User public string Password { get; set; } //密碼 public string Email { get; set; } //信箱 public string Phone { get; set; } //手機 + public int RoleId { get; set; } //角色編號 public string Tel { get; set; } //市話 } @@ -62,6 +63,7 @@ namespace SolarPower.Models.User public int CompanyId { get; set; } //公司編號 public string Email { get; set; } //信箱 public string Phone { get; set; } //手機 + public int RoleId { get; set; } //角色編號 } /// diff --git a/SolarPower/Repository/Implement/CompanyRepository.cs b/SolarPower/Repository/Implement/CompanyRepository.cs index fb456ef..14e9023 100644 --- a/SolarPower/Repository/Implement/CompanyRepository.cs +++ b/SolarPower/Repository/Implement/CompanyRepository.cs @@ -1,6 +1,8 @@ using Dapper; using SolarPower.Helper; +using SolarPower.Models; using SolarPower.Models.Company; +using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; @@ -43,13 +45,13 @@ namespace SolarPower.Repository.Implement } /// - /// 取得狀態為正常的公司基本資料 + /// 取得當前使用者所在的公司資訊 /// /// /// - public SimpleCompany GetOneNormalSimpleCompanyById(int id) + public MyCompany GetMyCompanyInfoById(int id) { - SimpleCompany result; + MyCompany result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); @@ -57,7 +59,7 @@ namespace SolarPower.Repository.Implement { var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0 AND Status = @Status AND Id = @Id"; - result = conn.QueryFirstOrDefault(sql, new { Status = CompanyStatusEnum.Normal, Id = id }); + result = conn.QueryFirstOrDefault(sql, new { Status = CompanyStatusEnum.Normal, Id = id }); } catch (Exception exception) { @@ -232,5 +234,64 @@ namespace SolarPower.Repository.Implement return result; } } + + /// + /// 透過公司編號,取得該公司的註冊人數 + /// + /// + /// + public async Task GetRegisterNumberByCompanyId(int companyId) + { + int result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + try + { + var sql = $"SELECT COUNT(*) FROM user WHERE Deleted = 0 AND Status = @Status AND CompanyId = @CompanyId"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Status = UserStatusEnum.Normal, CompanyId = companyId }); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } + + public async Task> GetCompanyAuthByCompanyId(int companyId) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + try + { + var sql = @$"SELECT + ap.*, + CASE WHEN cap_id.ComapnyId IS NOT NULL THEN 1 ELSE 0 END AS CheckAuth + FROM auth_page ap + LEFT JOIN (SELECT * FROM company_auth_page WHERE ComapnyId = @ComapnyId) + cap_id ON ap.AuthCode = cap_id.AuthCode + "; + + result = (await conn.QueryAsync(sql, new { CompanyId = companyId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } } } diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index c605248..1b0bb2c 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -227,6 +227,33 @@ namespace SolarPower.Repository.Implement return insertQuery.ToString(); } + /// + /// 產生Insert語句,可選擇自己要加入資料表 + /// + /// + /// 欲新增至目標資料表 + /// + protected string GenerateInsertQueryWithCustomTable(List properties, string table_name) + { + var insertQuery = new StringBuilder($"INSERT INTO {table_name} "); + + insertQuery.Append("("); + + properties.ForEach(prop => { insertQuery.Append($"{prop},"); }); + + insertQuery + .Remove(insertQuery.Length - 1, 1) + .Append(") VALUES ("); + + properties.ForEach(prop => { insertQuery.Append($"@{prop},"); }); + + insertQuery + .Remove(insertQuery.Length - 1, 1) + .Append(")"); + + return insertQuery.ToString(); + } + /// /// 產生Update語句 /// diff --git a/SolarPower/Repository/Implement/RoleRepository.cs b/SolarPower/Repository/Implement/RoleRepository.cs index 50e0394..f29fb83 100644 --- a/SolarPower/Repository/Implement/RoleRepository.cs +++ b/SolarPower/Repository/Implement/RoleRepository.cs @@ -14,9 +14,14 @@ namespace SolarPower.Repository.Implement { public RoleRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { - tableName = "Role"; + tableName = "role"; } + /// + /// 取得下拉式公司角色選單,須為Deleted: 0 + /// + /// + /// public async Task> GetRoleSelectOptionListAsync(int companyId) { List result; @@ -35,5 +40,265 @@ namespace SolarPower.Repository.Implement return result; } } + + /// + /// 取得單一公司角色,須為Deleted: 0 + /// + /// + /// + public async Task GetOneRoleAsync(int id) + { + Role result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + try + { + var sql = $"SELECT * FROM {tableName} WHERE Deleted = 0 AND Id = @Id"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { Id = id }); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } + + /// + /// 透過搜尋條件,查詢過濾後的使用者 + /// + /// + /// + public async Task> GetAllByFilterAsync(PostRoleFilter filter) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT + r.*, + c.Name AS CompanyName, + u.Name AS CreatorName + FROM {tableName} r + LEFT JOIN company c ON r.CompanyId = c.Id + LEFT JOIN user u ON r.CreatedBy = u.Id + WHERE r.Deleted = 0 + AND c.Deleted = 0 + AND r.CompanyId = @SelectedCompanyId"; + + if (!string.IsNullOrEmpty(filter.Name)) + { + sql += @" AND Name LIKE CONCAT('%', @Name, '%')"; + } + + result = (await conn.QueryAsync(sql, filter)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 修改角色資料 + /// + /// + /// + public async Task UpdateRoleAsync(UpdateRole entity, List properties) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = GenerateUpdateQuery(properties); + + await conn.ExecuteAsync(sql, entity, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + /// + /// 透過角色編號,取得所有權限功能 + /// + /// + /// + public async Task> GetAllAuthByRoleIdAsync(int roleId) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT + ra.*, + r.Name AS RoleName, + c.Name AS CompanyName, + u.Name AS CreatorName, + ap.SubName AS AuthPageSubName + FROM role_auth ra + LEFT JOIN role r ON ra.Id = r.Id + LEFT JOIN auth_page ap ON ra.AuthCode = ap.AuthCode + LEFT JOIN user u ON ra.CreatedBy = u.Id + LEFT JOIN company c ON r.CompanyId = c.Id + WHERE r.Deleted = 0 + AND c.Deleted = 0 + AND r.Id = @SelectedRoleId"; + + result = (await conn.QueryAsync(sql, new { SelectedRoleId = roleId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 透過公司編號,取得被賦予的權限池 + /// + /// + /// + public async Task> GetAllCompanyAuthPageAsync(int companyId) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT + cap.*, + ap.MainName AS AuthPageMainName, + ap.SubName AS AuthPageSubName + FROM company_auth_page cap + LEFT JOIN auth_page ap ON cap.AuthCode = ap.AuthCode + WHERE cap.CompanyId = @CompanyId"; + + result = (await conn.QueryAsync(sql, new { CompanyId = companyId })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 查詢公司權限池裡面該角色尚未擁有的權限 + /// + /// + /// + public async Task> GetRoleNotAuthPageAsync(PostRoleAuthFilter post) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT ap2.AuthCode, ap2.MainName, ap2.SubName + FROM + ( + SELECT cap.ComapnyId, cap.AuthCode, ap.MainName, ap.SubName, ap.ControlName + FROM company_auth_page cap + LEFT JOIN auth_page ap ON cap.AuthCode = ap.AuthCode + WHERE cap.ComapnyId = @CompanyId + ) ap2 + LEFT JOIN role_auth ra ON ap2.AuthCode = ra.AuthCode AND ra.Id = @RoleId + WHERE ra.AuthCode IS NULL + "; + + result = (await conn.QueryAsync(sql, new { CompanyId = post.SelectedCompanyId, RoleId = post.SelectedRoleId})).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + /// + /// 新增角色權限 + /// + /// + /// + /// + public async Task AddRoleAuthAsync(List entity, List properties) + { + int count; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + string sql = $"INSERT INTO role_auth (Id, AuthCode, CreatedBy) VALUES (@Id, @AuthCode, @CreatedBy)"; + + count = await conn.ExecuteAsync(sql, entity); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return count; + } + } + + public async Task PurgeOneRoleAuthAsync(int roleId, string authCode) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = $"DELETE FROM role_auth WHERE Id = @RoleId AND AuthCode = @AuthCode"; + + await conn.ExecuteAsync(sql, new { RoleId = roleId, AuthCode = authCode }, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } } } diff --git a/SolarPower/Repository/Implement/UserRepository.cs b/SolarPower/Repository/Implement/UserRepository.cs index aa02bd1..0761ef2 100644 --- a/SolarPower/Repository/Implement/UserRepository.cs +++ b/SolarPower/Repository/Implement/UserRepository.cs @@ -1,5 +1,6 @@ using Dapper; using SolarPower.Helper; +using SolarPower.Models; using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; @@ -109,9 +110,9 @@ namespace SolarPower.Repository.Implement /// /// /// - public SimpleUser GetOneNormalSimpleUserByAccount(string account) + public MyUser GetMyUserInfoByAccount(string account) { - SimpleUser result; + MyUser result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { conn.Open(); @@ -119,7 +120,7 @@ namespace SolarPower.Repository.Implement { var sql = $"SELECT * FROM {tableName} WHERE deleted = 0 AND status = @Status AND account = @Account"; - result = conn.QueryFirstOrDefault(sql, new { Status = UserStatusEnum.Normal, Account = account }); + result = conn.QueryFirstOrDefault(sql, new { Status = UserStatusEnum.Normal, Account = account }); } catch (Exception exception) { @@ -254,7 +255,7 @@ namespace SolarPower.Repository.Implement if (filter.SelectedCompanyId > 0) { - sql += @" AND CompanyId = @SelectedCompanyId"; + sql += @" AND u.CompanyId = @SelectedCompanyId"; } if (!string.IsNullOrEmpty(filter.Name)) @@ -264,7 +265,7 @@ namespace SolarPower.Repository.Implement if (filter.SelectedRoleId > 0) { - sql += @" AND RoleId = @SelectedRoleId"; + sql += @" AND u.RoleId = @SelectedRoleId"; } result = (await conn.QueryAsync(sql, filter)).ToList(); diff --git a/SolarPower/Repository/Interface/ICompanyRepository.cs b/SolarPower/Repository/Interface/ICompanyRepository.cs index c9ec669..496c984 100644 --- a/SolarPower/Repository/Interface/ICompanyRepository.cs +++ b/SolarPower/Repository/Interface/ICompanyRepository.cs @@ -1,4 +1,5 @@ -using SolarPower.Models.Company; +using SolarPower.Models; +using SolarPower.Models.Company; using System; using System.Collections.Generic; using System.Linq; @@ -10,11 +11,11 @@ namespace SolarPower.Repository.Interface { /// - /// 取得狀態為正常的公司 + /// 取得當前使用者所在的公司資訊 /// /// /// - SimpleCompany GetOneNormalSimpleCompanyById(int id); + MyCompany GetMyCompanyInfoById(int id); /// /// 修改公司資料 @@ -58,5 +59,19 @@ namespace SolarPower.Repository.Interface /// /// Task GetOneNormalSimpleCompanyByTaxIDNumber(string taxIDNumber); + + /// + /// 透過公司編號,取得該公司的註冊人數 + /// + /// + /// + Task GetRegisterNumberByCompanyId(int companyId); + + /// + /// 透過公司編號,取得該公司的權限池 + /// + /// + /// + Task> GetCompanyAuthByCompanyId(int companyId); } } diff --git a/SolarPower/Repository/Interface/IRepositoryBase.cs b/SolarPower/Repository/Interface/IRepositoryBase.cs index d2fbd33..7a194ea 100644 --- a/SolarPower/Repository/Interface/IRepositoryBase.cs +++ b/SolarPower/Repository/Interface/IRepositoryBase.cs @@ -42,6 +42,7 @@ namespace SolarPower.Repository.Interface /// /// Task DeleteOne(int id); + /// /// 透過Id,實際刪除單一筆資料 /// diff --git a/SolarPower/Repository/Interface/IRoleRepository.cs b/SolarPower/Repository/Interface/IRoleRepository.cs index f84cf28..95e5fd6 100644 --- a/SolarPower/Repository/Interface/IRoleRepository.cs +++ b/SolarPower/Repository/Interface/IRoleRepository.cs @@ -14,5 +14,60 @@ namespace SolarPower.Repository.Interface /// /// Task> GetRoleSelectOptionListAsync(int companyId); + + /// + /// 取得單一公司角色,須為Deleted: 0 + /// + /// + /// + Task GetOneRoleAsync(int id); + + /// + /// 透過搜尋條件,查詢過濾後的角色 + /// + /// + /// + Task> GetAllByFilterAsync(PostRoleFilter filter); + + /// + /// 更新公司角色名稱 + /// + /// + /// + Task UpdateRoleAsync(UpdateRole update, List properties); + + /// + /// 透過角色編號,取得所有權限功能 + /// + /// + /// + Task> GetAllAuthByRoleIdAsync(int roleId); + + /// + /// 透過公司編號,取得被賦予的權限池 + /// + /// + /// + Task> GetAllCompanyAuthPageAsync(int companyId); + + /// + /// 查詢公司權限池裡面該角色尚未擁有的權限 + /// + /// + /// + Task> GetRoleNotAuthPageAsync(PostRoleAuthFilter post); + + /// + /// 新增角色權限 + /// + /// + /// + Task AddRoleAuthAsync(List entity, List properties); + + /// + /// 實際刪除角色權限 + /// + /// + Task PurgeOneRoleAuthAsync(int roleId, string authCode); } } diff --git a/SolarPower/Repository/Interface/IUserRepository.cs b/SolarPower/Repository/Interface/IUserRepository.cs index 5242c8f..b44ef2f 100644 --- a/SolarPower/Repository/Interface/IUserRepository.cs +++ b/SolarPower/Repository/Interface/IUserRepository.cs @@ -1,4 +1,5 @@ -using SolarPower.Models.User; +using SolarPower.Models; +using SolarPower.Models.User; using System; using System.Collections.Generic; using System.Linq; @@ -31,11 +32,11 @@ namespace SolarPower.Repository.Interface Task ChangePassword(string password, int id); /// - /// 透過Account,取得狀態為正常的使用者 + /// 透過Account,取得當前登入使用者資訊 /// /// /// - SimpleUser GetOneNormalSimpleUserByAccount(string account); + MyUser GetMyUserInfoByAccount(string account); /// /// 取得單一使用者 diff --git a/SolarPower/SolarPower.csproj b/SolarPower/SolarPower.csproj index ccb923a..86fdf2c 100644 --- a/SolarPower/SolarPower.csproj +++ b/SolarPower/SolarPower.csproj @@ -16,6 +16,7 @@ + diff --git a/SolarPower/Views/Company/Index.cshtml b/SolarPower/Views/Company/Index.cshtml index b73f98d..4fc5edd 100644 --- a/SolarPower/Views/Company/Index.cshtml +++ b/SolarPower/Views/Company/Index.cshtml @@ -132,7 +132,7 @@ - + + @section Scripts{ } \ No newline at end of file diff --git a/SolarPower/Views/Shared/_Layout.cshtml b/SolarPower/Views/Shared/_Layout.cshtml index ea39000..d6a111c 100644 --- a/SolarPower/Views/Shared/_Layout.cshtml +++ b/SolarPower/Views/Shared/_Layout.cshtml @@ -906,7 +906,7 @@