11using Microsoft . AspNetCore . Http ;
22using Microsoft . Extensions . Caching . Distributed ;
3+ using Microsoft . Extensions . Hosting ;
4+
5+ using StackExchange . Redis ;
6+ using System . Data ;
7+ using System . Diagnostics ;
8+ using System . Security ;
39using System . Security . Claims ;
10+ using System . Text . Json ;
11+ using UserManagementApi . Contracts . Models ;
412
513namespace SharedLibrary . Cache
614{
715 public sealed class CacheAccessProvider : ICacheAccessProvider
816 {
17+ private readonly IDatabase _db ;
918 private readonly IDistributedCache _cache ;
1019 private readonly IHttpContextAccessor _http ;
20+ private readonly string _envPrefix ;
21+ public CacheAccessProvider ( IDatabase db , IDistributedCache cache , IHttpContextAccessor http , IHostEnvironment env ) => ( _db , _cache , _http , _envPrefix ) = ( db , cache , http ,
22+ string . IsNullOrWhiteSpace ( env . EnvironmentName ) ? "" : env . EnvironmentName + ":" ) ;
23+
24+ private string K ( string key ) => _envPrefix + key ;
1125
12- public CacheAccessProvider ( IDistributedCache cache , IHttpContextAccessor http )
26+ public async Task < long > InvalidatePermissionsForRoleAsync ( string roleName , CancellationToken ct = default )
1327 {
14- _cache = cache ;
15- _http = http ;
28+
29+ var members = await _db . SetMembersAsync ( K ( $ "auth:roleusers:{ roleName } ") ) ;
30+ if ( members . Length == 0 )
31+ return 0 ;
32+
33+ var keys = members
34+ . Select ( m => ( RedisKey ) K ( $ "auth:permissions:{ ( int ) m } ") )
35+ . ToArray ( ) ;
36+
37+ return await _db . KeyDeleteAsync ( keys ) ;
1638 }
1739
1840 public async Task < string ? > GetAccessTokenAsync ( CancellationToken ct = default )
@@ -26,8 +48,7 @@ public CacheAccessProvider(IDistributedCache cache, IHttpContextAccessor http)
2648
2749 var key = $ "auth:token:{ uid } ";
2850 var token = await _cache . GetStringAsync ( key , ct ) ;
29- return token ;
30-
51+ return token ;
3152 }
3253
3354 public async Task < string ? > GetUserPermissionsAsync ( CancellationToken ct = default )
@@ -39,9 +60,9 @@ public CacheAccessProvider(IDistributedCache cache, IHttpContextAccessor http)
3960
4061 if ( string . IsNullOrEmpty ( uid ) ) return null ;
4162
42- var key = $ "auth:permissions:{ uid } ";
43- var permissions = await _cache . GetStringAsync ( key , ct ) ;
44- return permissions ;
63+ var dataKey = K ( $ "auth:permissions:{ uid } ") ;
64+ return await _db . StringGetAsync ( dataKey ) ;
65+
4566 }
4667
4768 // Optional helper method to set the token into cache
@@ -60,7 +81,7 @@ public async Task SetAccessToken(string token, int userId, DateTime expiresAtUtc
6081 await _cache . SetStringAsync ( key , token , options , ct ) ;
6182 }
6283
63- public async Task SetUserPermissions ( string permissions , int userId , DateTime expiresAtUtc , CancellationToken ct = default )
84+ public async Task < bool > SetUserInRoleSet ( string roleName , int userId , DateTime expiresAtUtc , CancellationToken ct = default )
6485 {
6586 var ttl = ToTtl ( expiresAtUtc ) ;
6687
@@ -70,15 +91,26 @@ public async Task SetUserPermissions(string permissions, int userId, DateTime ex
7091 } ;
7192
7293 // IMPORTANT: Do not manually prefix here if using InstanceName in startup.
73- var key = $ "auth:permissions: { userId } " ;
94+ var usersForRoleKey = K ( $ "auth:roleusers: { roleName } " ) ;
7495
75- await _cache . SetStringAsync ( key , permissions , options , ct ) ;
96+ bool added = await _db . SetAddAsync ( usersForRoleKey , userId ) ;
97+ return added ;
7698 }
7799
100+ public async Task SetUserPermissions ( string permissions , int userId , DateTime expiresAtUtc , CancellationToken ct = default )
101+ {
102+ var ttl = ToTtl ( expiresAtUtc ) ;
103+
104+ // IMPORTANT: Do not manually prefix here if using InstanceName in startup.
105+ var key = K ( $ "auth:permissions:{ userId } ") ;
106+ await _db . StringSetAsync ( key , permissions , ttl ) ;
107+
108+ }
109+
110+ //User Logout
78111 public Task RemoveAsync ( string userId , CancellationToken ct = default )
79112 {
80- _cache . Remove ( $ "auth:token:{ userId } ") ;
81- _cache . Remove ( $ "auth:permissions:{ userId } ") ;
113+ _cache . Remove ( $ "auth:token:{ userId } ") ;
82114 return Task . CompletedTask ;
83115 }
84116
0 commit comments