mirror of https://github.com/abpframework/abp.git
5 changed files with 168 additions and 25 deletions
@ -0,0 +1,111 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement; |
||||
|
|
||||
|
public class DynamicPermissionDefinitionStoreCache : ISingletonDependency |
||||
|
{ |
||||
|
public string CacheStamp { get; set; } |
||||
|
|
||||
|
protected IDictionary<string, PermissionGroupDefinition> PermissionGroupDefinitions { get; } |
||||
|
|
||||
|
protected IDictionary<string, PermissionDefinition> PermissionDefinitions { get; } |
||||
|
protected ISimpleStateCheckerSerializer StateCheckerSerializer { get; } |
||||
|
|
||||
|
public DynamicPermissionDefinitionStoreCache(ISimpleStateCheckerSerializer stateCheckerSerializer) |
||||
|
{ |
||||
|
StateCheckerSerializer = stateCheckerSerializer; |
||||
|
PermissionGroupDefinitions = new Dictionary<string, PermissionGroupDefinition>(); |
||||
|
PermissionDefinitions = new Dictionary<string, PermissionDefinition>(); |
||||
|
} |
||||
|
|
||||
|
public async Task FillAsync( |
||||
|
List<PermissionGroupDefinitionRecord> permissionGroupRecords, |
||||
|
List<PermissionDefinitionRecord> permissionRecords) |
||||
|
{ |
||||
|
var context = new PermissionDefinitionContext(null); |
||||
|
|
||||
|
foreach (var permissionGroupRecord in permissionGroupRecords) |
||||
|
{ |
||||
|
var permissionGroup = context.AddGroup( |
||||
|
permissionGroupRecord.Name, |
||||
|
new FixedLocalizableString(permissionGroupRecord.DisplayName) |
||||
|
); |
||||
|
|
||||
|
foreach (var property in permissionGroupRecord.ExtraProperties) |
||||
|
{ |
||||
|
permissionGroup[property.Key] = property.Value; |
||||
|
} |
||||
|
|
||||
|
var permissionRecordsInThisGroup = permissionRecords.Where(p => p.GroupName == permissionGroup.Name); |
||||
|
foreach (var permissionRecord in permissionRecordsInThisGroup) |
||||
|
{ |
||||
|
var permission = permissionGroup.AddPermission( |
||||
|
permissionRecord.Name, |
||||
|
new FixedLocalizableString(permissionRecord.DisplayName), |
||||
|
permissionRecord.MultiTenancySide, |
||||
|
permissionRecord.IsEnabled |
||||
|
); |
||||
|
|
||||
|
if (!permissionRecord.Providers.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
permission.Providers.AddRange(permissionRecord.Providers.Split(',')); |
||||
|
} |
||||
|
|
||||
|
if (!permissionRecord.StateCheckers.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
var checkers = StateCheckerSerializer |
||||
|
.DeserializeArray<PermissionDefinition>( |
||||
|
permissionRecord.StateCheckers |
||||
|
); |
||||
|
permission.StateCheckers.AddRange(checkers); |
||||
|
} |
||||
|
|
||||
|
foreach (var property in permissionRecord.ExtraProperties) |
||||
|
{ |
||||
|
permission[property.Key] = property.Value; |
||||
|
} |
||||
|
|
||||
|
//TODO: Child permissions! (with parent name setting)
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual Dictionary<string, PermissionDefinition> CreatePermissionDefinitions() |
||||
|
{ |
||||
|
var permissions = new Dictionary<string, PermissionDefinition>(); |
||||
|
|
||||
|
foreach (var groupDefinition in PermissionGroupDefinitions.Values) |
||||
|
{ |
||||
|
foreach (var permission in groupDefinition.Permissions) |
||||
|
{ |
||||
|
AddPermissionToDictionaryRecursively(permissions, permission); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return permissions; |
||||
|
} |
||||
|
|
||||
|
protected virtual void AddPermissionToDictionaryRecursively( |
||||
|
Dictionary<string, PermissionDefinition> permissions, |
||||
|
PermissionDefinition permission) |
||||
|
{ |
||||
|
if (permissions.ContainsKey(permission.Name)) |
||||
|
{ |
||||
|
throw new AbpException("Duplicate permission name: " + permission.Name); |
||||
|
} |
||||
|
|
||||
|
permissions[permission.Name] = permission; |
||||
|
|
||||
|
foreach (var child in permission.Children) |
||||
|
{ |
||||
|
AddPermissionToDictionaryRecursively(permissions, child); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue