/
RoleStore.cs
95 lines (80 loc) · 3.09 KB
/
RoleStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Threading;
using System.Threading.Tasks;
using InfinniPlatform.DocumentStorage;
using Microsoft.AspNetCore.Identity;
namespace InfinniPlatform.Auth.DocumentStorage
{
/// <inheritdoc />
public class RoleStore<TRole> : IRoleStore<TRole> where TRole : AppUserRole
{
private readonly Lazy<ISystemDocumentStorage<TRole>> _roles;
/// <summary>
/// Initializes a new instance of <see cref="RoleStore{TRole}" />.
/// </summary>
/// <param name="documentStorageFactory">Roles database storage.</param>
public RoleStore(ISystemDocumentStorageFactory documentStorageFactory)
{
_roles = new Lazy<ISystemDocumentStorage<TRole>>(() => documentStorageFactory.GetStorage<TRole>());
}
/// <inheritdoc />
public virtual void Dispose()
{
}
/// <inheritdoc />
public virtual async Task<IdentityResult> CreateAsync(TRole role, CancellationToken token)
{
await _roles.Value.InsertOneAsync(role);
return IdentityResult.Success;
}
/// <inheritdoc />
public virtual async Task<IdentityResult> UpdateAsync(TRole role, CancellationToken token)
{
await _roles.Value.ReplaceOneAsync(role, r => r.Id == role.Id);
return IdentityResult.Success;
}
/// <inheritdoc />
public virtual async Task<IdentityResult> DeleteAsync(TRole role, CancellationToken token)
{
await _roles.Value.DeleteOneAsync(r => r.Id == role.Id);
return IdentityResult.Success;
}
/// <inheritdoc />
public virtual Task<string> GetRoleIdAsync(TRole role, CancellationToken token)
{
return Task.FromResult(role.Id);
}
/// <inheritdoc />
public virtual Task<string> GetRoleNameAsync(TRole role, CancellationToken token)
{
return Task.FromResult(role.Name);
}
/// <inheritdoc />
public virtual Task SetRoleNameAsync(TRole role, string roleName, CancellationToken token)
{
role.Name = roleName;
return Task.CompletedTask;
}
/// <inheritdoc />
public virtual Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken token)
{
return Task.FromResult(role.NormalizedName);
}
/// <inheritdoc />
public virtual Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken token)
{
role.NormalizedName = normalizedName;
return Task.CompletedTask;
}
/// <inheritdoc />
public virtual Task<TRole> FindByIdAsync(string roleId, CancellationToken token)
{
return _roles.Value.Find(r => r.Id == roleId).FirstOrDefaultAsync();
}
/// <inheritdoc />
public virtual Task<TRole> FindByNameAsync(string normalizedName, CancellationToken token)
{
return _roles.Value.Find(r => r.NormalizedName == normalizedName).FirstOrDefaultAsync();
}
}
}