protected virtual async Task LoadRolePermissionsAsync(Role role) { if (role == null) { throw new ArgumentNullException(nameof(role)); } if (SupportsRoleClaims) { role.Permissions = new List <Permission>(); //Load role claims and convert it to the permissions and assign to role var storedPermissions = (await GetClaimsAsync(role)).Select(x => Permission.TryCreateFromClaim(x, _jsonOptions.SerializerSettings)).ToList(); var knownPermissionsDict = _knownPermissions.GetAllPermissions().Select(x => x.Clone() as Permission).ToDictionary(x => x.Name, x => x).WithDefaultValue(null); foreach (var storedPermission in storedPermissions) { //Copy all meta information from registered to stored (for particular role) permission var knownPermission = knownPermissionsDict[storedPermission.Name]; if (knownPermission != null) { knownPermission.Patch(storedPermission); } role.Permissions.Add(storedPermission); } } }
protected virtual async Task LoadRolePermissionsAsync(Role role) { if (role == null) { throw new ArgumentNullException(nameof(role)); } if (SupportsRoleClaims) { //Load role claims and convert it to the permissions and assign to role var rolePermissionClaims = (await GetClaimsAsync(role)).Where(x => x.Type == PlatformConstants.Security.Claims.PermissionClaimType); role.Permissions = _knownPermissions.GetAllPermissions().Join(rolePermissionClaims, p => p.Name, c => c.Value, (p, c) => p).ToList(); } }
private Dictionary <string, AuthorizationPolicy> GetDynamicAuthorizationPoliciesFromModulesPermissions() { var cacheKey = CacheKey.With(GetType(), "GetDynamicAuthorizationPoliciesFromModulesPermissions"); var result = _memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) => { var resultLookup = new Dictionary <string, AuthorizationPolicy>(); foreach (var permission in _permissionsProvider.GetAllPermissions()) { resultLookup[permission.Name] = new AuthorizationPolicyBuilder().AddRequirements(new PermissionAuthorizationRequirement { Permission = permission }).Build(); } return(resultLookup); }); return(result); }
private Dictionary <string, AuthorizationPolicy> GetDynamicAuthorizationPoliciesFromModulesPermissions() { var cacheKey = CacheKey.With(GetType(), "GetDynamicAuthorizationPoliciesFromModulesPermissions"); var result = _memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) => { var resultLookup = new Dictionary <string, AuthorizationPolicy>(); foreach (var permission in _permissionsProvider.GetAllPermissions()) { resultLookup[permission.Name] = new AuthorizationPolicyBuilder().AddRequirements(new PermissionAuthorizationRequirement(permission.Name)) //Use the two schema (JwtBearer and ApiKey) authentication for permission authorization policies. .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme, ApiKeyAuthenticationOptions.DefaultScheme) .Build(); } return(resultLookup); }); return(result); }
public static IPermissionsRegistrar WithAvailabeScopesForPermissions(this IPermissionsRegistrar registrar, string[] permissionNames, params PermissionScope[] scopes) { if (registrar == null) { throw new ArgumentNullException(nameof(registrar)); } if (permissionNames == null) { throw new ArgumentNullException(nameof(permissionNames)); } var permissions = registrar.GetAllPermissions().Where(x => permissionNames.Contains(x.Name)); foreach (var permission in permissions) { permission.AvailableScopes.AddRange(scopes.Distinct()); } return(registrar); }
public ActionResult <Permission[]> GetAllRegisteredPermissions() { var result = _permissionsProvider.GetAllPermissions().ToArray(); return(Ok(result)); }
private async Task ExportPlatformEntriesInternalAsync(ZipArchive zipArchive, PlatformExportManifest manifest, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken) { var progressInfo = new ExportImportProgressInfo(); var serializer = GetJsonSerializer(); //Create part for platform entries var platformEntiriesPart = zipArchive.CreateEntry(PlatformZipEntryName, CompressionLevel.Optimal); using (var partStream = platformEntiriesPart.Open()) { using (var sw = new StreamWriter(partStream, Encoding.UTF8)) using (var writer = new JsonTextWriter(sw)) { await writer.WriteStartObjectAsync(); if (manifest.HandleSecurity) { progressInfo.Description = "Roles exporting..."; progressCallback(progressInfo); cancellationToken.ThrowIfCancellationRequested(); await writer.WritePropertyNameAsync("Roles"); await writer.WriteStartArrayAsync(); var roles = _roleManager.Roles.ToList(); if (_roleManager.SupportsRoleClaims) { var permissions = _permissionsProvider.GetAllPermissions().ToArray(); foreach (var role in roles) { role.Permissions = (await _roleManager.GetClaimsAsync(role)).Join(permissions, c => c.Value, p => p.Name, (c, p) => p).ToArray(); serializer.Serialize(writer, role); } writer.Flush(); progressInfo.Description = $"{ roles.Count } roles exported"; progressCallback(progressInfo); } await writer.WriteEndArrayAsync(); cancellationToken.ThrowIfCancellationRequested(); await writer.WritePropertyNameAsync("Users"); await writer.WriteStartArrayAsync(); var usersResult = _userManager.Users.ToArray(); progressInfo.Description = $"Security: {usersResult.Length} users exporting..."; progressCallback(progressInfo); var userExported = 0; foreach (var user in usersResult) { var userExt = await _userManager.FindByIdAsync(user.Id); if (userExt != null) { serializer.Serialize(writer, userExt); userExported++; } } await writer.FlushAsync(); progressInfo.Description = $"{ userExported } of { usersResult.Length } users exported"; progressCallback(progressInfo); await writer.WriteEndArrayAsync(); } if (manifest.HandleSettings) { cancellationToken.ThrowIfCancellationRequested(); await writer.WritePropertyNameAsync("Settings"); await writer.WriteStartArrayAsync(); progressInfo.Description = "Settings: selected modules settings exporting..."; progressCallback(progressInfo); foreach (var module in manifest.Modules) { var moduleSettings = await _settingsManager.GetObjectSettingsAsync(_settingsManager.AllRegisteredSettings.Where(x => x.ModuleId == module.Id).Select(x => x.Name)); //Export only settings with set values foreach (var setting in moduleSettings.Where(x => x.ItHasValues)) { serializer.Serialize(writer, setting); } await writer.FlushAsync(); } progressInfo.Description = $"Settings of modules exported"; progressCallback(progressInfo); await writer.WriteEndArrayAsync(); } cancellationToken.ThrowIfCancellationRequested(); await writer.WritePropertyNameAsync("DynamicProperties"); await writer.WriteStartArrayAsync(); progressInfo.Description = "Dynamic properties: load properties..."; progressCallback(progressInfo); var dynamicProperties = (await _dynamicPropertySearchService.SearchDynamicPropertiesAsync(new DynamicPropertySearchCriteria { Take = int.MaxValue })).Results; foreach (var dynamicProperty in dynamicProperties) { serializer.Serialize(writer, dynamicProperty); } progressInfo.Description = $"Dynamic properties exported"; progressCallback(progressInfo); await writer.WriteEndArrayAsync(); cancellationToken.ThrowIfCancellationRequested(); await writer.WritePropertyNameAsync("DynamicPropertyDictionaryItems"); await writer.WriteStartArrayAsync(); progressInfo.Description = "Dynamic properties Dictionary Items: load properties..."; progressCallback(progressInfo); var dynamicPropertyDictionaryItems = (await _dynamicPropertySearchService.SearchDictionaryItemsAsync(new DynamicPropertyDictionaryItemSearchCriteria { Take = int.MaxValue })).Results; foreach (var dynamicPropertyDictionaryItem in dynamicPropertyDictionaryItems) { serializer.Serialize(writer, dynamicPropertyDictionaryItem); } progressInfo.Description = $"Dynamic properties dictionary items exported"; progressCallback(progressInfo); await writer.WriteEndArrayAsync(); await writer.WriteEndObjectAsync(); await writer.FlushAsync(); } } }
private async Task ExportPlatformEntriesInternalAsync(ZipArchive zipArchive, PlatformExportManifest manifest, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken) { var progressInfo = new ExportImportProgressInfo(); var platformExportObj = new PlatformExportEntries(); if (manifest.HandleSecurity) { //Roles platformExportObj.Roles = _roleManager.Roles.ToList(); if (_roleManager.SupportsRoleClaims) { var permissions = _permissionsProvider.GetAllPermissions(); foreach (var role in platformExportObj.Roles) { role.Permissions = (await _roleManager.GetClaimsAsync(role)).Join(permissions, c => c.Value, p => p.Name, (c, p) => p).ToArray(); } } //users var usersResult = _userManager.Users.ToArray(); progressInfo.Description = $"Security: {usersResult.Count()} users exporting..."; progressCallback(progressInfo); foreach (var user in usersResult) { var userExt = await _userManager.FindByIdAsync(user.Id); if (userExt != null) { platformExportObj.Users.Add(userExt); } } } //Export setting for selected modules if (manifest.HandleSettings) { progressInfo.Description = "Settings: selected modules settings exporting..."; progressCallback(progressInfo); foreach (var module in manifest.Modules) { var moduleSettings = await _settingsManager.GetObjectSettingsAsync(_settingsManager.AllRegisteredSettings.Where(x => x.ModuleId == module.Id).Select(x => x.Name)); platformExportObj.Settings = platformExportObj.Settings.Concat(moduleSettings).ToList(); } } //Dynamic properties progressInfo.Description = "Dynamic properties: load properties..."; progressCallback(progressInfo); platformExportObj.DynamicProperties = (await _dynamicPropertySearchService.SearchDynamicPropertiesAsync(new DynamicPropertySearchCriteria { Take = int.MaxValue })).Results; platformExportObj.DynamicPropertyDictionaryItems = (await _dynamicPropertySearchService.SearchDictionaryItemsAsync(new DynamicPropertyDictionaryItemSearchCriteria { Take = int.MaxValue })).Results; //Create part for platform entries var platformEntiriesPart = zipArchive.CreateEntry(_platformZipEntryName, CompressionLevel.Optimal); using (var partStream = platformEntiriesPart.Open()) { platformExportObj.SerializeJson(partStream); } }