12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 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 =>
- {
- //todo 加上全局配置,对未开启模块过滤
- var systemAuth = _systemAuthorityRepository.GetAsync(d => d.RoleCode == role).GetAwaiter()
- .GetResult();
- return systemAuth?.GetPermissions() ?? new();
- }
- );
- }
- public void RemovePermissions(string role)
- {
- _cache.Remove(GetKey(role));
- }
- private string GetKey(string role) =>
- $"{RolePermissionCacheKey}{role}";
- }
- }
|