public virtual async Task UpdateAsync(string providerName, string providerKey, UpdatePermissionsDto input) { await CheckProviderPolicy(providerName); var permissions = await PermissionGrantRepository.GetListAsync(providerName, providerKey); foreach (var permission in input.Permissions) { var editPermission = permissions.FirstOrDefault(p => p.Name.Equals(permission.Name)); if (editPermission == null) { if (permission.IsGranted) { var permissionGrant = new PermissionGrant(GuidGenerator.Create(), permission.Name, providerName, providerKey, CurrentTenant.Id); await PermissionGrantRepository.InsertAsync(permissionGrant); } } else { if (!permission.IsGranted) { await PermissionGrantRepository.DeleteAsync(editPermission.Id); } } } }
protected virtual async Task <List <KeyValuePair <string, PermissionGrantCacheItem> > > SetCacheItemsAsync( string providerName, string providerKey, List <string> notCacheKeys) { var permissions = PermissionDefinitionManager.GetPermissions().Where(x => notCacheKeys.Any(k => GetPermissionNameFormCacheKeyOrNull(k) == x.Name)).ToList(); Logger.LogDebug($"Getting not cache granted permissions from the repository for this provider name,key: {providerName},{providerKey}"); var grantedPermissionsHashSet = new HashSet <string>( (await PermissionGrantRepository.GetListAsync(notCacheKeys.Select(GetPermissionNameFormCacheKeyOrNull).ToArray(), providerName, providerKey)).Select(p => p.Name) ); Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}"); var cacheItems = new List <KeyValuePair <string, PermissionGrantCacheItem> >(); foreach (var permission in permissions) { var isGranted = grantedPermissionsHashSet.Contains(permission.Name); cacheItems.Add(new KeyValuePair <string, PermissionGrantCacheItem>( CalculateCacheKey(permission.Name, providerName, providerKey), new PermissionGrantCacheItem(isGranted)) ); } await Cache.SetManyAsync(cacheItems); Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}"); return(cacheItems); }
public virtual async Task SeedAsync( string providerName, string providerKey, IEnumerable <string> grantedPermissions, Guid?tenantId = null) { using (CurrentTenant.Change(tenantId)) { var names = grantedPermissions.ToArray(); var existsPermissionGrants = (await PermissionGrantRepository.GetListAsync(names, providerName, providerKey)).Select(x => x.Name).ToList(); foreach (var permissionName in names.Except(existsPermissionGrants)) { await PermissionGrantRepository.InsertAsync( new PermissionGrant( GuidGenerator.Create(), permissionName, providerName, providerKey, tenantId ) ); } } }
public virtual async Task DeleteAsync(string providerName, string providerKey) { var permissionGrants = await PermissionGrantRepository.GetListAsync(providerName, providerKey); foreach (var permissionGrant in permissionGrants) { await PermissionGrantRepository.DeleteAsync(permissionGrant); } }
public virtual async Task <MultiplePermissionValueProviderGrantInfo> CheckAsync(string[] names, string providerName, string providerKey) { var multiplePermissionValueProviderGrantInfo = new MultiplePermissionValueProviderGrantInfo(names); if (providerName != Name) { return(multiplePermissionValueProviderGrantInfo); } var permissionGrants = await PermissionGrantRepository.GetListAsync(names, providerName, providerKey); foreach (var permissionName in names) { var isGrant = permissionGrants.Any(x => x.Name == permissionName); multiplePermissionValueProviderGrantInfo.Result[permissionName] = new PermissionValueProviderGrantInfo(isGrant, providerKey); } return(multiplePermissionValueProviderGrantInfo); }
public override async Task <MultiplePermissionValueProviderGrantInfo> CheckAsync(string[] names, string providerName, string providerKey) { var multiplePermissionValueProviderGrantInfo = new MultiplePermissionValueProviderGrantInfo(names); var permissionGrants = new List <PermissionGrant>(); if (providerName == Name) { permissionGrants.AddRange(await PermissionGrantRepository.GetListAsync(names, providerName, providerKey)); } if (providerName == UserPermissionValueProvider.ProviderName) { var userId = Guid.Parse(providerKey); var roleNames = await UserRoleFinder.GetRolesAsync(userId); foreach (var roleName in roleNames) { permissionGrants.AddRange(await PermissionGrantRepository.GetListAsync(names, Name, roleName)); } } permissionGrants = permissionGrants.Distinct().ToList(); if (!permissionGrants.Any()) { return(multiplePermissionValueProviderGrantInfo); } foreach (var permissionName in names) { var permissionGrant = permissionGrants.FirstOrDefault(x => x.Name == permissionName); if (permissionGrant != null) { multiplePermissionValueProviderGrantInfo.Result[permissionName] = new PermissionValueProviderGrantInfo(true, permissionGrant.ProviderKey); } } return(multiplePermissionValueProviderGrantInfo); }
protected virtual async Task SetCacheItemsAsync( string providerName, string providerKey, string currentName, PermissionGrantCacheItem currentCacheItem) { var permissions = PermissionDefinitionManager.GetPermissions(); Logger.LogDebug($"Getting all granted permissions from the repository for this provider name,key: {providerName},{providerKey}"); var grantedPermissionsHashSet = new HashSet <string>( (await PermissionGrantRepository.GetListAsync(providerName, providerKey)).Select(p => p.Name) ); Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}"); var cacheItems = new List <KeyValuePair <string, PermissionGrantCacheItem> >(); foreach (var permission in permissions) { var isGranted = grantedPermissionsHashSet.Contains(permission.Name); cacheItems.Add(new KeyValuePair <string, PermissionGrantCacheItem>( CalculateCacheKey(permission.Name, providerName, providerKey), new PermissionGrantCacheItem(isGranted)) ); if (permission.Name == currentName) { currentCacheItem.IsGranted = isGranted; } } await Cache.SetManyAsync(cacheItems); Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}"); }
public virtual async Task <GetPermissionListResultDto> GetAsync(string providerName, string providerKey) { var permissionListResult = new GetPermissionListResultDto { EntityDisplayName = providerKey, Groups = new List <PermissionGroupDto>() }; var multiTenancySide = CurrentTenant.GetMultiTenancySide(); var permissionGroups = PermissionDefinitionManager.GetGroups(); IEnumerable <PermissionGrant> permissions = await PermissionGrantRepository.GetListAsync(providerName, providerKey); // 如果是当前用户权限,还需要查询角色权限 if (providerName.Equals("U")) { var userId = CurrentUser.GetId().ToString(); if (providerKey.Equals(userId)) { foreach (var role in CurrentUser.Roles) { var rolePermissions = await PermissionGrantRepository .GetListAsync(RolePermissionValueProvider.ProviderName, role); permissions = permissions.Union(rolePermissions); } } } foreach (var permissionGroup in permissionGroups) { var groupDto = new PermissionGroupDto { Name = permissionGroup.Name, DisplayName = permissionGroup.DisplayName.Localize(StringLocalizerFactory), Permissions = new List <PermissionGrantInfoDto>() }; foreach (var permission in permissionGroup.GetPermissionsWithChildren()) { if (!permission.IsEnabled) { continue; } if (permission.Providers.Any() && !permission.Providers.Contains(providerName)) { continue; } if (!permission.MultiTenancySide.HasFlag(multiTenancySide)) { continue; } var grantInfoDto = new PermissionGrantInfoDto { Name = permission.Name, DisplayName = permission.DisplayName.Localize(StringLocalizerFactory), ParentName = permission.Parent?.Name, AllowedProviders = permission.Providers, GrantedProviders = new List <ProviderInfoDto>() }; var grantedPermissions = permissions.Where(p => p.Name.Equals(permission.Name)); foreach (var grantedPermission in grantedPermissions) { grantInfoDto.IsGranted = true; grantInfoDto.GrantedProviders.Add(new ProviderInfoDto { ProviderKey = grantedPermission.ProviderKey, ProviderName = grantedPermission.ProviderName }); } groupDto.Permissions.Add(grantInfoDto); } if (groupDto.Permissions.Any()) { permissionListResult.Groups.Add(groupDto); } } return(permissionListResult); }