-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
backend-src/UZonMailService/Models/SQL/Permission/License.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using UZonMailService.Models.SQL.Base; | ||
|
||
namespace UZonMailService.Models.SQL.Permission | ||
{ | ||
/// <summary> | ||
/// 授权验证 | ||
/// </summary> | ||
public class License : SqlId | ||
{ | ||
|
||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
backend-src/UZonMailService/Services/License/LicenseManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Uamazing.Utils.Web.Service; | ||
|
||
namespace UZonMailService.Services.License | ||
{ | ||
/// <summary> | ||
/// 授权管理 | ||
/// </summary> | ||
public class LicenseManager(IServiceScopeFactory ssf) : ISingletonService | ||
{ | ||
/// <summary> | ||
/// 上一次授权更新日期 | ||
/// </summary> | ||
private DateTime _lastUpdateDate; | ||
|
||
/// <summary> | ||
/// 更新间隔 | ||
/// </summary> | ||
private double _updateIntervalHours = 24; | ||
|
||
/// <summary> | ||
/// 获取授权类型 | ||
/// </summary> | ||
/// <returns></returns> | ||
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; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend-src/UZonMailService/Services/License/LicenseType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace UZonMailService.Services.License | ||
{ | ||
/// <summary> | ||
/// 授权类型 | ||
/// </summary> | ||
public enum LicenseType | ||
{ | ||
/// <summary> | ||
/// 社区版本,免费 | ||
/// </summary> | ||
Community = 1 << 0, | ||
|
||
/// <summary> | ||
/// 专业版 | ||
/// </summary> | ||
Professional = 1 << 1, | ||
|
||
/// <summary> | ||
/// 企业版 | ||
/// </summary> | ||
Enterprise = 1 << 2, | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
backend-src/UZonMailService/Services/Permission/PermissionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// 权限服务 | ||
/// </summary> | ||
public class PermissionService(SqlContext db, CacheService cache, IHubContext<UzonMailHub, IUzonMailClient> hub) : IScopedService | ||
{ | ||
/// <summary> | ||
/// 更新用户的权限缓存 | ||
/// </summary> | ||
/// <param name="userIds"></param> | ||
/// <returns>返回权限码</returns> | ||
public async Task<Dictionary<long,List<string>>> UpdateUserPermissionsCache(List<long> 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<long, List<string>> 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; | ||
} | ||
|
||
/// <summary> | ||
/// 通知用户权限更新 | ||
/// </summary> | ||
/// <param name="userPermissionCodes"></param> | ||
/// <returns></returns> | ||
public async Task NotifyPermissionUpdate(Dictionary<long,List<string>> userPermissionCodes) | ||
{ | ||
if(userPermissionCodes.Count == 0) return; | ||
|
||
foreach (var item in userPermissionCodes) | ||
{ | ||
await hub.GetUserClient(item.Key).PermissionUpdated(item.Value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using UZonMailService.SignalRHubs.Notify; | ||
using UZonMailService.SignalRHubs.Permission; | ||
using UZonMailService.SignalRHubs.SendEmail; | ||
|
||
namespace UZonMailService.SignalRHubs | ||
{ | ||
/// <summary> | ||
/// 客户端的方法 | ||
/// </summary> | ||
public interface IUzonMailClient: ISendEmailClient, INotifyClient | ||
public interface IUzonMailClient: ISendEmailClient, INotifyClient,IPermissionClient | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend-src/UZonMailService/SignalRHubs/Permission/IPermissionClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace UZonMailService.SignalRHubs.Permission | ||
{ | ||
public interface IPermissionClient | ||
{ | ||
/// <summary> | ||
/// 通知权限更新 | ||
/// </summary> | ||
/// <param name="permissions"></param> | ||
/// <returns></returns> | ||
Task PermissionUpdated(List<string> permissions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters