Bläddra i källkod

RolePermissionCache

xf 2 år sedan
förälder
incheckning
8784905f0d

+ 4 - 0
src/Hotline.Api/Controllers/RoleController.cs

@@ -27,6 +27,7 @@ public class RoleController : BaseController
     private readonly ITableAccessLevelRepository _tableAccessLevelRepository;
     private readonly IMapper _mapper;
     private readonly ITableAccessLevelCacheManager _tableAccessLevelCacheManager;
+    private readonly IRolePermissionsCacheManager _rolePermissionsCacheManager;
     private readonly IOptions<IdentityConfiguration> _identityConfigurationAccessor;
     private readonly ISystemDataTableRepository _systemDataTableRepository;
 
@@ -36,6 +37,7 @@ public class RoleController : BaseController
         ISystemDataAuthorityRepository systemDataAuthorityRepository,
         ITableAccessLevelRepository tableAccessLevelRepository,
         ITableAccessLevelCacheManager tableAccessLevelCacheManager,
+        IRolePermissionsCacheManager rolePermissionsCacheManager,
         IMapper mapper,
         IOptions<IdentityConfiguration> identityConfigurationAccessor,
         ISystemDataTableRepository systemDataTableRepository)
@@ -45,6 +47,7 @@ public class RoleController : BaseController
         _systemDataAuthorityRepository = systemDataAuthorityRepository;
         _tableAccessLevelRepository = tableAccessLevelRepository;
         _tableAccessLevelCacheManager = tableAccessLevelCacheManager;
+        _rolePermissionsCacheManager = rolePermissionsCacheManager;
         _mapper = mapper;
         _identityConfigurationAccessor = identityConfigurationAccessor;
         _systemDataTableRepository = systemDataTableRepository;
@@ -150,6 +153,7 @@ public class RoleController : BaseController
             _mapper.Map(dto, model);
             await _systemAuthorityRepository.UpdateAsync(model);
         }
+        _rolePermissionsCacheManager.RemovePermissions(dto.RoleCode);
     }
 
     /// <summary>

+ 12 - 5
src/Hotline.Api/Permissions/IPermissionManager.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Hotline.Caches;
 using XF.Domain.Dependency;
 
 namespace Hotline.Permissions
@@ -15,19 +16,25 @@ namespace Hotline.Permissions
 
     public class PermissionManager : IPermissionManager, ISingletonDependency
     {
-        private readonly ISystemAuthorityRepository _systemauthorityRepository;
+        private readonly IServiceScopeFactory _serviceScopeFactory;
 
-        public PermissionManager(IServiceScopeFactory factory)
+        public PermissionManager(IServiceScopeFactory serviceScopeFactory)
         {
-            _systemauthorityRepository = factory.CreateScope().ServiceProvider.GetRequiredService<ISystemAuthorityRepository>();
+            _serviceScopeFactory = serviceScopeFactory;
+
+            //_systemauthorityRepository = factory.CreateScope().ServiceProvider.GetRequiredService<ISystemAuthorityRepository>();
         }
 
 
         public IReadOnlyList<string> RolesToPermissions(List<string> roles)
         {
-            var list = _systemauthorityRepository.GetPermission(roles);
+            using var scope = _serviceScopeFactory.CreateScope();
+            var cacheManager = scope.ServiceProvider.GetService<IRolePermissionsCacheManager>();
+            return roles.SelectMany(d => cacheManager.GetPermissions(d)).ToList();
+
+            //var list = _systemauthorityRepository.GetPermission(roles);
 
-            return list.Select(x => Enum.Parse(typeof(EPermission), x).ToString()).ToList();
+            //return list.Select(x => Enum.Parse(typeof(EPermission), x).ToString()).ToList();
         }
     }
 }

+ 45 - 0
src/Hotline.CacheManager/RolePermissionsCacheManager.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Hotline.Caches;
+using Hotline.Settings;
+using XF.Domain.Cache;
+using XF.Domain.Dependency;
+
+namespace Hotline.CacheManager
+{
+    public class RolePermissionsCacheManager : IRolePermissionsCacheManager, IScopeDependency
+    {
+        private const string RolePermissionCacheKey = "RolePermissionCache_";
+        private readonly ITypedCache<IReadOnlyList<string>> _cache;
+        private readonly ISystemAuthorityRepository _systemAuthorityRepository;
+
+        public RolePermissionsCacheManager(ITypedCache<IReadOnlyList<string>> cache, ISystemAuthorityRepository systemAuthorityRepository)
+        {
+            _cache = cache;
+            _systemAuthorityRepository = systemAuthorityRepository;
+        }
+
+        public IReadOnlyList<string> GetPermissions(string role)
+        {
+            return _cache.GetOrAdd(GetKey(role), d =>
+                {
+                    var systemAuth = _systemAuthorityRepository.GetAsync(d => d.RoleCode == role).GetAwaiter()
+                        .GetResult();
+                    return systemAuth.GetAllPermissions();
+                }
+            );
+        }
+
+        public void RemovePermissions(string role)
+        {
+            _cache.Remove(GetKey(role));
+        }
+
+
+        private string GetKey(string role) =>
+            $"{RolePermissionCacheKey}{role}";
+    }
+}

+ 15 - 0
src/Hotline/Caches/IRolePermissionsCacheManager.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hotline.Caches
+{
+    public interface IRolePermissionsCacheManager
+    {
+        IReadOnlyList<string> GetPermissions(string role);
+
+        void RemovePermissions(string role);
+    }
+}

+ 0 - 12
src/Hotline/IDeviceEventHandler.cs

@@ -1,12 +0,0 @@
-using Hotline.CallCenter.Devices;
-
-namespace Hotline
-{
-    /// <summary>
-    /// 处理设备事件
-    /// </summary>
-    public interface IDeviceEventHandler
-    {
-        Task HandleAsync(Stream eventStream, DeviceConfigs deviceConfigs, CancellationToken cancellationToken);
-    }
-}

+ 0 - 0
src/Hotline.Api/Permissions/EPermission.cs → src/Hotline/Permissions/EPermission.cs


+ 10 - 7
src/Hotline/Settings/SystemAuthority.cs

@@ -1,12 +1,13 @@
 using SqlSugar;
 using System.ComponentModel;
+using Hotline.Permissions;
 using XF.Domain.Entities;
 using XF.Domain.Repository;
 
 namespace Hotline.Settings
 {
     [Description("权限分配")]
-    public class SystemAuthority: CreationEntity
+    public class SystemAuthority : CreationEntity
     {
         /// <summary>
         /// 角色ID
@@ -21,19 +22,21 @@ namespace Hotline.Settings
         /// <summary>
         /// 菜单ID
         /// </summary>
-        [SugarColumn(ColumnDataType="varchar(3000)",IsJson = true)]
+        [SugarColumn(ColumnDataType = "varchar(3000)", IsJson = true)]
         public List<string> SystemMenuArr { get; set; }
 
         /// <summary>
         /// 功能点
         /// </summary>
-        [SugarColumn(ColumnDataType = "varchar(4000)",IsJson = true)]
+        [SugarColumn(ColumnDataType = "varchar(4000)", IsJson = true)]
         public List<string> SystemButtonArr { get; set; }
 
-        /// <summary>
-        /// 创建时间
-        /// </summary>
-        public DateTime CreationTime { get; set; }
+        public List<string> GetAllPermissions()
+        {
+            var permissions = new List<string>(SystemMenuArr);
+            permissions.AddRange(SystemButtonArr);
+            return permissions.Select(d=>Enum.Parse<EPermission>(d).ToString()).Distinct().ToList();
+        }
     }
 
     public class SystemMenuAuth