From 44139b1c2d4e5a4fc36688aab359be4d70ff5801 Mon Sep 17 00:00:00 2001 From: "galens.gmx" <260827400@qq.com> Date: Tue, 23 Jul 2024 20:07:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Permission/RoleController.cs | 21 ++++++- .../Permission/UserRoleController.cs | 23 ++++++-- .../Models/SQL/Permission/License.cs | 12 ++++ .../Services/License/LicenseManager.cs | 44 ++++++++++++++ .../Services/License/LicenseType.cs | 23 ++++++++ .../Services/Permission/PermissionService.cs | 59 +++++++++++++++++++ .../SignalRHubs/IUzonMailClient.cs | 3 +- .../Permission/IPermissionClient.cs | 12 ++++ .../UZonMailService/UZonMailService.csproj | 2 +- .../Properties/AssemblyInfo.cs | 4 +- 10 files changed, 191 insertions(+), 12 deletions(-) create mode 100644 backend-src/UZonMailService/Models/SQL/Permission/License.cs create mode 100644 backend-src/UZonMailService/Services/License/LicenseManager.cs create mode 100644 backend-src/UZonMailService/Services/License/LicenseType.cs create mode 100644 backend-src/UZonMailService/Services/Permission/PermissionService.cs create mode 100644 backend-src/UZonMailService/SignalRHubs/Permission/IPermissionClient.cs diff --git a/backend-src/UZonMailService/Controllers/Permission/RoleController.cs b/backend-src/UZonMailService/Controllers/Permission/RoleController.cs index 50194de0..6d4bf284 100644 --- a/backend-src/UZonMailService/Controllers/Permission/RoleController.cs +++ b/backend-src/UZonMailService/Controllers/Permission/RoleController.cs @@ -6,6 +6,7 @@ using UZonMailService.Models.SQL; using UZonMailService.Models.SQL.Permission; using UZonMailService.Models.Validators; +using UZonMailService.Services.Permission; using UZonMailService.Services.Settings; using UZonMailService.Utils.ASPNETCore.PagingQuery; using UZonMailService.Utils.Database; @@ -13,7 +14,7 @@ namespace UZonMailService.Controllers.Permission { - public class RoleController(TokenService tokenService, SqlContext db) : PermissionControllerBase + public class RoleController(TokenService tokenService, SqlContext db, PermissionService permission) : PermissionControllerBase { /// /// 获取角色数量 @@ -123,12 +124,26 @@ public async Task> UpsertRole([FromBody] Role role) public async Task> DeleteRole(long roleId) { // 删除角色 + var role = await db.Roles.Where(x => x.Id == roleId) + .Include(x => x.UserRoles) + .Include(x=>x.PermissionCodes) + .FirstOrDefaultAsync(); + if (role == null) return true.ToSuccessResponse(); + var userRoles = role.UserRoles?.ToList() ?? []; // 删除用户与角色关联表 + role.UserRoles?.Clear(); + role.PermissionCodes?.Clear(); + // 删除角色 + db.Roles.Remove(role); + await db.SaveChangesAsync(); // 重新计算受影响的用户的权限 - - // 通知用户更新权限 + // 更新权限缓存 + var userIds = userRoles.Select(x => x.UserId).ToList(); + var permissionCodesDic = await permission.UpdateUserPermissionsCache(userIds); + // 通知权限更新 + await permission.NotifyPermissionUpdate(permissionCodesDic); return true.ToSuccessResponse(); } diff --git a/backend-src/UZonMailService/Controllers/Permission/UserRoleController.cs b/backend-src/UZonMailService/Controllers/Permission/UserRoleController.cs index c99a164c..2be03223 100644 --- a/backend-src/UZonMailService/Controllers/Permission/UserRoleController.cs +++ b/backend-src/UZonMailService/Controllers/Permission/UserRoleController.cs @@ -5,6 +5,7 @@ using UZonMailService.Models.SQL; using UZonMailService.Models.SQL.Permission; using UZonMailService.Models.Validators; +using UZonMailService.Services.Permission; using UZonMailService.Services.Settings; using UZonMailService.Utils.ASPNETCore.PagingQuery; using UZonMailService.Utils.Database; @@ -12,7 +13,7 @@ namespace UZonMailService.Controllers.Permission { - public class UserRoleController(SqlContext db) : PermissionControllerBase + public class UserRoleController(SqlContext db, PermissionService permission) : PermissionControllerBase { /// /// 创建或者更新角色 @@ -42,7 +43,7 @@ public async Task> UpsertUserRole([FromBody] UserRole u userRole = existOne; } else - { + { db.UserRoles.Add(userRole); } @@ -58,7 +59,7 @@ public async Task> UpsertUserRole([FromBody] UserRole u [HttpGet("filtered-count")] public async Task> GetRolesCount(string filter) { - var dbSet = db.UserRoles.AsNoTracking(); + var dbSet = db.UserRoles.AsNoTracking(); if (!string.IsNullOrEmpty(filter)) { dbSet = dbSet.Where(x => x.User.UserName.Contains(filter) || x.User.UserName.Contains(filter)); @@ -82,7 +83,7 @@ public async Task>> GetRolesData(string filter, [F } var results = await dbSet.Page(pagination) .Include(x => x.User) - .Include(x=>x.Roles) + .Include(x => x.Roles) .ToListAsync(); return results.ToSuccessResponse(); @@ -96,11 +97,23 @@ public async Task>> GetRolesData(string filter, [F [HttpDelete("{userRoleId:long}")] public async Task> DeleteUserRole(long userRoleId) { - // 先移除关联的角色 + // 先移除关联的角色 + var userRole = await db.UserRoles.Where(x => x.Id == userRoleId) + .Include(x => x.Roles) + .FirstOrDefaultAsync(); + if (userRole == null) return false.ToErrorResponse("未找到对应的用户角色"); + userRole.Roles.Clear(); // 删除本身 + db.UserRoles.Remove(userRole); + await db.SaveChangesAsync(); + // 更新权限缓存 + var permissionCodesDic = await permission.UpdateUserPermissionsCache([userRole.UserId]); // 通知权限更新 + await permission.NotifyPermissionUpdate(permissionCodesDic); + + // 返回结果 return true.ToSuccessResponse(); } } diff --git a/backend-src/UZonMailService/Models/SQL/Permission/License.cs b/backend-src/UZonMailService/Models/SQL/Permission/License.cs new file mode 100644 index 00000000..5c89daef --- /dev/null +++ b/backend-src/UZonMailService/Models/SQL/Permission/License.cs @@ -0,0 +1,12 @@ +using UZonMailService.Models.SQL.Base; + +namespace UZonMailService.Models.SQL.Permission +{ + /// + /// 授权验证 + /// + public class License : SqlId + { + + } +} diff --git a/backend-src/UZonMailService/Services/License/LicenseManager.cs b/backend-src/UZonMailService/Services/License/LicenseManager.cs new file mode 100644 index 00000000..14c908d6 --- /dev/null +++ b/backend-src/UZonMailService/Services/License/LicenseManager.cs @@ -0,0 +1,44 @@ +using Uamazing.Utils.Web.Service; + +namespace UZonMailService.Services.License +{ + /// + /// 授权管理 + /// + public class LicenseManager(IServiceScopeFactory ssf) : ISingletonService + { + /// + /// 上一次授权更新日期 + /// + private DateTime _lastUpdateDate; + + /// + /// 更新间隔 + /// + private double _updateIntervalHours = 24; + + /// + /// 获取授权类型 + /// + /// + public LicenseType GetLicenseType() + { + // 判断是否需要更新 + var timespan = DateTime.Now - _lastUpdateDate; + if (timespan.TotalHours > _updateIntervalHours) + { + // 更新授权 + UpdateLicense(); + } + + return _licenseType; + } + + private LicenseType _licenseType = LicenseType.Community; + private void UpdateLicense() + { + // 从数据库读取授权文件 + _licenseType = LicenseType.Community; + } + } +} diff --git a/backend-src/UZonMailService/Services/License/LicenseType.cs b/backend-src/UZonMailService/Services/License/LicenseType.cs new file mode 100644 index 00000000..ebbf5d80 --- /dev/null +++ b/backend-src/UZonMailService/Services/License/LicenseType.cs @@ -0,0 +1,23 @@ +namespace UZonMailService.Services.License +{ + /// + /// 授权类型 + /// + public enum LicenseType + { + /// + /// 社区版本,免费 + /// + Community = 1 << 0, + + /// + /// 专业版 + /// + Professional = 1 << 1, + + /// + /// 企业版 + /// + Enterprise = 1 << 2, + } +} diff --git a/backend-src/UZonMailService/Services/Permission/PermissionService.cs b/backend-src/UZonMailService/Services/Permission/PermissionService.cs new file mode 100644 index 00000000..9d6b8d1f --- /dev/null +++ b/backend-src/UZonMailService/Services/Permission/PermissionService.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; +using Uamazing.Utils.Web.Service; +using UZonMailService.Cache; +using UZonMailService.Models.SQL; +using UZonMailService.Models.SQL.MultiTenant; +using UZonMailService.SignalRHubs; +using UZonMailService.SignalRHubs.Extensions; + +namespace UZonMailService.Services.Permission +{ + /// + /// 权限服务 + /// + public class PermissionService(SqlContext db, CacheService cache, IHubContext hub) : IScopedService + { + /// + /// 更新用户的权限缓存 + /// + /// + /// 返回权限码 + public async Task>> UpdateUserPermissionsCache(List userIds) + { + if (userIds.Count == 0) return []; + + var userRoles = await db.UserRoles.AsNoTracking() + .Where(x => userIds.Contains(x.UserId)) + .Include(x => x.Roles) + .ThenInclude(x => x.PermissionCodes) + .GroupBy(x=>x.UserId) + .ToListAsync(); + + Dictionary> results = []; + foreach (var item in userRoles) + { + var permissionCodes = item.SelectMany(x=>x.Roles).SelectMany(x=>x.PermissionCodes).Select(x => x.Code).Distinct().ToList(); + results.Add(item.Key, permissionCodes); + // 更新缓存 + await cache.SetAsync($"permissions/{item.Key}", permissionCodes); + } + return results; + } + + /// + /// 通知用户权限更新 + /// + /// + /// + public async Task NotifyPermissionUpdate(Dictionary> userPermissionCodes) + { + if(userPermissionCodes.Count == 0) return; + + foreach (var item in userPermissionCodes) + { + await hub.GetUserClient(item.Key).PermissionUpdated(item.Value); + } + } + } +} diff --git a/backend-src/UZonMailService/SignalRHubs/IUzonMailClient.cs b/backend-src/UZonMailService/SignalRHubs/IUzonMailClient.cs index fd3f8020..ad51b423 100644 --- a/backend-src/UZonMailService/SignalRHubs/IUzonMailClient.cs +++ b/backend-src/UZonMailService/SignalRHubs/IUzonMailClient.cs @@ -1,4 +1,5 @@ using UZonMailService.SignalRHubs.Notify; +using UZonMailService.SignalRHubs.Permission; using UZonMailService.SignalRHubs.SendEmail; namespace UZonMailService.SignalRHubs @@ -6,7 +7,7 @@ namespace UZonMailService.SignalRHubs /// /// 客户端的方法 /// - public interface IUzonMailClient: ISendEmailClient, INotifyClient + public interface IUzonMailClient: ISendEmailClient, INotifyClient,IPermissionClient { } } diff --git a/backend-src/UZonMailService/SignalRHubs/Permission/IPermissionClient.cs b/backend-src/UZonMailService/SignalRHubs/Permission/IPermissionClient.cs new file mode 100644 index 00000000..e01ab982 --- /dev/null +++ b/backend-src/UZonMailService/SignalRHubs/Permission/IPermissionClient.cs @@ -0,0 +1,12 @@ +namespace UZonMailService.SignalRHubs.Permission +{ + public interface IPermissionClient + { + /// + /// 通知权限更新 + /// + /// + /// + Task PermissionUpdated(List permissions); + } +} diff --git a/backend-src/UZonMailService/UZonMailService.csproj b/backend-src/UZonMailService/UZonMailService.csproj index 60e54a2a..1244aa82 100644 --- a/backend-src/UZonMailService/UZonMailService.csproj +++ b/backend-src/UZonMailService/UZonMailService.csproj @@ -9,7 +9,7 @@ false true x64 - 0.8.7.0 + 0.8.9.0 diff --git a/backend-src/UzonMailDesktop/Properties/AssemblyInfo.cs b/backend-src/UzonMailDesktop/Properties/AssemblyInfo.cs index 07e9ec7a..e2ab1e81 100644 --- a/backend-src/UzonMailDesktop/Properties/AssemblyInfo.cs +++ b/backend-src/UzonMailDesktop/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.8.7.0")] -[assembly: AssemblyFileVersion("0.8.7.0")] +[assembly: AssemblyVersion("0.8.9.0")] +[assembly: AssemblyFileVersion("0.8.9.0")]