public async Task <List <string> > GetAllClaims() { var claimItemType = _appSession.TenantName.IsNullOrWhiteSpace() ? ClaimItemType.Host : ClaimItemType.Tenant; return(await Task.FromResult( _claimsManager.GetAll(claimItemType).Select(o => o.Claim).ToList() )); }
/// <summary> /// 创建系统角色 /// </summary> /// <param name="dbContext">数据库上下文</param> /// <param name="tenantName">租户名称</param> /// <returns></returns> protected virtual async Task <Role> CreateRoles(DbContext dbContext, string tenantName) { var roleStore = dbContext.Set <Role>(); var roleClaimStore = dbContext.Set <RoleClaim>(); var systemRole = await roleStore.IgnoreQueryFilters() .FirstOrDefaultAsync(o => o.Name == AppConsts.Authorization.SystemRoleName && o.TenantName == tenantName); if (systemRole == null) { systemRole = new Role() { Name = AppConsts.Authorization.SystemRoleName, DisplayName = AppConsts.Authorization.SystemRoleName, Description = AppConsts.Authorization.SystemRoleName, NormalizedName = this._lookupNormalizer.NormalizeName(AppConsts.Authorization.SystemRoleName), IsStatic = true, TenantName = tenantName }; await roleStore.AddAsync(systemRole); await dbContext.SaveChangesAsync(); } // 查询现有权限 var roleClaims = await roleClaimStore.IgnoreQueryFilters() .Where(o => o.RoleId == systemRole.Id && o.TenantName == tenantName) .ToListAsync(); // 移除权限 roleClaimStore.RemoveRange(roleClaims); // 添加权限 roleClaims.Clear(); var claimItems = _claimsManager.GetAll( string.IsNullOrWhiteSpace(tenantName) ? ClaimItemType.Host : ClaimItemType.Tenant ); foreach (var item in claimItems) { roleClaims.Add(new RoleClaim() { RoleId = systemRole.Id, TenantName = tenantName, ClaimType = item.Claim, ClaimValue = item.Claim }); } await roleClaimStore.AddRangeAsync(roleClaims); await dbContext.SaveChangesAsync(); return(systemRole); }