From b6676dc54c9c482582faff73a6e5dc2fe5fb85d4 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Tue, 14 Jul 2020 15:01:11 +0800 Subject: [PATCH] Add some methods. --- .../Localization/Resources/en.json | 3 +- .../Localization/Resources/zh-Hans.json | 3 +- .../ApiScopes/IApiScopeeRepository.cs | 20 ++++++++- .../IdentityResources/IdentityResource.cs | 20 +++++++++ .../ApiResources/ApiResourceRepository.cs | 14 +++++- .../ApiScopes/ApiScopeRepository.cs | 45 ++++++++++++++++++- .../MongoDB/MongoApiResourceRepository.cs | 2 +- .../MongoDB/MongoApiScopeRepository.cs | 22 ++++++++- 8 files changed, 120 insertions(+), 9 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/en.json b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/en.json index f974297b06..22b5f13474 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/en.json +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/en.json @@ -3,10 +3,11 @@ "texts": { "Volo.IdentityServer:DuplicateIdentityResourceName": "Identity Resource name already exist: {Name}", "Volo.IdentityServer:DuplicateApiResourceName": "Api Resource name already exist: {Name}", + "Volo.IdentityServer:DuplicateApiScopeName": "Api Scope name already exist: {Name}", "Volo.IdentityServer:DuplicateClientId": "ClientId already exist: {ClientId}", "UserLockedOut": "The user account has been locked out due to invalid login attempts. Please wait a while and try again.", "InvalidUserNameOrPassword": "Invalid username or password!", "LoginIsNotAllowed": "You are not allowed to login! You need to confirm your email/phone number.", "InvalidUsername": "Invalid username or password!" } -} \ No newline at end of file +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/zh-Hans.json b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/zh-Hans.json index ac175e574d..1d40aa16b3 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/zh-Hans.json +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain.Shared/Volo/Abp/IdentityServer/Localization/Resources/zh-Hans.json @@ -3,10 +3,11 @@ "texts": { "Volo.IdentityServer:DuplicateIdentityResourceName": "Identity资源名称已存在: {Name}", "Volo.IdentityServer:DuplicateApiResourceName": "Api资源名称已存在: {Name}", + "Volo.IdentityServer:DuplicateApiScopeName": "Api Scope已存在: {Name}", "Volo.IdentityServer:DuplicateClientId": "ClientId已经存在: {ClientId}", "UserLockedOut": "登录失败,用户账户已被锁定.请稍后再试.", "InvalidUserNameOrPassword": "用户名或密码错误!", "LoginIsNotAllowed": "无法登录!你需要验证邮箱地址/手机号.", "InvalidUsername": "用户名或密码错误!" } -} \ No newline at end of file +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiScopes/IApiScopeeRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiScopes/IApiScopeeRepository.cs index c6ebcc26e5..a0aba8fe2a 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiScopes/IApiScopeeRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiScopes/IApiScopeeRepository.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; namespace Volo.Abp.IdentityServer.ApiScopes { - public interface IApiScopeRepository : IBasicRepository + public interface IApiScopeRepository : IBasicRepository { Task GetByNameAsync( string scopeName, @@ -18,5 +19,20 @@ namespace Volo.Abp.IdentityServer.ApiScopes bool includeDetails = false, CancellationToken cancellationToken = default ); + + Task> GetListAsync( + string sorting, + int skipCount, + int maxResultCount, + string filter = null, + bool includeDetails = false, + CancellationToken cancellationToken = default + ); + + Task CheckNameExistAsync( + string name, + Guid? expectedId = null, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResource.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResource.cs index 246e1ddf98..b5812da7e2 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResource.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResource.cs @@ -89,5 +89,25 @@ namespace Volo.Abp.IdentityServer.IdentityResources { return UserClaims.FirstOrDefault(c => c.Type == type); } + + public virtual void AddProperty([NotNull] string key, string value) + { + Properties.Add(new IdentityResourceProperty(Id, key, value)); + } + + public virtual void RemoveAllProperties() + { + Properties.Clear(); + } + + public virtual void RemoveProperty(string key) + { + Properties.RemoveAll(r => r.Key == key); + } + + public virtual IdentityResourceProperty FindProperty(string key) + { + return Properties.FirstOrDefault(r => r.Key == key); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs index 4824696108..6718b20fb8 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiResources/ApiResourceRepository.cs @@ -68,7 +68,7 @@ namespace Volo.Abp.IdentityServer.ApiResources public virtual async Task CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default) { - return await DbSet.AnyAsync(ar => ar.Id != expectedId && ar.Name == name, cancellationToken: cancellationToken); + return await DbSet.AnyAsync(ar => ar.Id != expectedId && ar.Name == name, GetCancellationToken(cancellationToken)); } public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default) @@ -85,6 +85,18 @@ namespace Volo.Abp.IdentityServer.ApiResources DbContext.Set().Remove(scope); } + var resourceSecrets = DbContext.Set().Where(s => s.ApiResourceId == id); + foreach (var secret in resourceSecrets) + { + DbContext.Set().Remove(secret); + } + + var apiResourceProperties = DbContext.Set().Where(s => s.ApiResourceId == id); + foreach (var property in apiResourceProperties) + { + DbContext.Set().Remove(property); + } + await base.DeleteAsync(id, autoSave, cancellationToken); } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiScopes/ApiScopeRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiScopes/ApiScopeRepository.cs index b4674aad37..88772bfab7 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiScopes/ApiScopeRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/ApiScopes/ApiScopeRepository.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -9,7 +11,7 @@ using Volo.Abp.IdentityServer.EntityFrameworkCore; namespace Volo.Abp.IdentityServer.ApiScopes { - public class ApiScopeRepository : EfCoreRepository, IApiScopeRepository + public class ApiScopeRepository : EfCoreRepository, IApiScopeRepository { public ApiScopeRepository(IDbContextProvider dbContextProvider) : base( dbContextProvider) @@ -30,5 +32,44 @@ namespace Volo.Abp.IdentityServer.ApiScopes return await query.ToListAsync(GetCancellationToken(cancellationToken)); } + + public async Task> GetListAsync(string sorting, int skipCount, int maxResultCount, string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default) + { + return await DbSet + .IncludeDetails(includeDetails) + .WhereIf(!filter.IsNullOrWhiteSpace(), x => x.Name.Contains(filter) || + x.Description.Contains(filter) || + x.DisplayName.Contains(filter)) + .OrderBy(sorting ?? "name desc") + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default) + { + return await DbSet.AnyAsync(x => x.Id != expectedId && x.Name == name, GetCancellationToken(cancellationToken)); + } + + public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = new CancellationToken()) + { + var scopeClaims = DbContext.Set().Where(sc => sc.ApiScopeId == id); + foreach (var claim in scopeClaims) + { + DbContext.Set().Remove(claim); + } + + var scopeProperties = DbContext.Set().Where(s => s.ApiScopeId == id); + foreach (var property in scopeProperties) + { + DbContext.Set().Remove(property); + } + + await base.DeleteAsync(id, autoSave, cancellationToken); + } + + public override IQueryable WithDetails() + { + return GetQueryable().IncludeDetails(); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiResourceRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiResourceRepository.cs index 55a0cece76..0f4337b895 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiResourceRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiResourceRepository.cs @@ -63,7 +63,7 @@ namespace Volo.Abp.IdentityServer.MongoDB public virtual async Task CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default) { - return await GetMongoQueryable().AnyAsync(ar => ar.Id != expectedId && ar.Name == name, cancellationToken: cancellationToken); + return await GetMongoQueryable().AnyAsync(ar => ar.Id != expectedId && ar.Name == name, GetCancellationToken(cancellationToken)); } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiScopeRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiScopeRepository.cs index 8a20b8ae2b..a446e772e8 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiScopeRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoApiScopeRepository.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Linq; using MongoDB.Driver; using MongoDB.Driver.Linq; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.IdentityServer.ApiScopes; +using System.Linq.Dynamic.Core; using Volo.Abp.MongoDB; namespace Volo.Abp.IdentityServer.MongoDB @@ -33,5 +34,24 @@ namespace Volo.Abp.IdentityServer.MongoDB return await query.ToListAsync(GetCancellationToken(cancellationToken)); } + + public async Task> GetListAsync(string sorting, int skipCount, int maxResultCount, string filter = null, bool includeDetails = false, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .WhereIf(!filter.IsNullOrWhiteSpace(), + x => x.Name.Contains(filter) || + x.Description.Contains(filter) || + x.DisplayName.Contains(filter)) + .OrderBy(sorting ?? nameof(ApiScope.Name)) + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default) + { + return await GetMongoQueryable().AnyAsync(x => x.Id != expectedId && x.Name == name, GetCancellationToken(cancellationToken)); + } } }