Skip to content

Commit 5243c97

Browse files
Invalidate permssions in cache and set permissions added
1 parent 4de7cd6 commit 5243c97

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

SharedLibrary/Cache/CacheAccessProvider.cs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
using Microsoft.AspNetCore.Http;
22
using 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;
39
using System.Security.Claims;
10+
using System.Text.Json;
11+
using UserManagementApi.Contracts.Models;
412

513
namespace 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

SharedLibrary/Cache/ICacheAccessProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ namespace SharedLibrary.Cache
99
public interface ICacheAccessProvider
1010
{
1111
Task<string?> GetAccessTokenAsync(CancellationToken ct = default);
12+
Task<long> InvalidatePermissionsForRoleAsync(string roleName, CancellationToken ct = default);
1213
Task<string?> GetUserPermissionsAsync(CancellationToken ct = default);
1314
Task SetAccessToken(string token, int userId, DateTime expiresAtUtc, CancellationToken ct = default);
14-
Task SetUserPermissions(string permissions, int userId, DateTime expiresAtUtc, CancellationToken ct = default);
15-
public Task RemoveAsync(string userId, CancellationToken ct = default);
15+
Task<bool> SetUserInRoleSet(string roleName, int userId, DateTime expiresAtUtc, CancellationToken ct = default);
16+
Task SetUserPermissions(string permissions, int userId, DateTime expiresAtUtc, CancellationToken ct = default);
17+
Task RemoveAsync(string userId, CancellationToken ct = default);
18+
1619
}
1720
}

0 commit comments

Comments
 (0)