-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RoleUpdater.cs
231 lines (190 loc) · 8.54 KB
/
RoleUpdater.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OrchardCore.Documents;
using OrchardCore.Environment.Extensions;
using OrchardCore.Environment.Extensions.Features;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Descriptor.Models;
using OrchardCore.Roles.Models;
using OrchardCore.Security;
using OrchardCore.Security.Permissions;
namespace OrchardCore.Roles.Services
{
public class RoleUpdater : FeatureEventHandler, IRoleCreatedEventHandler, IRoleRemovedEventHandler
{
private readonly ShellDescriptor _shellDescriptor;
private readonly IExtensionManager _extensionManager;
private readonly IDocumentManager<RolesDocument> _documentManager;
private readonly IEnumerable<IPermissionProvider> _permissionProviders;
private readonly ITypeFeatureProvider _typeFeatureProvider;
private readonly ILogger _logger;
private readonly HashSet<string> _installedFeatures = [];
public RoleUpdater(
ShellDescriptor shellDescriptor,
IExtensionManager extensionManager,
IDocumentManager<RolesDocument> documentManager,
IEnumerable<IPermissionProvider> permissionProviders,
ITypeFeatureProvider typeFeatureProvider,
ILogger<RoleUpdater> logger)
{
_shellDescriptor = shellDescriptor;
_extensionManager = extensionManager;
_documentManager = documentManager;
_permissionProviders = permissionProviders;
_typeFeatureProvider = typeFeatureProvider;
_logger = logger;
}
public override Task InstalledAsync(IFeatureInfo feature) => UpdateRolesForInstalledFeatureAsync(feature);
public override Task EnabledAsync(IFeatureInfo feature) => UpdateRolesForEnabledFeatureAsync(feature);
public Task RoleCreatedAsync(string roleName) => UpdateRoleForInstalledFeaturesAsync(roleName);
public Task RoleRemovedAsync(string roleName) => RemoveRoleForMissingFeaturesAsync(roleName);
private async Task UpdateRolesForInstalledFeatureAsync(IFeatureInfo feature)
{
_installedFeatures.Add(feature.Id);
var providers = _permissionProviders
.Where(provider => _typeFeatureProvider.GetFeatureForDependency(provider.GetType()).Id == feature.Id);
if (!providers.Any())
{
return;
}
var updated = false;
var rolesDocument = await _documentManager.GetOrCreateMutableAsync();
foreach (var provider in providers)
{
var stereotypes = provider.GetDefaultStereotypes();
foreach (var stereotype in stereotypes)
{
var role = rolesDocument.Roles.FirstOrDefault(role => string.Equals(role.RoleName, stereotype.Name, StringComparison.OrdinalIgnoreCase));
if (role == null)
{
continue;
}
var permissions = (stereotype.Permissions ?? [])
.Select(stereotype => stereotype.Name);
if (UpdateRole(role, permissions, _logger))
{
updated = true;
}
}
}
if (updated)
{
await _documentManager.UpdateAsync(rolesDocument);
}
}
private async Task UpdateRolesForEnabledFeatureAsync(IFeatureInfo feature)
{
if (_installedFeatures.Contains(feature.Id))
{
return;
}
var providers = _permissionProviders
.Where(provider => _typeFeatureProvider.GetFeatureForDependency(provider.GetType()).Id == feature.Id);
if (!providers.Any())
{
return;
}
var updated = false;
var rolesDocument = await _documentManager.GetOrCreateMutableAsync();
foreach (var role in rolesDocument.Roles)
{
if (!rolesDocument.MissingFeaturesByRole.TryGetValue(role.RoleName, out var missingFeatures) ||
!missingFeatures.Contains(feature.Id))
{
continue;
}
updated = true;
missingFeatures.Remove(feature.Id);
UpdateRolesForEnabledFeature(role, providers, _logger);
}
if (updated)
{
await _documentManager.UpdateAsync(rolesDocument);
}
}
private async Task UpdateRoleForInstalledFeaturesAsync(string roleName)
{
var rolesDocument = await _documentManager.GetOrCreateMutableAsync();
var role = rolesDocument.Roles.FirstOrDefault(role => string.Equals(role.RoleName, roleName, StringComparison.OrdinalIgnoreCase));
if (role == null)
{
return;
}
// Get installed features that are no more enabled.
var missingFeatures = _shellDescriptor.Installed
.Except(_shellDescriptor.Features)
.Select(feature => feature.Id)
.ToArray();
// And defining at least one 'IPermissionProvider'.
rolesDocument.MissingFeaturesByRole[roleName] = (await _extensionManager.LoadFeaturesAsync(missingFeatures))
.Where(entry => entry.ExportedTypes.Any(type => type.IsAssignableTo(typeof(IPermissionProvider))))
.Select(entry => entry.FeatureInfo.Id)
.ToList();
await _documentManager.UpdateAsync(rolesDocument);
var stereotypes = _permissionProviders
.SelectMany(provider => provider.GetDefaultStereotypes())
.Where(stereotype => string.Equals(stereotype.Name, roleName, StringComparison.OrdinalIgnoreCase));
if (!stereotypes.Any())
{
return;
}
var permissions = stereotypes
.SelectMany(stereotype => stereotype.Permissions ?? [])
.Select(stereotype => stereotype.Name);
UpdateRole(role, permissions, _logger);
}
private async Task RemoveRoleForMissingFeaturesAsync(string roleName)
{
var rolesDocument = await _documentManager.GetOrCreateMutableAsync();
if (rolesDocument.MissingFeaturesByRole.TryGetValue(roleName, out _))
{
rolesDocument.MissingFeaturesByRole.Remove(roleName);
await _documentManager.UpdateAsync(rolesDocument);
}
}
private static bool UpdateRolesForEnabledFeature(Role role, IEnumerable<IPermissionProvider> providers, ILogger logger)
{
var stereotypes = providers
.SelectMany(provider => provider.GetDefaultStereotypes())
.Where(stereotype => string.Equals(stereotype.Name, role.RoleName, StringComparison.OrdinalIgnoreCase));
if (!stereotypes.Any())
{
return false;
}
var permissions = stereotypes
.SelectMany(stereotype => stereotype.Permissions ?? [])
.Select(stereotype => stereotype.Name);
if (!permissions.Any())
{
return false;
}
return UpdateRole(role, permissions, logger);
}
private static bool UpdateRole(Role role, IEnumerable<string> permissions, ILogger logger)
{
var currentPermissions = role.RoleClaims
.Where(roleClaim => roleClaim.ClaimType == Permission.ClaimType)
.Select(roleClaim => roleClaim.ClaimValue);
var distinctPermissions = currentPermissions
.Union(permissions)
.Distinct();
var additionalPermissions = distinctPermissions.Except(currentPermissions);
if (!additionalPermissions.Any())
{
return false;
}
foreach (var permission in additionalPermissions)
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("Default role '{RoleName}' granted permission '{PermissionName}'.", role.RoleName, permission);
}
role.RoleClaims.Add(new RoleClaim { ClaimType = Permission.ClaimType, ClaimValue = permission });
}
return true;
}
}
}