From 986a33cd1553b18096ce7f1884ff4e1adee791ce Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:44:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86UnitOfWork.SaveChangesAsync=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E4=B8=BAUnitOfWork.CompleteAsync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiGateway/Ocelot/RouteGroupAppService.cs | 184 ++-- .../Identity/IdentityClaimTypeAppService.cs | 258 ++--- .../Abp/Identity/IdentityRoleAppService.cs | 246 ++--- .../Abp/Identity/IdentityUserAppService.cs | 16 +- .../Abp/Identity/MyProfileAppService.cs | 4 +- .../Identity/OrganizationUnitAppService.cs | 472 ++++----- .../ApiResources/ApiResourceAppService.cs | 6 +- .../ApiScopes/ApiScopeAppService.cs | 284 ++--- .../Clients/ClientAppService.cs | 966 +++++++++--------- .../Grants/PersistedGrantAppService.cs | 104 +- .../IdentityResourceAppService.cs | 262 ++--- .../LINGYUN/Platform/Datas/DataAppService.cs | 10 +- .../Platform/Layouts/LayoutAppService.cs | 6 +- .../LINGYUN/Platform/Menus/MenuAppService.cs | 4 +- .../SettingManagement/SettingAppService.cs | 4 +- 15 files changed, 1413 insertions(+), 1413 deletions(-) diff --git a/aspnet-core/modules/apigateway/LINGYUN.ApiGateway.Application/LINGYUN/ApiGateway/Ocelot/RouteGroupAppService.cs b/aspnet-core/modules/apigateway/LINGYUN.ApiGateway.Application/LINGYUN/ApiGateway/Ocelot/RouteGroupAppService.cs index bb5d3ceea..bc88d48a3 100644 --- a/aspnet-core/modules/apigateway/LINGYUN.ApiGateway.Application/LINGYUN/ApiGateway/Ocelot/RouteGroupAppService.cs +++ b/aspnet-core/modules/apigateway/LINGYUN.ApiGateway.Application/LINGYUN/ApiGateway/Ocelot/RouteGroupAppService.cs @@ -1,92 +1,92 @@ -using LINGYUN.ApiGateway.Data.Filter; -using Microsoft.AspNetCore.Authorization; -using System.Collections.Generic; -using System.Threading.Tasks; -using Volo.Abp.Application.Dtos; -using Volo.Abp.Data; - -namespace LINGYUN.ApiGateway.Ocelot -{ - [Authorize(ApiGatewayPermissions.RouteGroup.Default)] - public class RouteGroupAppService : ApiGatewayApplicationServiceBase, IRouteGroupAppService - { - protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService(); - protected IRouteGroupRepository RouterRepository { get; } - - public RouteGroupAppService( - IRouteGroupRepository routerRepository) - { - RouterRepository = routerRepository; - } - - [Authorize(ApiGatewayPermissions.RouteGroup.Create)] - public virtual async Task CreateAsync(RouteGroupCreateDto input) - { - var router = new RouteGroup(input.AppId, input.AppName, input.AppIpAddress); - router.Name = input.Name; - router.IsActive = input.IsActive; - router.Description = input.Description; - - router = await RouterRepository.InsertAsync(router, true); - - return ObjectMapper.Map(router); - } - - public virtual async Task GetAsync(RouteGroupGetByAppIdInputDto input) - { - using (DataFilter.Disable()) - { - var router = await RouterRepository.GetByAppIdAsync(input.AppId); - - return ObjectMapper.Map(router); - } - } - - public virtual async Task> GetActivedAsync() - { - var appIdsDto = new List(); - var appKeys = await RouterRepository.GetActivedAppsAsync(); - - foreach(var app in appKeys) - { - appIdsDto.Add(new RouteGroupAppIdsDto(app.AppId, app.AppName)); - } - - return new ListResultDto(appIdsDto); - } - - public virtual async Task> GetAsync(RouteGroupGetByPagedInputDto input) - { - using (DataFilter.Disable()) - { - var (Routers, TotalCount) = await RouterRepository.GetPagedListAsync(input.Filter, - input.Sorting, input.SkipCount, input.MaxResultCount); - var routers = ObjectMapper.Map, List>(Routers); - - return new PagedResultDto(TotalCount, routers); - } - } - - [Authorize(ApiGatewayPermissions.RouteGroup.Update)] - public virtual async Task UpdateAsync(RouteGroupUpdateDto input) - { - var router = await RouterRepository.GetByAppIdAsync(input.AppId); - router.Name = input.Name; - router.IsActive = input.IsActive; - router.Description = input.Description; - router.SwitchApp(input.AppName, input.AppIpAddress); - - await RouterRepository.UpdateAsync(router); - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(router); - } - - [Authorize(ApiGatewayPermissions.RouteGroup.Delete)] - public virtual async Task DeleteAsync(RouteGroupGetByAppIdInputDto input) - { - var router = await RouterRepository.GetByAppIdAsync(input.AppId); - await RouterRepository.DeleteAsync(router); - } - } -} +using LINGYUN.ApiGateway.Data.Filter; +using Microsoft.AspNetCore.Authorization; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Data; + +namespace LINGYUN.ApiGateway.Ocelot +{ + [Authorize(ApiGatewayPermissions.RouteGroup.Default)] + public class RouteGroupAppService : ApiGatewayApplicationServiceBase, IRouteGroupAppService + { + protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService(); + protected IRouteGroupRepository RouterRepository { get; } + + public RouteGroupAppService( + IRouteGroupRepository routerRepository) + { + RouterRepository = routerRepository; + } + + [Authorize(ApiGatewayPermissions.RouteGroup.Create)] + public virtual async Task CreateAsync(RouteGroupCreateDto input) + { + var router = new RouteGroup(input.AppId, input.AppName, input.AppIpAddress); + router.Name = input.Name; + router.IsActive = input.IsActive; + router.Description = input.Description; + + router = await RouterRepository.InsertAsync(router, true); + + return ObjectMapper.Map(router); + } + + public virtual async Task GetAsync(RouteGroupGetByAppIdInputDto input) + { + using (DataFilter.Disable()) + { + var router = await RouterRepository.GetByAppIdAsync(input.AppId); + + return ObjectMapper.Map(router); + } + } + + public virtual async Task> GetActivedAsync() + { + var appIdsDto = new List(); + var appKeys = await RouterRepository.GetActivedAppsAsync(); + + foreach(var app in appKeys) + { + appIdsDto.Add(new RouteGroupAppIdsDto(app.AppId, app.AppName)); + } + + return new ListResultDto(appIdsDto); + } + + public virtual async Task> GetAsync(RouteGroupGetByPagedInputDto input) + { + using (DataFilter.Disable()) + { + var (Routers, TotalCount) = await RouterRepository.GetPagedListAsync(input.Filter, + input.Sorting, input.SkipCount, input.MaxResultCount); + var routers = ObjectMapper.Map, List>(Routers); + + return new PagedResultDto(TotalCount, routers); + } + } + + [Authorize(ApiGatewayPermissions.RouteGroup.Update)] + public virtual async Task UpdateAsync(RouteGroupUpdateDto input) + { + var router = await RouterRepository.GetByAppIdAsync(input.AppId); + router.Name = input.Name; + router.IsActive = input.IsActive; + router.Description = input.Description; + router.SwitchApp(input.AppName, input.AppIpAddress); + + await RouterRepository.UpdateAsync(router); + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(router); + } + + [Authorize(ApiGatewayPermissions.RouteGroup.Delete)] + public virtual async Task DeleteAsync(RouteGroupGetByAppIdInputDto input) + { + var router = await RouterRepository.GetByAppIdAsync(input.AppId); + await RouterRepository.DeleteAsync(router); + } + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityClaimTypeAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityClaimTypeAppService.cs index 5320a6734..7d5082c51 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityClaimTypeAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityClaimTypeAppService.cs @@ -1,129 +1,129 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Application.Dtos; -using Volo.Abp.Identity; - -namespace LINGYUN.Abp.Identity -{ - [Authorize(IdentityPermissions.IdentityClaimType.Default)] - public class IdentityClaimTypeAppService : IdentityAppServiceBase, IIdentityClaimTypeAppService - { - protected IdentityClaimTypeManager IdentityClaimTypeManager { get; } - - protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; } - - public IdentityClaimTypeAppService( - IdentityClaimTypeManager identityClaimTypeManager, - IIdentityClaimTypeRepository identityClaimTypeRepository) - { - IdentityClaimTypeManager = identityClaimTypeManager; - IdentityClaimTypeRepository = identityClaimTypeRepository; - } - - [Authorize(IdentityPermissions.IdentityClaimType.Create)] - public virtual async Task CreateAsync(IdentityClaimTypeCreateDto input) - { - if (await IdentityClaimTypeRepository.AnyAsync(input.Name)) - { - throw new UserFriendlyException(L["IdentityClaimTypeAlreadyExists", input.Name]); - } - var identityClaimType = new IdentityClaimType( - GuidGenerator.Create(), - input.Name, - input.Required, - input.IsStatic, - input.Regex, - input.RegexDescription, - input.Description, - input.ValueType - ); - identityClaimType = await IdentityClaimTypeManager.CreateAsync(identityClaimType); - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(identityClaimType); - } - - [Authorize(IdentityPermissions.IdentityClaimType.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - var identityClaimType = await IdentityClaimTypeRepository.FindAsync(id); - if (identityClaimType == null) - { - return; - } - CheckDeletionClaimType(identityClaimType); - await IdentityClaimTypeRepository.DeleteAsync(identityClaimType); - } - - public virtual async Task GetAsync(Guid id) - { - var identityClaimType = await IdentityClaimTypeRepository.FindAsync(id); - - return ObjectMapper.Map(identityClaimType); - } - - public virtual async Task> GetAllListAsync() - { - var identityClaimTypes = await IdentityClaimTypeRepository - .GetListAsync(); - - return new ListResultDto( - ObjectMapper.Map, List>(identityClaimTypes)); - } - - public virtual async Task> GetListAsync(IdentityClaimTypeGetByPagedDto input) - { - var identityClaimTypeCount = await IdentityClaimTypeRepository.GetCountAsync(input.Filter); - - var identityClaimTypes = await IdentityClaimTypeRepository - .GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter); - - return new PagedResultDto(identityClaimTypeCount, - ObjectMapper.Map, List>(identityClaimTypes)); - } - - [Authorize(IdentityPermissions.IdentityClaimType.Update)] - public virtual async Task UpdateAsync(Guid id, IdentityClaimTypeUpdateDto input) - { - var identityClaimType = await IdentityClaimTypeRepository.GetAsync(id); - CheckChangingClaimType(identityClaimType); - identityClaimType.Required = input.Required; - if (!string.Equals(identityClaimType.Regex, input.Regex, StringComparison.InvariantCultureIgnoreCase)) - { - identityClaimType.Regex = input.Regex; - } - if (!string.Equals(identityClaimType.RegexDescription, input.RegexDescription, StringComparison.InvariantCultureIgnoreCase)) - { - identityClaimType.RegexDescription = input.RegexDescription; - } - if (!string.Equals(identityClaimType.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) - { - identityClaimType.Description = input.Description; - } - - identityClaimType = await IdentityClaimTypeManager.UpdateAsync(identityClaimType); - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(identityClaimType); - } - - protected virtual void CheckChangingClaimType(IdentityClaimType claimType) - { - if (claimType.IsStatic) - { - throw new BusinessException(IdentityErrorCodes.StaticClaimTypeChange); - } - } - - protected virtual void CheckDeletionClaimType(IdentityClaimType claimType) - { - if (claimType.IsStatic) - { - throw new BusinessException(IdentityErrorCodes.StaticClaimTypeDeletion); - } - } - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Identity; + +namespace LINGYUN.Abp.Identity +{ + [Authorize(IdentityPermissions.IdentityClaimType.Default)] + public class IdentityClaimTypeAppService : IdentityAppServiceBase, IIdentityClaimTypeAppService + { + protected IdentityClaimTypeManager IdentityClaimTypeManager { get; } + + protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; } + + public IdentityClaimTypeAppService( + IdentityClaimTypeManager identityClaimTypeManager, + IIdentityClaimTypeRepository identityClaimTypeRepository) + { + IdentityClaimTypeManager = identityClaimTypeManager; + IdentityClaimTypeRepository = identityClaimTypeRepository; + } + + [Authorize(IdentityPermissions.IdentityClaimType.Create)] + public virtual async Task CreateAsync(IdentityClaimTypeCreateDto input) + { + if (await IdentityClaimTypeRepository.AnyAsync(input.Name)) + { + throw new UserFriendlyException(L["IdentityClaimTypeAlreadyExists", input.Name]); + } + var identityClaimType = new IdentityClaimType( + GuidGenerator.Create(), + input.Name, + input.Required, + input.IsStatic, + input.Regex, + input.RegexDescription, + input.Description, + input.ValueType + ); + identityClaimType = await IdentityClaimTypeManager.CreateAsync(identityClaimType); + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(identityClaimType); + } + + [Authorize(IdentityPermissions.IdentityClaimType.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + var identityClaimType = await IdentityClaimTypeRepository.FindAsync(id); + if (identityClaimType == null) + { + return; + } + CheckDeletionClaimType(identityClaimType); + await IdentityClaimTypeRepository.DeleteAsync(identityClaimType); + } + + public virtual async Task GetAsync(Guid id) + { + var identityClaimType = await IdentityClaimTypeRepository.FindAsync(id); + + return ObjectMapper.Map(identityClaimType); + } + + public virtual async Task> GetAllListAsync() + { + var identityClaimTypes = await IdentityClaimTypeRepository + .GetListAsync(); + + return new ListResultDto( + ObjectMapper.Map, List>(identityClaimTypes)); + } + + public virtual async Task> GetListAsync(IdentityClaimTypeGetByPagedDto input) + { + var identityClaimTypeCount = await IdentityClaimTypeRepository.GetCountAsync(input.Filter); + + var identityClaimTypes = await IdentityClaimTypeRepository + .GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter); + + return new PagedResultDto(identityClaimTypeCount, + ObjectMapper.Map, List>(identityClaimTypes)); + } + + [Authorize(IdentityPermissions.IdentityClaimType.Update)] + public virtual async Task UpdateAsync(Guid id, IdentityClaimTypeUpdateDto input) + { + var identityClaimType = await IdentityClaimTypeRepository.GetAsync(id); + CheckChangingClaimType(identityClaimType); + identityClaimType.Required = input.Required; + if (!string.Equals(identityClaimType.Regex, input.Regex, StringComparison.InvariantCultureIgnoreCase)) + { + identityClaimType.Regex = input.Regex; + } + if (!string.Equals(identityClaimType.RegexDescription, input.RegexDescription, StringComparison.InvariantCultureIgnoreCase)) + { + identityClaimType.RegexDescription = input.RegexDescription; + } + if (!string.Equals(identityClaimType.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) + { + identityClaimType.Description = input.Description; + } + + identityClaimType = await IdentityClaimTypeManager.UpdateAsync(identityClaimType); + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(identityClaimType); + } + + protected virtual void CheckChangingClaimType(IdentityClaimType claimType) + { + if (claimType.IsStatic) + { + throw new BusinessException(IdentityErrorCodes.StaticClaimTypeChange); + } + } + + protected virtual void CheckDeletionClaimType(IdentityClaimType claimType) + { + if (claimType.IsStatic) + { + throw new BusinessException(IdentityErrorCodes.StaticClaimTypeDeletion); + } + } + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityRoleAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityRoleAppService.cs index 7d9a21d01..0b17bc430 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityRoleAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityRoleAppService.cs @@ -1,123 +1,123 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Claims; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Application.Dtos; -using Volo.Abp.Identity; - -namespace LINGYUN.Abp.Identity -{ - [Authorize(Volo.Abp.Identity.IdentityPermissions.Roles.Default)] - public class IdentityRoleAppService : IdentityAppServiceBase, IIdentityRoleAppService - { - protected IIdentityRoleRepository IdentityRoleRepository { get; } - protected OrganizationUnitManager OrganizationUnitManager { get; } - protected IOrganizationUnitRepository OrganizationUnitRepository { get; } - public IdentityRoleAppService( - IIdentityRoleRepository roleRepository, - OrganizationUnitManager organizationUnitManager) - { - OrganizationUnitManager = organizationUnitManager; - IdentityRoleRepository = roleRepository; - } - - #region OrganizationUnit - - [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] - public virtual async Task> GetOrganizationUnitsAsync(Guid id) - { - var origanizationUnits = await IdentityRoleRepository.GetOrganizationUnitsAsync(id); - - return new ListResultDto( - ObjectMapper.Map, List>(origanizationUnits)); - } - - [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] - public virtual async Task SetOrganizationUnitsAsync(Guid id, IdentityRoleAddOrRemoveOrganizationUnitDto input) - { - var origanizationUnits = await IdentityRoleRepository.GetOrganizationUnitsAsync(id, true); - - var notInRoleOuIds = input.OrganizationUnitIds.Where(ouid => !origanizationUnits.Any(ou => ou.Id.Equals(ouid))); - - foreach (var ouId in notInRoleOuIds) - { - await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(id, ouId); - } - - var removeRoleOriganzationUnits = origanizationUnits.Where(ou => !input.OrganizationUnitIds.Contains(ou.Id)); - foreach (var origanzationUnit in removeRoleOriganzationUnits) - { - origanzationUnit.RemoveRole(id); - } - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] - public virtual async Task RemoveOrganizationUnitsAsync(Guid id, Guid ouId) - { - await OrganizationUnitManager.RemoveRoleFromOrganizationUnitAsync(id, ouId); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - #endregion - - #region ClaimType - - public virtual async Task> GetClaimsAsync(Guid id) - { - var role = await IdentityRoleRepository.GetAsync(id); - - return new ListResultDto(ObjectMapper.Map, List>(role.Claims)); - } - - [Authorize(IdentityPermissions.Roles.ManageClaims)] - public virtual async Task AddClaimAsync(Guid id, IdentityRoleClaimCreateDto input) - { - var role = await IdentityRoleRepository.GetAsync(id); - var claim = new Claim(input.ClaimType, input.ClaimValue); - if (role.FindClaim(claim) != null) - { - throw new UserFriendlyException(L["RoleClaimAlreadyExists"]); - } - - role.AddClaim(GuidGenerator, claim); - await IdentityRoleRepository.UpdateAsync(role); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - [Authorize(IdentityPermissions.Roles.ManageClaims)] - public virtual async Task UpdateClaimAsync(Guid id, IdentityRoleClaimUpdateDto input) - { - var role = await IdentityRoleRepository.GetAsync(id); - var oldClaim = role.FindClaim(new Claim(input.ClaimType, input.ClaimValue)); - if (oldClaim != null) - { - role.RemoveClaim(oldClaim.ToClaim()); - role.AddClaim(GuidGenerator, new Claim(input.ClaimType, input.NewClaimValue)); - - await IdentityRoleRepository.UpdateAsync(role); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - } - - [Authorize(IdentityPermissions.Roles.ManageClaims)] - public virtual async Task DeleteClaimAsync(Guid id, IdentityRoleClaimDeleteDto input) - { - var role = await IdentityRoleRepository.GetAsync(id); - role.RemoveClaim(new Claim(input.ClaimType, input.ClaimValue)); - - await IdentityRoleRepository.UpdateAsync(role); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - #endregion - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Identity; + +namespace LINGYUN.Abp.Identity +{ + [Authorize(Volo.Abp.Identity.IdentityPermissions.Roles.Default)] + public class IdentityRoleAppService : IdentityAppServiceBase, IIdentityRoleAppService + { + protected IIdentityRoleRepository IdentityRoleRepository { get; } + protected OrganizationUnitManager OrganizationUnitManager { get; } + protected IOrganizationUnitRepository OrganizationUnitRepository { get; } + public IdentityRoleAppService( + IIdentityRoleRepository roleRepository, + OrganizationUnitManager organizationUnitManager) + { + OrganizationUnitManager = organizationUnitManager; + IdentityRoleRepository = roleRepository; + } + + #region OrganizationUnit + + [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] + public virtual async Task> GetOrganizationUnitsAsync(Guid id) + { + var origanizationUnits = await IdentityRoleRepository.GetOrganizationUnitsAsync(id); + + return new ListResultDto( + ObjectMapper.Map, List>(origanizationUnits)); + } + + [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] + public virtual async Task SetOrganizationUnitsAsync(Guid id, IdentityRoleAddOrRemoveOrganizationUnitDto input) + { + var origanizationUnits = await IdentityRoleRepository.GetOrganizationUnitsAsync(id, true); + + var notInRoleOuIds = input.OrganizationUnitIds.Where(ouid => !origanizationUnits.Any(ou => ou.Id.Equals(ouid))); + + foreach (var ouId in notInRoleOuIds) + { + await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(id, ouId); + } + + var removeRoleOriganzationUnits = origanizationUnits.Where(ou => !input.OrganizationUnitIds.Contains(ou.Id)); + foreach (var origanzationUnit in removeRoleOriganzationUnits) + { + origanzationUnit.RemoveRole(id); + } + + await CurrentUnitOfWork.CompleteAsync(); + } + + [Authorize(IdentityPermissions.Roles.ManageOrganizationUnits)] + public virtual async Task RemoveOrganizationUnitsAsync(Guid id, Guid ouId) + { + await OrganizationUnitManager.RemoveRoleFromOrganizationUnitAsync(id, ouId); + + await CurrentUnitOfWork.CompleteAsync(); + } + + #endregion + + #region ClaimType + + public virtual async Task> GetClaimsAsync(Guid id) + { + var role = await IdentityRoleRepository.GetAsync(id); + + return new ListResultDto(ObjectMapper.Map, List>(role.Claims)); + } + + [Authorize(IdentityPermissions.Roles.ManageClaims)] + public virtual async Task AddClaimAsync(Guid id, IdentityRoleClaimCreateDto input) + { + var role = await IdentityRoleRepository.GetAsync(id); + var claim = new Claim(input.ClaimType, input.ClaimValue); + if (role.FindClaim(claim) != null) + { + throw new UserFriendlyException(L["RoleClaimAlreadyExists"]); + } + + role.AddClaim(GuidGenerator, claim); + await IdentityRoleRepository.UpdateAsync(role); + + await CurrentUnitOfWork.CompleteAsync(); + } + + [Authorize(IdentityPermissions.Roles.ManageClaims)] + public virtual async Task UpdateClaimAsync(Guid id, IdentityRoleClaimUpdateDto input) + { + var role = await IdentityRoleRepository.GetAsync(id); + var oldClaim = role.FindClaim(new Claim(input.ClaimType, input.ClaimValue)); + if (oldClaim != null) + { + role.RemoveClaim(oldClaim.ToClaim()); + role.AddClaim(GuidGenerator, new Claim(input.ClaimType, input.NewClaimValue)); + + await IdentityRoleRepository.UpdateAsync(role); + + await CurrentUnitOfWork.CompleteAsync(); + } + } + + [Authorize(IdentityPermissions.Roles.ManageClaims)] + public virtual async Task DeleteClaimAsync(Guid id, IdentityRoleClaimDeleteDto input) + { + var role = await IdentityRoleRepository.GetAsync(id); + role.RemoveClaim(new Claim(input.ClaimType, input.ClaimValue)); + + await IdentityRoleRepository.UpdateAsync(role); + + await CurrentUnitOfWork.CompleteAsync(); + } + + #endregion + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs index 52d5068db..5736d7064 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs @@ -46,7 +46,7 @@ namespace LINGYUN.Abp.Identity await UserManager.SetOrganizationUnitsAsync(user, input.OrganizationUnitIds); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(IdentityPermissions.Users.ManageOrganizationUnits)] @@ -54,7 +54,7 @@ namespace LINGYUN.Abp.Identity { await UserManager.RemoveFromOrganizationUnitAsync(id, ouId); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } #endregion @@ -80,7 +80,7 @@ namespace LINGYUN.Abp.Identity user.AddClaim(GuidGenerator, claim); (await UserManager.UpdateAsync(user)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(IdentityPermissions.Users.ManageClaims)] @@ -92,7 +92,7 @@ namespace LINGYUN.Abp.Identity user.ReplaceClaim(oldClaim, newClaim); (await UserManager.UpdateAsync(user)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(IdentityPermissions.Users.ManageClaims)] @@ -102,7 +102,7 @@ namespace LINGYUN.Abp.Identity user.RemoveClaim(new Claim(input.ClaimType, input.ClaimValue)); (await UserManager.UpdateAsync(user)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } #endregion @@ -144,7 +144,7 @@ namespace LINGYUN.Abp.Identity (await UserManager.SetTwoFactorEnabledWithAccountConfirmedAsync(user, input.Enabled)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)] @@ -158,7 +158,7 @@ namespace LINGYUN.Abp.Identity var endDate = new DateTimeOffset(Clock.Now).AddSeconds(seconds); (await UserManager.SetLockoutEndDateAsync(user, endDate)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)] @@ -167,7 +167,7 @@ namespace LINGYUN.Abp.Identity var user = await GetUserAsync(id); (await UserManager.SetLockoutEndDateAsync(user, null)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } protected virtual async Task GetUserAsync(Guid id) diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs index 8f09f679e..a411f2953 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs @@ -48,7 +48,7 @@ namespace LINGYUN.Abp.Identity (await UserManager.SetTwoFactorEnabledWithAccountConfirmedAsync(user, input.Enabled)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } public virtual async Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input) @@ -94,7 +94,7 @@ namespace LINGYUN.Abp.Identity // 更换手机号 (await UserManager.ChangePhoneNumberAsync(user, input.NewPhoneNumber, input.Code)).CheckErrors(); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); var securityTokenCacheKey = SmsSecurityTokenCacheItem.CalculateCacheKey(input.NewPhoneNumber, "SmsChangePhoneNumber"); await SecurityTokenCache.RemoveAsync(securityTokenCacheKey); diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/OrganizationUnitAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/OrganizationUnitAppService.cs index f3c9a08ec..b3507cee2 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/OrganizationUnitAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/OrganizationUnitAppService.cs @@ -1,236 +1,236 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp.Application.Dtos; -using Volo.Abp.Identity; -using Volo.Abp.ObjectExtending; - -namespace LINGYUN.Abp.Identity -{ - [Authorize(IdentityPermissions.OrganizationUnits.Default)] - public class OrganizationUnitAppService : IdentityAppServiceBase, IOrganizationUnitAppService - { - protected OrganizationUnitManager OrganizationUnitManager { get; } - protected IOrganizationUnitRepository OrganizationUnitRepository { get; } - - protected IdentityUserManager UserManager { get; } - protected IIdentityRoleRepository RoleRepository { get; } - protected IIdentityUserRepository UserRepository { get; } - - public OrganizationUnitAppService( - IdentityUserManager userManager, - IIdentityRoleRepository roleRepository, - IIdentityUserRepository userRepository, - OrganizationUnitManager organizationUnitManager, - IOrganizationUnitRepository organizationUnitRepository) - { - UserManager = userManager; - RoleRepository = roleRepository; - UserRepository = userRepository; - OrganizationUnitManager = organizationUnitManager; - OrganizationUnitRepository = organizationUnitRepository; - - ObjectMapperContext = typeof(AbpIdentityApplicationModule); - } - - [Authorize(IdentityPermissions.OrganizationUnits.Create)] - public virtual async Task CreateAsync(OrganizationUnitCreateDto input) - { - var origanizationUnit = new OrganizationUnit( - GuidGenerator.Create(), input.DisplayName, input.ParentId, CurrentTenant.Id) - { - CreationTime = Clock.Now - }; - input.MapExtraPropertiesTo(origanizationUnit); - - await OrganizationUnitManager.CreateAsync(origanizationUnit); - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(origanizationUnit); - } - - [Authorize(IdentityPermissions.OrganizationUnits.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - var origanizationUnit = await OrganizationUnitRepository.FindAsync(id); - if (origanizationUnit == null) - { - return; - } - await OrganizationUnitManager.DeleteAsync(id); - } - - public virtual async Task> GetRootAsync() - { - var rootOriganizationUnits = await OrganizationUnitManager.FindChildrenAsync(null, recursive: false); - - return new ListResultDto( - ObjectMapper.Map, List>(rootOriganizationUnits)); - } - - public virtual async Task> FindChildrenAsync(OrganizationUnitGetChildrenDto input) - { - var origanizationUnitChildren = await OrganizationUnitManager.FindChildrenAsync(input.Id, input.Recursive); - - return new ListResultDto( - ObjectMapper.Map, List>(origanizationUnitChildren)); - } - - public virtual async Task GetAsync(Guid id) - { - var origanizationUnit = await OrganizationUnitRepository.FindAsync(id); - - return ObjectMapper.Map(origanizationUnit); - } - - public virtual async Task GetLastChildOrNullAsync(Guid? parentId) - { - var origanizationUnitLastChildren = await OrganizationUnitManager.GetLastChildOrNullAsync(parentId); - - return ObjectMapper.Map(origanizationUnitLastChildren); - } - - public virtual async Task> GetAllListAsync() - { - var origanizationUnits = await OrganizationUnitRepository.GetListAsync(false); - - return new ListResultDto( - ObjectMapper.Map, List>(origanizationUnits)); - } - - public virtual async Task> GetListAsync(OrganizationUnitGetByPagedDto input) - { - var origanizationUnitCount = await OrganizationUnitRepository.GetCountAsync(); - var origanizationUnits = await OrganizationUnitRepository - .GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, false); - - return new PagedResultDto(origanizationUnitCount, - ObjectMapper.Map, List>(origanizationUnits)); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] - public virtual async Task> GetRoleNamesAsync(Guid id) - { - var inOrignizationUnitRoleNames = await UserRepository.GetRoleNamesInOrganizationUnitAsync(id); - return new ListResultDto(inOrignizationUnitRoleNames); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] - public virtual async Task> GetUnaddedRolesAsync(Guid id, OrganizationUnitGetUnaddedRoleByPagedDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - - var origanizationUnitRoleCount = await OrganizationUnitRepository - .GetUnaddedRolesCountAsync(origanizationUnit, input.Filter); - - var origanizationUnitRoles = await OrganizationUnitRepository - .GetUnaddedRolesAsync(origanizationUnit, - input.Sorting, input.MaxResultCount, - input.SkipCount, input.Filter); - - return new PagedResultDto(origanizationUnitRoleCount, - ObjectMapper.Map, List>(origanizationUnitRoles)); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] - public virtual async Task> GetRolesAsync(Guid id, PagedAndSortedResultRequestDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - - var origanizationUnitRoleCount = await OrganizationUnitRepository - .GetRolesCountAsync(origanizationUnit); - - var origanizationUnitRoles = await OrganizationUnitRepository - .GetRolesAsync(origanizationUnit, - input.Sorting, input.MaxResultCount, - input.SkipCount); - - return new PagedResultDto(origanizationUnitRoleCount, - ObjectMapper.Map, List>(origanizationUnitRoles)); - } - - - [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] - public virtual async Task> GetUnaddedUsersAsync(Guid id, OrganizationUnitGetUnaddedUserByPagedDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - - var origanizationUnitUserCount = await OrganizationUnitRepository - .GetUnaddedUsersCountAsync(origanizationUnit, input.Filter); - var origanizationUnitUsers = await OrganizationUnitRepository - .GetUnaddedUsersAsync(origanizationUnit, - input.Sorting, input.MaxResultCount, - input.SkipCount, input.Filter); - - return new PagedResultDto(origanizationUnitUserCount, - ObjectMapper.Map, List>(origanizationUnitUsers)); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] - public virtual async Task> GetUsersAsync(Guid id, GetIdentityUsersInput input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - - var origanizationUnitUserCount = await OrganizationUnitRepository - .GetMembersCountAsync(origanizationUnit, input.Filter); - var origanizationUnitUsers = await OrganizationUnitRepository - .GetMembersAsync(origanizationUnit, - input.Sorting, input.MaxResultCount, - input.SkipCount, input.Filter); - - return new PagedResultDto(origanizationUnitUserCount, - ObjectMapper.Map, List>(origanizationUnitUsers)); - } - - [Authorize(IdentityPermissions.OrganizationUnits.Update)] - public virtual async Task MoveAsync(Guid id, OrganizationUnitMoveDto input) - { - await OrganizationUnitManager.MoveAsync(id, input.ParentId); - } - - [Authorize(IdentityPermissions.OrganizationUnits.Update)] - public virtual async Task UpdateAsync(Guid id, OrganizationUnitUpdateDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - origanizationUnit.DisplayName = input.DisplayName; - input.MapExtraPropertiesTo(origanizationUnit); - - await OrganizationUnitManager.UpdateAsync(origanizationUnit); - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(origanizationUnit); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] - public virtual async Task AddUsersAsync(Guid id, OrganizationUnitAddUserDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - var users = await UserRepository.GetListByIdListAsync(input.UserIds, includeDetails: true); - - // 调用内部方法设置用户组织机构 - foreach (var user in users) - { - await UserManager.AddToOrganizationUnitAsync(user, origanizationUnit); - } - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] - public virtual async Task AddRolesAsync(Guid id, OrganizationUnitAddRoleDto input) - { - var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); - - var roles = await RoleRepository.GetListByIdListAsync(input.RoleIds, includeDetails: true); - - foreach (var role in roles) - { - await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(role, origanizationUnit); - } - - await CurrentUnitOfWork.SaveChangesAsync(); - } - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Identity; +using Volo.Abp.ObjectExtending; + +namespace LINGYUN.Abp.Identity +{ + [Authorize(IdentityPermissions.OrganizationUnits.Default)] + public class OrganizationUnitAppService : IdentityAppServiceBase, IOrganizationUnitAppService + { + protected OrganizationUnitManager OrganizationUnitManager { get; } + protected IOrganizationUnitRepository OrganizationUnitRepository { get; } + + protected IdentityUserManager UserManager { get; } + protected IIdentityRoleRepository RoleRepository { get; } + protected IIdentityUserRepository UserRepository { get; } + + public OrganizationUnitAppService( + IdentityUserManager userManager, + IIdentityRoleRepository roleRepository, + IIdentityUserRepository userRepository, + OrganizationUnitManager organizationUnitManager, + IOrganizationUnitRepository organizationUnitRepository) + { + UserManager = userManager; + RoleRepository = roleRepository; + UserRepository = userRepository; + OrganizationUnitManager = organizationUnitManager; + OrganizationUnitRepository = organizationUnitRepository; + + ObjectMapperContext = typeof(AbpIdentityApplicationModule); + } + + [Authorize(IdentityPermissions.OrganizationUnits.Create)] + public virtual async Task CreateAsync(OrganizationUnitCreateDto input) + { + var origanizationUnit = new OrganizationUnit( + GuidGenerator.Create(), input.DisplayName, input.ParentId, CurrentTenant.Id) + { + CreationTime = Clock.Now + }; + input.MapExtraPropertiesTo(origanizationUnit); + + await OrganizationUnitManager.CreateAsync(origanizationUnit); + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(origanizationUnit); + } + + [Authorize(IdentityPermissions.OrganizationUnits.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + var origanizationUnit = await OrganizationUnitRepository.FindAsync(id); + if (origanizationUnit == null) + { + return; + } + await OrganizationUnitManager.DeleteAsync(id); + } + + public virtual async Task> GetRootAsync() + { + var rootOriganizationUnits = await OrganizationUnitManager.FindChildrenAsync(null, recursive: false); + + return new ListResultDto( + ObjectMapper.Map, List>(rootOriganizationUnits)); + } + + public virtual async Task> FindChildrenAsync(OrganizationUnitGetChildrenDto input) + { + var origanizationUnitChildren = await OrganizationUnitManager.FindChildrenAsync(input.Id, input.Recursive); + + return new ListResultDto( + ObjectMapper.Map, List>(origanizationUnitChildren)); + } + + public virtual async Task GetAsync(Guid id) + { + var origanizationUnit = await OrganizationUnitRepository.FindAsync(id); + + return ObjectMapper.Map(origanizationUnit); + } + + public virtual async Task GetLastChildOrNullAsync(Guid? parentId) + { + var origanizationUnitLastChildren = await OrganizationUnitManager.GetLastChildOrNullAsync(parentId); + + return ObjectMapper.Map(origanizationUnitLastChildren); + } + + public virtual async Task> GetAllListAsync() + { + var origanizationUnits = await OrganizationUnitRepository.GetListAsync(false); + + return new ListResultDto( + ObjectMapper.Map, List>(origanizationUnits)); + } + + public virtual async Task> GetListAsync(OrganizationUnitGetByPagedDto input) + { + var origanizationUnitCount = await OrganizationUnitRepository.GetCountAsync(); + var origanizationUnits = await OrganizationUnitRepository + .GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, false); + + return new PagedResultDto(origanizationUnitCount, + ObjectMapper.Map, List>(origanizationUnits)); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] + public virtual async Task> GetRoleNamesAsync(Guid id) + { + var inOrignizationUnitRoleNames = await UserRepository.GetRoleNamesInOrganizationUnitAsync(id); + return new ListResultDto(inOrignizationUnitRoleNames); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] + public virtual async Task> GetUnaddedRolesAsync(Guid id, OrganizationUnitGetUnaddedRoleByPagedDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + + var origanizationUnitRoleCount = await OrganizationUnitRepository + .GetUnaddedRolesCountAsync(origanizationUnit, input.Filter); + + var origanizationUnitRoles = await OrganizationUnitRepository + .GetUnaddedRolesAsync(origanizationUnit, + input.Sorting, input.MaxResultCount, + input.SkipCount, input.Filter); + + return new PagedResultDto(origanizationUnitRoleCount, + ObjectMapper.Map, List>(origanizationUnitRoles)); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] + public virtual async Task> GetRolesAsync(Guid id, PagedAndSortedResultRequestDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + + var origanizationUnitRoleCount = await OrganizationUnitRepository + .GetRolesCountAsync(origanizationUnit); + + var origanizationUnitRoles = await OrganizationUnitRepository + .GetRolesAsync(origanizationUnit, + input.Sorting, input.MaxResultCount, + input.SkipCount); + + return new PagedResultDto(origanizationUnitRoleCount, + ObjectMapper.Map, List>(origanizationUnitRoles)); + } + + + [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] + public virtual async Task> GetUnaddedUsersAsync(Guid id, OrganizationUnitGetUnaddedUserByPagedDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + + var origanizationUnitUserCount = await OrganizationUnitRepository + .GetUnaddedUsersCountAsync(origanizationUnit, input.Filter); + var origanizationUnitUsers = await OrganizationUnitRepository + .GetUnaddedUsersAsync(origanizationUnit, + input.Sorting, input.MaxResultCount, + input.SkipCount, input.Filter); + + return new PagedResultDto(origanizationUnitUserCount, + ObjectMapper.Map, List>(origanizationUnitUsers)); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] + public virtual async Task> GetUsersAsync(Guid id, GetIdentityUsersInput input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + + var origanizationUnitUserCount = await OrganizationUnitRepository + .GetMembersCountAsync(origanizationUnit, input.Filter); + var origanizationUnitUsers = await OrganizationUnitRepository + .GetMembersAsync(origanizationUnit, + input.Sorting, input.MaxResultCount, + input.SkipCount, input.Filter); + + return new PagedResultDto(origanizationUnitUserCount, + ObjectMapper.Map, List>(origanizationUnitUsers)); + } + + [Authorize(IdentityPermissions.OrganizationUnits.Update)] + public virtual async Task MoveAsync(Guid id, OrganizationUnitMoveDto input) + { + await OrganizationUnitManager.MoveAsync(id, input.ParentId); + } + + [Authorize(IdentityPermissions.OrganizationUnits.Update)] + public virtual async Task UpdateAsync(Guid id, OrganizationUnitUpdateDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + origanizationUnit.DisplayName = input.DisplayName; + input.MapExtraPropertiesTo(origanizationUnit); + + await OrganizationUnitManager.UpdateAsync(origanizationUnit); + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(origanizationUnit); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageUsers)] + public virtual async Task AddUsersAsync(Guid id, OrganizationUnitAddUserDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + var users = await UserRepository.GetListByIdListAsync(input.UserIds, includeDetails: true); + + // 调用内部方法设置用户组织机构 + foreach (var user in users) + { + await UserManager.AddToOrganizationUnitAsync(user, origanizationUnit); + } + + await CurrentUnitOfWork.CompleteAsync(); + } + + [Authorize(IdentityPermissions.OrganizationUnits.ManageRoles)] + public virtual async Task AddRolesAsync(Guid id, OrganizationUnitAddRoleDto input) + { + var origanizationUnit = await OrganizationUnitRepository.GetAsync(id); + + var roles = await RoleRepository.GetListByIdListAsync(input.RoleIds, includeDetails: true); + + foreach (var role in roles) + { + await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(role, origanizationUnit); + } + + await CurrentUnitOfWork.CompleteAsync(); + } + } +} diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiResources/ApiResourceAppService.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiResources/ApiResourceAppService.cs index a161b0908..e4ff62ec2 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiResources/ApiResourceAppService.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiResources/ApiResourceAppService.cs @@ -59,7 +59,7 @@ namespace LINGYUN.Abp.IdentityServer.ApiResources apiResource = await ApiResourceRepository.InsertAsync(apiResource); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(apiResource); } @@ -73,7 +73,7 @@ namespace LINGYUN.Abp.IdentityServer.ApiResources apiResource = await ApiResourceRepository.UpdateAsync(apiResource); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(apiResource); } @@ -84,7 +84,7 @@ namespace LINGYUN.Abp.IdentityServer.ApiResources var apiResource = await ApiResourceRepository.GetAsync(id); await ApiResourceRepository.DeleteAsync(apiResource); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } protected virtual async Task UpdateApiResourceByInputAsync(ApiResource apiResource, ApiResourceCreateOrUpdateDto input) diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiScopes/ApiScopeAppService.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiScopes/ApiScopeAppService.cs index 103ddd131..1ad1094d6 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiScopes/ApiScopeAppService.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/ApiScopes/ApiScopeAppService.cs @@ -1,142 +1,142 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Application.Dtos; -using Volo.Abp.IdentityServer.ApiScopes; - -namespace LINGYUN.Abp.IdentityServer.ApiScopes -{ - [Authorize(AbpIdentityServerPermissions.ApiScopes.Default)] - public class ApiScopeAppService : AbpIdentityServerAppServiceBase, IApiScopeAppService - { - protected IApiScopeRepository ApiScopeRepository { get; } - - public ApiScopeAppService( - IApiScopeRepository apiScopeRepository) - { - ApiScopeRepository = apiScopeRepository; - } - - [Authorize(AbpIdentityServerPermissions.ApiScopes.Create)] - public virtual async Task CreateAsync(ApiScopeCreateDto input) - { - if (await ApiScopeRepository.CheckNameExistAsync(input.Name)) - { - throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ApiScopeNameExisted, input.Name]); - } - var apiScope = new ApiScope( - GuidGenerator.Create(), - input.Name, - input.DisplayName, - input.Description, - input.Enabled, - input.Required, - input.Emphasize, - input.ShowInDiscoveryDocument); - - await UpdateApiScopeByInputAsync(apiScope, input); - - await CurrentUnitOfWork.SaveChangesAsync(); - - apiScope = await ApiScopeRepository.InsertAsync(apiScope); - - return ObjectMapper.Map(apiScope); - } - - [Authorize(AbpIdentityServerPermissions.ApiScopes.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - var apiScope = await ApiScopeRepository.GetAsync(id); - - await ApiScopeRepository.DeleteAsync(apiScope); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - public virtual async Task GetAsync(Guid id) - { - var apiScope = await ApiScopeRepository.GetAsync(id); - - return ObjectMapper.Map(apiScope); - } - - public virtual async Task> GetListAsync(GetApiScopeInput input) - { - var totalCount = await ApiScopeRepository - .GetCountAsync(input.Filter); - - var apiScopes = await ApiScopeRepository - .GetListAsync( - input.Sorting, - input.SkipCount, input.MaxResultCount, - input.Filter); - - return new PagedResultDto(totalCount, - ObjectMapper.Map, List>(apiScopes)); - } - - [Authorize(AbpIdentityServerPermissions.ApiScopes.Update)] - public virtual async Task UpdateAsync(Guid id, ApiScopeUpdateDto input) - { - var apiScope = await ApiScopeRepository.GetAsync(id); - - await UpdateApiScopeByInputAsync(apiScope, input); - apiScope = await ApiScopeRepository.UpdateAsync(apiScope); - - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(apiScope); - } - - protected virtual async Task UpdateApiScopeByInputAsync(ApiScope apiScope, ApiScopeCreateOrUpdateDto input) - { - if (!string.Equals(apiScope.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) - { - apiScope.Description = input.Description; - } - if (!string.Equals(apiScope.DisplayName, input.DisplayName, StringComparison.InvariantCultureIgnoreCase)) - { - apiScope.DisplayName = input.DisplayName; - } - apiScope.Emphasize = input.Emphasize; - apiScope.Enabled = input.Enabled; - apiScope.Required = input.Required; - apiScope.ShowInDiscoveryDocument = input.ShowInDiscoveryDocument; - - if (await IsGrantAsync(AbpIdentityServerPermissions.ApiScopes.ManageClaims)) - { - // 删除不存在的UserClaim - apiScope.UserClaims.RemoveAll(claim => !input.UserClaims.Any(inputClaim => claim.Type == inputClaim.Type)); - foreach (var inputClaim in input.UserClaims) - { - var userClaim = apiScope.FindClaim(inputClaim.Type); - if (userClaim == null) - { - apiScope.AddUserClaim(inputClaim.Type); - } - } - } - - if (await IsGrantAsync(AbpIdentityServerPermissions.ApiScopes.ManageProperties)) - { - // 删除不存在的Property - apiScope.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); - foreach (var inputProp in input.Properties) - { - var identityResourceProperty = apiScope.FindProperty(inputProp.Key); - if (identityResourceProperty == null) - { - apiScope.AddProperty(inputProp.Key, inputProp.Value); - } - else - { - identityResourceProperty.Value = inputProp.Value; - } - } - } - } - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Volo.Abp.IdentityServer.ApiScopes; + +namespace LINGYUN.Abp.IdentityServer.ApiScopes +{ + [Authorize(AbpIdentityServerPermissions.ApiScopes.Default)] + public class ApiScopeAppService : AbpIdentityServerAppServiceBase, IApiScopeAppService + { + protected IApiScopeRepository ApiScopeRepository { get; } + + public ApiScopeAppService( + IApiScopeRepository apiScopeRepository) + { + ApiScopeRepository = apiScopeRepository; + } + + [Authorize(AbpIdentityServerPermissions.ApiScopes.Create)] + public virtual async Task CreateAsync(ApiScopeCreateDto input) + { + if (await ApiScopeRepository.CheckNameExistAsync(input.Name)) + { + throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ApiScopeNameExisted, input.Name]); + } + var apiScope = new ApiScope( + GuidGenerator.Create(), + input.Name, + input.DisplayName, + input.Description, + input.Enabled, + input.Required, + input.Emphasize, + input.ShowInDiscoveryDocument); + + await UpdateApiScopeByInputAsync(apiScope, input); + + await CurrentUnitOfWork.CompleteAsync(); + + apiScope = await ApiScopeRepository.InsertAsync(apiScope); + + return ObjectMapper.Map(apiScope); + } + + [Authorize(AbpIdentityServerPermissions.ApiScopes.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + var apiScope = await ApiScopeRepository.GetAsync(id); + + await ApiScopeRepository.DeleteAsync(apiScope); + + await CurrentUnitOfWork.CompleteAsync(); + } + + public virtual async Task GetAsync(Guid id) + { + var apiScope = await ApiScopeRepository.GetAsync(id); + + return ObjectMapper.Map(apiScope); + } + + public virtual async Task> GetListAsync(GetApiScopeInput input) + { + var totalCount = await ApiScopeRepository + .GetCountAsync(input.Filter); + + var apiScopes = await ApiScopeRepository + .GetListAsync( + input.Sorting, + input.SkipCount, input.MaxResultCount, + input.Filter); + + return new PagedResultDto(totalCount, + ObjectMapper.Map, List>(apiScopes)); + } + + [Authorize(AbpIdentityServerPermissions.ApiScopes.Update)] + public virtual async Task UpdateAsync(Guid id, ApiScopeUpdateDto input) + { + var apiScope = await ApiScopeRepository.GetAsync(id); + + await UpdateApiScopeByInputAsync(apiScope, input); + apiScope = await ApiScopeRepository.UpdateAsync(apiScope); + + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(apiScope); + } + + protected virtual async Task UpdateApiScopeByInputAsync(ApiScope apiScope, ApiScopeCreateOrUpdateDto input) + { + if (!string.Equals(apiScope.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) + { + apiScope.Description = input.Description; + } + if (!string.Equals(apiScope.DisplayName, input.DisplayName, StringComparison.InvariantCultureIgnoreCase)) + { + apiScope.DisplayName = input.DisplayName; + } + apiScope.Emphasize = input.Emphasize; + apiScope.Enabled = input.Enabled; + apiScope.Required = input.Required; + apiScope.ShowInDiscoveryDocument = input.ShowInDiscoveryDocument; + + if (await IsGrantAsync(AbpIdentityServerPermissions.ApiScopes.ManageClaims)) + { + // 删除不存在的UserClaim + apiScope.UserClaims.RemoveAll(claim => !input.UserClaims.Any(inputClaim => claim.Type == inputClaim.Type)); + foreach (var inputClaim in input.UserClaims) + { + var userClaim = apiScope.FindClaim(inputClaim.Type); + if (userClaim == null) + { + apiScope.AddUserClaim(inputClaim.Type); + } + } + } + + if (await IsGrantAsync(AbpIdentityServerPermissions.ApiScopes.ManageProperties)) + { + // 删除不存在的Property + apiScope.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); + foreach (var inputProp in input.Properties) + { + var identityResourceProperty = apiScope.FindProperty(inputProp.Key); + if (identityResourceProperty == null) + { + apiScope.AddProperty(inputProp.Key, inputProp.Value); + } + else + { + identityResourceProperty.Value = inputProp.Value; + } + } + } + } + } +} diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Clients/ClientAppService.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Clients/ClientAppService.cs index eaae1c508..d2296136e 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Clients/ClientAppService.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Clients/ClientAppService.cs @@ -1,483 +1,483 @@ -using LINGYUN.Abp.IdentityServer.ApiResources; -using LINGYUN.Abp.IdentityServer.IdentityResources; -using IdentityServer4; -using IdentityServer4.Models; -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Application.Dtos; -using Volo.Abp.IdentityServer.Clients; -using Client = Volo.Abp.IdentityServer.Clients.Client; - -namespace LINGYUN.Abp.IdentityServer.Clients -{ - [Authorize(AbpIdentityServerPermissions.Clients.Default)] - public class ClientAppService : AbpIdentityServerAppServiceBase, IClientAppService - { - protected IClientRepository ClientRepository { get; } - protected IApiResourceRepository ApiResourceRepository { get; } - protected IIdentityResourceRepository IdentityResourceRepository { get; } - - public ClientAppService( - IClientRepository clientRepository, - IApiResourceRepository apiResourceRepository, - IIdentityResourceRepository identityResourceRepository) - { - ClientRepository = clientRepository; - ApiResourceRepository = apiResourceRepository; - IdentityResourceRepository = identityResourceRepository; - } - - [Authorize(AbpIdentityServerPermissions.Clients.Create)] - public virtual async Task CreateAsync(ClientCreateDto clientCreate) - { - var clientIdExists = await ClientRepository.CheckClientIdExistAsync(clientCreate.ClientId); - if(clientIdExists) - { - throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ClientIdExisted, clientCreate.ClientId]); - } - var client = new Client(GuidGenerator.Create(), clientCreate.ClientId) - { - ClientName = clientCreate.ClientName, - Description = clientCreate.Description - }; - foreach (var inputGrantType in clientCreate.AllowedGrantTypes) - { - client.AddGrantType(inputGrantType.GrantType); - } - - client = await ClientRepository.InsertAsync(client); - - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(client); - } - - [Authorize(AbpIdentityServerPermissions.Clients.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - var client = await ClientRepository.GetAsync(id); - await ClientRepository.DeleteAsync(client); - - await CurrentUnitOfWork.SaveChangesAsync(); - } - - public virtual async Task GetAsync(Guid id) - { - var client = await ClientRepository.GetAsync(id); - - return ObjectMapper.Map(client); - } - - public virtual async Task> GetListAsync(ClientGetByPagedDto input) - { - var clients = await ClientRepository.GetListAsync(input.Sorting, - input.SkipCount, input.MaxResultCount, - input.Filter); - - var clientCount = await ClientRepository.GetCountAsync(); - - return new PagedResultDto(clientCount, - ObjectMapper.Map, List>(clients)); - } - - [Authorize(AbpIdentityServerPermissions.Clients.Update)] - public virtual async Task UpdateAsync(Guid id, ClientUpdateDto input) - { - var client = await ClientRepository.GetAsync(id); - - #region Basic - if (!string.Equals(client.ClientId, input.ClientId, StringComparison.InvariantCultureIgnoreCase)) - { - client.ClientId = input.ClientId; - } - if (!string.Equals(client.ClientUri, input.ClientUri, StringComparison.InvariantCultureIgnoreCase)) - { - client.ClientUri = input.ClientUri; - } - if (!string.Equals(client.ClientName, input.ClientName, StringComparison.InvariantCultureIgnoreCase)) - { - client.ClientName = input.ClientName; - } - if (!string.Equals(client.BackChannelLogoutUri, input.BackChannelLogoutUri, StringComparison.InvariantCultureIgnoreCase)) - { - client.BackChannelLogoutUri = input.BackChannelLogoutUri; - } - if (!string.Equals(client.FrontChannelLogoutUri, input.FrontChannelLogoutUri, StringComparison.InvariantCultureIgnoreCase)) - { - client.FrontChannelLogoutUri = input.FrontChannelLogoutUri; - } - if (!string.Equals(client.ClientClaimsPrefix, input.ClientClaimsPrefix, StringComparison.InvariantCultureIgnoreCase)) - { - client.ClientClaimsPrefix = input.ClientClaimsPrefix; - } - if (!string.Equals(client.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) - { - client.Description = input.Description; - } - if (!string.Equals(client.LogoUri, input.LogoUri, StringComparison.InvariantCultureIgnoreCase)) - { - client.LogoUri = input.LogoUri; - } - if (!string.Equals(client.UserCodeType, input.UserCodeType, StringComparison.InvariantCultureIgnoreCase)) - { - client.UserCodeType = input.UserCodeType; - } - if (!string.Equals(client.PairWiseSubjectSalt, input.PairWiseSubjectSalt, StringComparison.InvariantCultureIgnoreCase)) - { - client.PairWiseSubjectSalt = input.PairWiseSubjectSalt; - } - if (!string.Equals(client.ProtocolType, input.ProtocolType, StringComparison.InvariantCultureIgnoreCase)) - { - client.ProtocolType = input.ProtocolType; - } - if (!string.Equals(client.AllowedIdentityTokenSigningAlgorithms, input.AllowedIdentityTokenSigningAlgorithms, StringComparison.InvariantCultureIgnoreCase)) - { - client.AllowedIdentityTokenSigningAlgorithms = input.AllowedIdentityTokenSigningAlgorithms; - } - - client.AbsoluteRefreshTokenLifetime = input.AbsoluteRefreshTokenLifetime; - client.AccessTokenLifetime = input.AccessTokenLifetime; - client.AccessTokenType = input.AccessTokenType; - client.AllowAccessTokensViaBrowser = input.AllowAccessTokensViaBrowser; - client.AllowOfflineAccess = input.AllowOfflineAccess; - client.AllowPlainTextPkce = input.AllowPlainTextPkce; - client.AllowRememberConsent = input.AllowRememberConsent; - client.AlwaysIncludeUserClaimsInIdToken = input.AlwaysIncludeUserClaimsInIdToken; - client.AlwaysSendClientClaims = input.AlwaysSendClientClaims; - client.AuthorizationCodeLifetime = input.AuthorizationCodeLifetime; - client.BackChannelLogoutSessionRequired = input.BackChannelLogoutSessionRequired; - client.DeviceCodeLifetime = input.DeviceCodeLifetime; - client.ConsentLifetime = input.ConsentLifetime ?? client.ConsentLifetime; - client.Enabled = input.Enabled; - client.RequireRequestObject = input.RequireRequestObject; - client.EnableLocalLogin = input.EnableLocalLogin; - client.FrontChannelLogoutSessionRequired = input.FrontChannelLogoutSessionRequired; - client.IdentityTokenLifetime = input.IdentityTokenLifetime; - client.IncludeJwtId = input.IncludeJwtId; - client.RefreshTokenExpiration = input.RefreshTokenExpiration; - client.RefreshTokenUsage = input.RefreshTokenUsage; - client.RequireClientSecret = input.RequireClientSecret; - client.RequireConsent = input.RequireConsent; - client.RequirePkce = input.RequirePkce; - client.SlidingRefreshTokenLifetime = input.SlidingRefreshTokenLifetime; - client.UpdateAccessTokenClaimsOnRefresh = input.UpdateAccessTokenClaimsOnRefresh; - client.UserSsoLifetime = input.UserSsoLifetime ?? client.UserSsoLifetime; - #endregion - - #region AllowScope - // 删除未在身份资源和Api资源中的作用域 - client.AllowedScopes.RemoveAll(scope => !input.AllowedScopes.Any(inputScope => scope.Scope == inputScope.Scope)); - foreach (var inputScope in input.AllowedScopes) - { - if (client.FindScope(inputScope.Scope) == null) - { - client.AddScope(inputScope.Scope); - } - } - - #endregion - - #region RedirectUris - // 删除不存在的uri - client.RedirectUris.RemoveAll(uri => !input.RedirectUris.Any(inputRedirectUri => uri.RedirectUri == inputRedirectUri.RedirectUri)); - foreach (var inputRedirectUri in input.RedirectUris) - { - if (client.FindRedirectUri(inputRedirectUri.RedirectUri) == null) - { - client.AddRedirectUri(inputRedirectUri.RedirectUri); - } - } - - #endregion - - #region AllowedGrantTypes - // 删除不存在的验证类型 - client.AllowedGrantTypes.RemoveAll(grantType => !input.AllowedGrantTypes.Any(inputGrantType => grantType.GrantType == inputGrantType.GrantType)); - foreach (var inputGrantType in input.AllowedGrantTypes) - { - if (client.FindGrantType(inputGrantType.GrantType) == null) - { - client.AddGrantType(inputGrantType.GrantType); - } - } - - #endregion - - #region AllowedCorsOrigins - // 删除不存在的同源域名 - client.AllowedCorsOrigins.RemoveAll(corsOrigin => !input.AllowedCorsOrigins.Any(inputCorsOrigin => corsOrigin.Origin == inputCorsOrigin.Origin)); - foreach (var inputCorsOrigin in input.AllowedCorsOrigins) - { - if (client.FindCorsOrigin(inputCorsOrigin.Origin) == null) - { - client.AddCorsOrigin(inputCorsOrigin.Origin); - } - } - - #endregion - - #region PostLogoutRedirectUris - - // 删除不存在的登录重定向域名 - client.PostLogoutRedirectUris.RemoveAll(uri => - !input.PostLogoutRedirectUris.Any(inputLogoutRedirectUri => uri.PostLogoutRedirectUri == inputLogoutRedirectUri.PostLogoutRedirectUri)); - foreach (var inputLogoutRedirectUri in input.PostLogoutRedirectUris) - { - if (client.FindPostLogoutRedirectUri(inputLogoutRedirectUri.PostLogoutRedirectUri) == null) - { - client.AddPostLogoutRedirectUri(inputLogoutRedirectUri.PostLogoutRedirectUri); - } - } - - #endregion - - #region IdentityProviderRestrictions - - // 删除身份认证限制提供商 - client.IdentityProviderRestrictions.RemoveAll(provider => - !input.IdentityProviderRestrictions.Any(inputProvider => provider.Provider == inputProvider.Provider)); - foreach (var inputProvider in input.IdentityProviderRestrictions) - { - if (client.FindIdentityProviderRestriction(inputProvider.Provider) == null) - { - client.AddIdentityProviderRestriction(inputProvider.Provider); - } - } - - #endregion - - #region Secrets - - if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageSecrets)) - { - // 移除已经不存在的客户端密钥 - client.ClientSecrets.RemoveAll(secret => !input.ClientSecrets.Any(inputSecret => secret.Value == inputSecret.Value && secret.Type == inputSecret.Type)); - foreach (var inputSecret in input.ClientSecrets) - { - // 先对加密过的进行过滤 - if (client.FindSecret(inputSecret.Value, inputSecret.Type) != null) - { - continue; - } - var inputSecretValue = inputSecret.Value.Sha256(); // TODO: 通过可选配置来加密 - - var clientSecret = client.FindSecret(inputSecretValue, inputSecret.Type); - if (clientSecret == null) - { - client.AddSecret(inputSecretValue, inputSecret.Expiration, inputSecret.Type, inputSecret.Description); - } - } - } - - #endregion - - #region Properties - - if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageProperties)) - { - // 移除不存在的属性 - client.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); - foreach (var inputProp in input.Properties) - { - if (client.FindProperty(inputProp.Key, inputProp.Value) == null) - { - client.AddProperty(inputProp.Key, inputProp.Value); - } - } - } - - - #endregion - - #region Claims - - if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageClaims)) - { - // 移除已经不存在的客户端声明 - client.Claims.RemoveAll(secret => !input.Claims.Any(inputClaim => secret.Value == inputClaim.Value && secret.Type == inputClaim.Type)); - foreach (var inputClaim in input.Claims) - { - if (client.FindClaim(inputClaim.Value, inputClaim.Type) == null) - { - client.AddClaim(inputClaim.Value, inputClaim.Type); - } - } - } - - #endregion - - client = await ClientRepository.UpdateAsync(client); - - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(client); - } - - /// - /// 克隆客户端 - /// - /// - /// 实现参考 Skoruba.IdentityServer4.Admin 项目 - /// https://github.com/skoruba/IdentityServer4.Admin.git - /// - /// - /// - /// - [Authorize(AbpIdentityServerPermissions.Clients.Clone)] - public virtual async Task CloneAsync(Guid id, ClientCloneDto input) - { - var clientIdExists = await ClientRepository.CheckClientIdExistAsync(input.ClientId); - if (clientIdExists) - { - throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ClientIdExisted, input.ClientId]); - } - var srcClient = await ClientRepository.GetAsync(id); - - var client = new Client(GuidGenerator.Create(), input.ClientId) - { - ClientName = input.ClientName, - Description = input.Description, - AbsoluteRefreshTokenLifetime = srcClient.AbsoluteRefreshTokenLifetime, - AccessTokenLifetime = srcClient.AccessTokenLifetime, - AccessTokenType = srcClient.AccessTokenType, - AllowAccessTokensViaBrowser = srcClient.AllowAccessTokensViaBrowser, - AllowOfflineAccess = srcClient.AllowOfflineAccess, - AllowPlainTextPkce = srcClient.AllowPlainTextPkce, - AllowRememberConsent = srcClient.AllowRememberConsent, - AlwaysIncludeUserClaimsInIdToken = srcClient.AlwaysIncludeUserClaimsInIdToken, - AlwaysSendClientClaims = srcClient.AlwaysSendClientClaims, - AuthorizationCodeLifetime = srcClient.AuthorizationCodeLifetime, - BackChannelLogoutSessionRequired = srcClient.BackChannelLogoutSessionRequired, - - BackChannelLogoutUri = srcClient.BackChannelLogoutUri, - ClientClaimsPrefix = srcClient.ClientClaimsPrefix, - ConsentLifetime = srcClient.ConsentLifetime, - DeviceCodeLifetime = srcClient.DeviceCodeLifetime, - Enabled = srcClient.Enabled, - EnableLocalLogin = srcClient.EnableLocalLogin, - FrontChannelLogoutSessionRequired = srcClient.FrontChannelLogoutSessionRequired, - FrontChannelLogoutUri = srcClient.FrontChannelLogoutUri, - - IdentityTokenLifetime = srcClient.IdentityTokenLifetime, - IncludeJwtId = srcClient.IncludeJwtId, - LogoUri = srcClient.LogoUri, - PairWiseSubjectSalt = srcClient.PairWiseSubjectSalt, - ProtocolType = srcClient.ProtocolType, - RefreshTokenExpiration = srcClient.RefreshTokenExpiration, - RefreshTokenUsage = srcClient.RefreshTokenUsage, - RequireClientSecret = srcClient.RequireClientSecret, - RequireConsent = srcClient.RequireConsent, - - RequirePkce = srcClient.RequirePkce, - SlidingRefreshTokenLifetime = srcClient.SlidingRefreshTokenLifetime, - UpdateAccessTokenClaimsOnRefresh = srcClient.UpdateAccessTokenClaimsOnRefresh, - - UserCodeType = srcClient.UserCodeType, - UserSsoLifetime = srcClient.UserSsoLifetime - }; - - if (input.CopyAllowedCorsOrigin) - { - foreach(var corsOrigin in srcClient.AllowedCorsOrigins) - { - client.AddCorsOrigin(corsOrigin.Origin); - } - } - if (input.CopyAllowedGrantType) - { - foreach (var grantType in srcClient.AllowedGrantTypes) - { - client.AddGrantType(grantType.GrantType); - } - } - if (input.CopyAllowedScope) - { - foreach (var scope in srcClient.AllowedScopes) - { - client.AddScope(scope.Scope); - } - } - if (input.CopyClaim) - { - foreach (var claim in srcClient.Claims) - { - client.AddClaim(claim.Value, claim.Type); - } - } - if (input.CopySecret) - { - foreach (var secret in srcClient.ClientSecrets) - { - client.AddSecret(secret.Value, secret.Expiration, secret.Type, secret.Description); - } - } - if (input.CopyIdentityProviderRestriction) - { - foreach (var provider in srcClient.IdentityProviderRestrictions) - { - client.AddIdentityProviderRestriction(provider.Provider); - } - } - if (input.CopyPostLogoutRedirectUri) - { - foreach (var uri in srcClient.PostLogoutRedirectUris) - { - client.AddPostLogoutRedirectUri(uri.PostLogoutRedirectUri); - } - } - if (input.CopyPropertie) - { - foreach (var property in srcClient.Properties) - { - client.AddProperty(property.Key, property.Value); - } - } - if (input.CopyRedirectUri) - { - foreach (var uri in srcClient.RedirectUris) - { - client.AddRedirectUri(uri.RedirectUri); - } - } - client = await ClientRepository.InsertAsync(client); - - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(client); - } - /// - /// 查询可用的Api资源 - /// - /// - public virtual async Task> GetAssignableApiResourcesAsync() - { - var resourceNames = await ApiResourceRepository.GetNamesAsync(); - - return new ListResultDto(resourceNames); - } - /// - /// 查询可用的身份资源 - /// - /// - public virtual async Task> GetAssignableIdentityResourcesAsync() - { - var resourceNames = await IdentityResourceRepository.GetNamesAsync(); - - return new ListResultDto(resourceNames); - } - /// - /// 查询所有不重复的跨域地址 - /// - /// - public virtual async Task> GetAllDistinctAllowedCorsOriginsAsync() - { - var corsOrigins = await ClientRepository.GetAllDistinctAllowedCorsOriginsAsync(); - - return new ListResultDto(corsOrigins); - } - } -} +using LINGYUN.Abp.IdentityServer.ApiResources; +using LINGYUN.Abp.IdentityServer.IdentityResources; +using IdentityServer4; +using IdentityServer4.Models; +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Volo.Abp.IdentityServer.Clients; +using Client = Volo.Abp.IdentityServer.Clients.Client; + +namespace LINGYUN.Abp.IdentityServer.Clients +{ + [Authorize(AbpIdentityServerPermissions.Clients.Default)] + public class ClientAppService : AbpIdentityServerAppServiceBase, IClientAppService + { + protected IClientRepository ClientRepository { get; } + protected IApiResourceRepository ApiResourceRepository { get; } + protected IIdentityResourceRepository IdentityResourceRepository { get; } + + public ClientAppService( + IClientRepository clientRepository, + IApiResourceRepository apiResourceRepository, + IIdentityResourceRepository identityResourceRepository) + { + ClientRepository = clientRepository; + ApiResourceRepository = apiResourceRepository; + IdentityResourceRepository = identityResourceRepository; + } + + [Authorize(AbpIdentityServerPermissions.Clients.Create)] + public virtual async Task CreateAsync(ClientCreateDto clientCreate) + { + var clientIdExists = await ClientRepository.CheckClientIdExistAsync(clientCreate.ClientId); + if(clientIdExists) + { + throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ClientIdExisted, clientCreate.ClientId]); + } + var client = new Client(GuidGenerator.Create(), clientCreate.ClientId) + { + ClientName = clientCreate.ClientName, + Description = clientCreate.Description + }; + foreach (var inputGrantType in clientCreate.AllowedGrantTypes) + { + client.AddGrantType(inputGrantType.GrantType); + } + + client = await ClientRepository.InsertAsync(client); + + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(client); + } + + [Authorize(AbpIdentityServerPermissions.Clients.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + var client = await ClientRepository.GetAsync(id); + await ClientRepository.DeleteAsync(client); + + await CurrentUnitOfWork.CompleteAsync(); + } + + public virtual async Task GetAsync(Guid id) + { + var client = await ClientRepository.GetAsync(id); + + return ObjectMapper.Map(client); + } + + public virtual async Task> GetListAsync(ClientGetByPagedDto input) + { + var clients = await ClientRepository.GetListAsync(input.Sorting, + input.SkipCount, input.MaxResultCount, + input.Filter); + + var clientCount = await ClientRepository.GetCountAsync(); + + return new PagedResultDto(clientCount, + ObjectMapper.Map, List>(clients)); + } + + [Authorize(AbpIdentityServerPermissions.Clients.Update)] + public virtual async Task UpdateAsync(Guid id, ClientUpdateDto input) + { + var client = await ClientRepository.GetAsync(id); + + #region Basic + if (!string.Equals(client.ClientId, input.ClientId, StringComparison.InvariantCultureIgnoreCase)) + { + client.ClientId = input.ClientId; + } + if (!string.Equals(client.ClientUri, input.ClientUri, StringComparison.InvariantCultureIgnoreCase)) + { + client.ClientUri = input.ClientUri; + } + if (!string.Equals(client.ClientName, input.ClientName, StringComparison.InvariantCultureIgnoreCase)) + { + client.ClientName = input.ClientName; + } + if (!string.Equals(client.BackChannelLogoutUri, input.BackChannelLogoutUri, StringComparison.InvariantCultureIgnoreCase)) + { + client.BackChannelLogoutUri = input.BackChannelLogoutUri; + } + if (!string.Equals(client.FrontChannelLogoutUri, input.FrontChannelLogoutUri, StringComparison.InvariantCultureIgnoreCase)) + { + client.FrontChannelLogoutUri = input.FrontChannelLogoutUri; + } + if (!string.Equals(client.ClientClaimsPrefix, input.ClientClaimsPrefix, StringComparison.InvariantCultureIgnoreCase)) + { + client.ClientClaimsPrefix = input.ClientClaimsPrefix; + } + if (!string.Equals(client.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) + { + client.Description = input.Description; + } + if (!string.Equals(client.LogoUri, input.LogoUri, StringComparison.InvariantCultureIgnoreCase)) + { + client.LogoUri = input.LogoUri; + } + if (!string.Equals(client.UserCodeType, input.UserCodeType, StringComparison.InvariantCultureIgnoreCase)) + { + client.UserCodeType = input.UserCodeType; + } + if (!string.Equals(client.PairWiseSubjectSalt, input.PairWiseSubjectSalt, StringComparison.InvariantCultureIgnoreCase)) + { + client.PairWiseSubjectSalt = input.PairWiseSubjectSalt; + } + if (!string.Equals(client.ProtocolType, input.ProtocolType, StringComparison.InvariantCultureIgnoreCase)) + { + client.ProtocolType = input.ProtocolType; + } + if (!string.Equals(client.AllowedIdentityTokenSigningAlgorithms, input.AllowedIdentityTokenSigningAlgorithms, StringComparison.InvariantCultureIgnoreCase)) + { + client.AllowedIdentityTokenSigningAlgorithms = input.AllowedIdentityTokenSigningAlgorithms; + } + + client.AbsoluteRefreshTokenLifetime = input.AbsoluteRefreshTokenLifetime; + client.AccessTokenLifetime = input.AccessTokenLifetime; + client.AccessTokenType = input.AccessTokenType; + client.AllowAccessTokensViaBrowser = input.AllowAccessTokensViaBrowser; + client.AllowOfflineAccess = input.AllowOfflineAccess; + client.AllowPlainTextPkce = input.AllowPlainTextPkce; + client.AllowRememberConsent = input.AllowRememberConsent; + client.AlwaysIncludeUserClaimsInIdToken = input.AlwaysIncludeUserClaimsInIdToken; + client.AlwaysSendClientClaims = input.AlwaysSendClientClaims; + client.AuthorizationCodeLifetime = input.AuthorizationCodeLifetime; + client.BackChannelLogoutSessionRequired = input.BackChannelLogoutSessionRequired; + client.DeviceCodeLifetime = input.DeviceCodeLifetime; + client.ConsentLifetime = input.ConsentLifetime ?? client.ConsentLifetime; + client.Enabled = input.Enabled; + client.RequireRequestObject = input.RequireRequestObject; + client.EnableLocalLogin = input.EnableLocalLogin; + client.FrontChannelLogoutSessionRequired = input.FrontChannelLogoutSessionRequired; + client.IdentityTokenLifetime = input.IdentityTokenLifetime; + client.IncludeJwtId = input.IncludeJwtId; + client.RefreshTokenExpiration = input.RefreshTokenExpiration; + client.RefreshTokenUsage = input.RefreshTokenUsage; + client.RequireClientSecret = input.RequireClientSecret; + client.RequireConsent = input.RequireConsent; + client.RequirePkce = input.RequirePkce; + client.SlidingRefreshTokenLifetime = input.SlidingRefreshTokenLifetime; + client.UpdateAccessTokenClaimsOnRefresh = input.UpdateAccessTokenClaimsOnRefresh; + client.UserSsoLifetime = input.UserSsoLifetime ?? client.UserSsoLifetime; + #endregion + + #region AllowScope + // 删除未在身份资源和Api资源中的作用域 + client.AllowedScopes.RemoveAll(scope => !input.AllowedScopes.Any(inputScope => scope.Scope == inputScope.Scope)); + foreach (var inputScope in input.AllowedScopes) + { + if (client.FindScope(inputScope.Scope) == null) + { + client.AddScope(inputScope.Scope); + } + } + + #endregion + + #region RedirectUris + // 删除不存在的uri + client.RedirectUris.RemoveAll(uri => !input.RedirectUris.Any(inputRedirectUri => uri.RedirectUri == inputRedirectUri.RedirectUri)); + foreach (var inputRedirectUri in input.RedirectUris) + { + if (client.FindRedirectUri(inputRedirectUri.RedirectUri) == null) + { + client.AddRedirectUri(inputRedirectUri.RedirectUri); + } + } + + #endregion + + #region AllowedGrantTypes + // 删除不存在的验证类型 + client.AllowedGrantTypes.RemoveAll(grantType => !input.AllowedGrantTypes.Any(inputGrantType => grantType.GrantType == inputGrantType.GrantType)); + foreach (var inputGrantType in input.AllowedGrantTypes) + { + if (client.FindGrantType(inputGrantType.GrantType) == null) + { + client.AddGrantType(inputGrantType.GrantType); + } + } + + #endregion + + #region AllowedCorsOrigins + // 删除不存在的同源域名 + client.AllowedCorsOrigins.RemoveAll(corsOrigin => !input.AllowedCorsOrigins.Any(inputCorsOrigin => corsOrigin.Origin == inputCorsOrigin.Origin)); + foreach (var inputCorsOrigin in input.AllowedCorsOrigins) + { + if (client.FindCorsOrigin(inputCorsOrigin.Origin) == null) + { + client.AddCorsOrigin(inputCorsOrigin.Origin); + } + } + + #endregion + + #region PostLogoutRedirectUris + + // 删除不存在的登录重定向域名 + client.PostLogoutRedirectUris.RemoveAll(uri => + !input.PostLogoutRedirectUris.Any(inputLogoutRedirectUri => uri.PostLogoutRedirectUri == inputLogoutRedirectUri.PostLogoutRedirectUri)); + foreach (var inputLogoutRedirectUri in input.PostLogoutRedirectUris) + { + if (client.FindPostLogoutRedirectUri(inputLogoutRedirectUri.PostLogoutRedirectUri) == null) + { + client.AddPostLogoutRedirectUri(inputLogoutRedirectUri.PostLogoutRedirectUri); + } + } + + #endregion + + #region IdentityProviderRestrictions + + // 删除身份认证限制提供商 + client.IdentityProviderRestrictions.RemoveAll(provider => + !input.IdentityProviderRestrictions.Any(inputProvider => provider.Provider == inputProvider.Provider)); + foreach (var inputProvider in input.IdentityProviderRestrictions) + { + if (client.FindIdentityProviderRestriction(inputProvider.Provider) == null) + { + client.AddIdentityProviderRestriction(inputProvider.Provider); + } + } + + #endregion + + #region Secrets + + if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageSecrets)) + { + // 移除已经不存在的客户端密钥 + client.ClientSecrets.RemoveAll(secret => !input.ClientSecrets.Any(inputSecret => secret.Value == inputSecret.Value && secret.Type == inputSecret.Type)); + foreach (var inputSecret in input.ClientSecrets) + { + // 先对加密过的进行过滤 + if (client.FindSecret(inputSecret.Value, inputSecret.Type) != null) + { + continue; + } + var inputSecretValue = inputSecret.Value.Sha256(); // TODO: 通过可选配置来加密 + + var clientSecret = client.FindSecret(inputSecretValue, inputSecret.Type); + if (clientSecret == null) + { + client.AddSecret(inputSecretValue, inputSecret.Expiration, inputSecret.Type, inputSecret.Description); + } + } + } + + #endregion + + #region Properties + + if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageProperties)) + { + // 移除不存在的属性 + client.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); + foreach (var inputProp in input.Properties) + { + if (client.FindProperty(inputProp.Key, inputProp.Value) == null) + { + client.AddProperty(inputProp.Key, inputProp.Value); + } + } + } + + + #endregion + + #region Claims + + if (await IsGrantAsync(AbpIdentityServerPermissions.Clients.ManageClaims)) + { + // 移除已经不存在的客户端声明 + client.Claims.RemoveAll(secret => !input.Claims.Any(inputClaim => secret.Value == inputClaim.Value && secret.Type == inputClaim.Type)); + foreach (var inputClaim in input.Claims) + { + if (client.FindClaim(inputClaim.Value, inputClaim.Type) == null) + { + client.AddClaim(inputClaim.Value, inputClaim.Type); + } + } + } + + #endregion + + client = await ClientRepository.UpdateAsync(client); + + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(client); + } + + /// + /// 克隆客户端 + /// + /// + /// 实现参考 Skoruba.IdentityServer4.Admin 项目 + /// https://github.com/skoruba/IdentityServer4.Admin.git + /// + /// + /// + /// + [Authorize(AbpIdentityServerPermissions.Clients.Clone)] + public virtual async Task CloneAsync(Guid id, ClientCloneDto input) + { + var clientIdExists = await ClientRepository.CheckClientIdExistAsync(input.ClientId); + if (clientIdExists) + { + throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.ClientIdExisted, input.ClientId]); + } + var srcClient = await ClientRepository.GetAsync(id); + + var client = new Client(GuidGenerator.Create(), input.ClientId) + { + ClientName = input.ClientName, + Description = input.Description, + AbsoluteRefreshTokenLifetime = srcClient.AbsoluteRefreshTokenLifetime, + AccessTokenLifetime = srcClient.AccessTokenLifetime, + AccessTokenType = srcClient.AccessTokenType, + AllowAccessTokensViaBrowser = srcClient.AllowAccessTokensViaBrowser, + AllowOfflineAccess = srcClient.AllowOfflineAccess, + AllowPlainTextPkce = srcClient.AllowPlainTextPkce, + AllowRememberConsent = srcClient.AllowRememberConsent, + AlwaysIncludeUserClaimsInIdToken = srcClient.AlwaysIncludeUserClaimsInIdToken, + AlwaysSendClientClaims = srcClient.AlwaysSendClientClaims, + AuthorizationCodeLifetime = srcClient.AuthorizationCodeLifetime, + BackChannelLogoutSessionRequired = srcClient.BackChannelLogoutSessionRequired, + + BackChannelLogoutUri = srcClient.BackChannelLogoutUri, + ClientClaimsPrefix = srcClient.ClientClaimsPrefix, + ConsentLifetime = srcClient.ConsentLifetime, + DeviceCodeLifetime = srcClient.DeviceCodeLifetime, + Enabled = srcClient.Enabled, + EnableLocalLogin = srcClient.EnableLocalLogin, + FrontChannelLogoutSessionRequired = srcClient.FrontChannelLogoutSessionRequired, + FrontChannelLogoutUri = srcClient.FrontChannelLogoutUri, + + IdentityTokenLifetime = srcClient.IdentityTokenLifetime, + IncludeJwtId = srcClient.IncludeJwtId, + LogoUri = srcClient.LogoUri, + PairWiseSubjectSalt = srcClient.PairWiseSubjectSalt, + ProtocolType = srcClient.ProtocolType, + RefreshTokenExpiration = srcClient.RefreshTokenExpiration, + RefreshTokenUsage = srcClient.RefreshTokenUsage, + RequireClientSecret = srcClient.RequireClientSecret, + RequireConsent = srcClient.RequireConsent, + + RequirePkce = srcClient.RequirePkce, + SlidingRefreshTokenLifetime = srcClient.SlidingRefreshTokenLifetime, + UpdateAccessTokenClaimsOnRefresh = srcClient.UpdateAccessTokenClaimsOnRefresh, + + UserCodeType = srcClient.UserCodeType, + UserSsoLifetime = srcClient.UserSsoLifetime + }; + + if (input.CopyAllowedCorsOrigin) + { + foreach(var corsOrigin in srcClient.AllowedCorsOrigins) + { + client.AddCorsOrigin(corsOrigin.Origin); + } + } + if (input.CopyAllowedGrantType) + { + foreach (var grantType in srcClient.AllowedGrantTypes) + { + client.AddGrantType(grantType.GrantType); + } + } + if (input.CopyAllowedScope) + { + foreach (var scope in srcClient.AllowedScopes) + { + client.AddScope(scope.Scope); + } + } + if (input.CopyClaim) + { + foreach (var claim in srcClient.Claims) + { + client.AddClaim(claim.Value, claim.Type); + } + } + if (input.CopySecret) + { + foreach (var secret in srcClient.ClientSecrets) + { + client.AddSecret(secret.Value, secret.Expiration, secret.Type, secret.Description); + } + } + if (input.CopyIdentityProviderRestriction) + { + foreach (var provider in srcClient.IdentityProviderRestrictions) + { + client.AddIdentityProviderRestriction(provider.Provider); + } + } + if (input.CopyPostLogoutRedirectUri) + { + foreach (var uri in srcClient.PostLogoutRedirectUris) + { + client.AddPostLogoutRedirectUri(uri.PostLogoutRedirectUri); + } + } + if (input.CopyPropertie) + { + foreach (var property in srcClient.Properties) + { + client.AddProperty(property.Key, property.Value); + } + } + if (input.CopyRedirectUri) + { + foreach (var uri in srcClient.RedirectUris) + { + client.AddRedirectUri(uri.RedirectUri); + } + } + client = await ClientRepository.InsertAsync(client); + + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(client); + } + /// + /// 查询可用的Api资源 + /// + /// + public virtual async Task> GetAssignableApiResourcesAsync() + { + var resourceNames = await ApiResourceRepository.GetNamesAsync(); + + return new ListResultDto(resourceNames); + } + /// + /// 查询可用的身份资源 + /// + /// + public virtual async Task> GetAssignableIdentityResourcesAsync() + { + var resourceNames = await IdentityResourceRepository.GetNamesAsync(); + + return new ListResultDto(resourceNames); + } + /// + /// 查询所有不重复的跨域地址 + /// + /// + public virtual async Task> GetAllDistinctAllowedCorsOriginsAsync() + { + var corsOrigins = await ClientRepository.GetAllDistinctAllowedCorsOriginsAsync(); + + return new ListResultDto(corsOrigins); + } + } +} diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Grants/PersistedGrantAppService.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Grants/PersistedGrantAppService.cs index 9314db511..9edf0f777 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Grants/PersistedGrantAppService.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/Grants/PersistedGrantAppService.cs @@ -1,52 +1,52 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Volo.Abp.Application.Dtos; -using Volo.Abp.IdentityServer.Grants; - -namespace LINGYUN.Abp.IdentityServer.Grants -{ - [Authorize(AbpIdentityServerPermissions.Grants.Default)] - public class PersistedGrantAppService : AbpIdentityServerAppServiceBase, IPersistedGrantAppService - { - protected IPersistentGrantRepository PersistentGrantRepository { get; } - - public PersistedGrantAppService( - IPersistentGrantRepository persistentGrantRepository) - { - PersistentGrantRepository = persistentGrantRepository; - } - - [Authorize(AbpIdentityServerPermissions.Grants.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - var persistedGrant = await PersistentGrantRepository.GetAsync(id); - - await PersistentGrantRepository.DeleteAsync(persistedGrant); - await CurrentUnitOfWork.SaveChangesAsync(); - } - - public virtual async Task GetAsync(Guid id) - { - var persistedGrant = await PersistentGrantRepository.GetAsync(id); - - return ObjectMapper.Map(persistedGrant); - } - - public virtual async Task> GetListAsync(GetPersistedGrantInput input) - { - var persistenGrantCount = await PersistentGrantRepository - .GetCountAsync( - input.SubjectId, input.Filter); - - var persistenGrants = await PersistentGrantRepository - .GetListAsync( - input.SubjectId, input.Filter, input.Sorting, - input.SkipCount, input.MaxResultCount); - - return new PagedResultDto(persistenGrantCount, - ObjectMapper.Map, List>(persistenGrants)); - } - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; +using Volo.Abp.IdentityServer.Grants; + +namespace LINGYUN.Abp.IdentityServer.Grants +{ + [Authorize(AbpIdentityServerPermissions.Grants.Default)] + public class PersistedGrantAppService : AbpIdentityServerAppServiceBase, IPersistedGrantAppService + { + protected IPersistentGrantRepository PersistentGrantRepository { get; } + + public PersistedGrantAppService( + IPersistentGrantRepository persistentGrantRepository) + { + PersistentGrantRepository = persistentGrantRepository; + } + + [Authorize(AbpIdentityServerPermissions.Grants.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + var persistedGrant = await PersistentGrantRepository.GetAsync(id); + + await PersistentGrantRepository.DeleteAsync(persistedGrant); + await CurrentUnitOfWork.CompleteAsync(); + } + + public virtual async Task GetAsync(Guid id) + { + var persistedGrant = await PersistentGrantRepository.GetAsync(id); + + return ObjectMapper.Map(persistedGrant); + } + + public virtual async Task> GetListAsync(GetPersistedGrantInput input) + { + var persistenGrantCount = await PersistentGrantRepository + .GetCountAsync( + input.SubjectId, input.Filter); + + var persistenGrants = await PersistentGrantRepository + .GetListAsync( + input.SubjectId, input.Filter, input.Sorting, + input.SkipCount, input.MaxResultCount); + + return new PagedResultDto(persistenGrantCount, + ObjectMapper.Map, List>(persistenGrants)); + } + } +} diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/IdentityResources/IdentityResourceAppService.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/IdentityResources/IdentityResourceAppService.cs index 4dcfec90c..d10a61087 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/IdentityResources/IdentityResourceAppService.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.Application/LINGYUN/Abp/IdentityServer/IdentityResources/IdentityResourceAppService.cs @@ -1,131 +1,131 @@ -using Microsoft.AspNetCore.Authorization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Application.Dtos; -using Volo.Abp.IdentityServer.IdentityResources; - -namespace LINGYUN.Abp.IdentityServer.IdentityResources -{ - [Authorize(AbpIdentityServerPermissions.IdentityResources.Default)] - public class IdentityResourceAppService : AbpIdentityServerAppServiceBase, IIdentityResourceAppService - { - protected IIdentityResourceRepository IdentityResourceRepository { get; } - - public IdentityResourceAppService( - IIdentityResourceRepository identityResourceRepository) - { - IdentityResourceRepository = identityResourceRepository; - } - - public virtual async Task GetAsync(Guid id) - { - var identityResource = await IdentityResourceRepository.GetAsync(id); - - return ObjectMapper.Map(identityResource); - } - - public virtual async Task> GetListAsync(IdentityResourceGetByPagedDto input) - { - var identityResources = await IdentityResourceRepository.GetListAsync(input.Sorting, - input.SkipCount, input.MaxResultCount, - input.Filter); - var identityResourceCount = await IdentityResourceRepository.GetCountAsync(); - - return new PagedResultDto(identityResourceCount, - ObjectMapper.Map, List>(identityResources)); - } - - [Authorize(AbpIdentityServerPermissions.IdentityResources.Create)] - public virtual async Task CreateAsync(IdentityResourceCreateOrUpdateDto input) - { - var identityResourceExists = await IdentityResourceRepository.CheckNameExistAsync(input.Name); - if (identityResourceExists) - { - throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.IdentityResourceNameExisted, input.Name]); - } - var identityResource = new IdentityResource(GuidGenerator.Create(), input.Name, input.DisplayName, - input.Description, input.Enabled, input.Required, input.Emphasize, - input.ShowInDiscoveryDocument); - await UpdateApiResourceByInputAsync(identityResource, input); - - await CurrentUnitOfWork.SaveChangesAsync(); - - identityResource = await IdentityResourceRepository.InsertAsync(identityResource); - - return ObjectMapper.Map(identityResource); - } - - [Authorize(AbpIdentityServerPermissions.IdentityResources.Update)] - public virtual async Task UpdateAsync(Guid id, IdentityResourceCreateOrUpdateDto input) - { - var identityResource = await IdentityResourceRepository.GetAsync(id); - await UpdateApiResourceByInputAsync(identityResource, input); - identityResource = await IdentityResourceRepository.UpdateAsync(identityResource); - - await CurrentUnitOfWork.SaveChangesAsync(); - - return ObjectMapper.Map(identityResource); - } - - [Authorize(AbpIdentityServerPermissions.IdentityResources.Delete)] - public virtual async Task DeleteAsync(Guid id) - { - await IdentityResourceRepository.DeleteAsync(id); - } - - protected virtual async Task UpdateApiResourceByInputAsync(IdentityResource identityResource, IdentityResourceCreateOrUpdateDto input) - { - if (!string.Equals(identityResource.Name, input.Name, StringComparison.InvariantCultureIgnoreCase)) - { - identityResource.Name = input.Name; - } - if (!string.Equals(identityResource.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) - { - identityResource.Description = input.Description; - } - if (!string.Equals(identityResource.DisplayName, input.DisplayName, StringComparison.InvariantCultureIgnoreCase)) - { - identityResource.DisplayName = input.DisplayName; - } - identityResource.Emphasize = input.Emphasize; - identityResource.Enabled = input.Enabled; - identityResource.Required = input.Required; - identityResource.ShowInDiscoveryDocument = input.ShowInDiscoveryDocument; - - if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageClaims)) - { - // 删除不存在的UserClaim - identityResource.UserClaims.RemoveAll(claim => input.UserClaims.Any(inputClaim => claim.Type == inputClaim.Type)); - foreach (var inputClaim in input.UserClaims) - { - var userClaim = identityResource.FindUserClaim(inputClaim.Type); - if (userClaim == null) - { - identityResource.AddUserClaim(inputClaim.Type); - } - } - } - - if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageProperties)) - { - // 删除不存在的Property - identityResource.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); - foreach (var inputProp in input.Properties) - { - var identityResourceProperty = identityResource.FindProperty(inputProp.Key); - if (identityResourceProperty == null) - { - identityResource.AddProperty(inputProp.Key, inputProp.Value); - } - else - { - identityResourceProperty.Value = inputProp.Value; - } - } - } - } - } -} +using Microsoft.AspNetCore.Authorization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Volo.Abp.IdentityServer.IdentityResources; + +namespace LINGYUN.Abp.IdentityServer.IdentityResources +{ + [Authorize(AbpIdentityServerPermissions.IdentityResources.Default)] + public class IdentityResourceAppService : AbpIdentityServerAppServiceBase, IIdentityResourceAppService + { + protected IIdentityResourceRepository IdentityResourceRepository { get; } + + public IdentityResourceAppService( + IIdentityResourceRepository identityResourceRepository) + { + IdentityResourceRepository = identityResourceRepository; + } + + public virtual async Task GetAsync(Guid id) + { + var identityResource = await IdentityResourceRepository.GetAsync(id); + + return ObjectMapper.Map(identityResource); + } + + public virtual async Task> GetListAsync(IdentityResourceGetByPagedDto input) + { + var identityResources = await IdentityResourceRepository.GetListAsync(input.Sorting, + input.SkipCount, input.MaxResultCount, + input.Filter); + var identityResourceCount = await IdentityResourceRepository.GetCountAsync(); + + return new PagedResultDto(identityResourceCount, + ObjectMapper.Map, List>(identityResources)); + } + + [Authorize(AbpIdentityServerPermissions.IdentityResources.Create)] + public virtual async Task CreateAsync(IdentityResourceCreateOrUpdateDto input) + { + var identityResourceExists = await IdentityResourceRepository.CheckNameExistAsync(input.Name); + if (identityResourceExists) + { + throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.IdentityResourceNameExisted, input.Name]); + } + var identityResource = new IdentityResource(GuidGenerator.Create(), input.Name, input.DisplayName, + input.Description, input.Enabled, input.Required, input.Emphasize, + input.ShowInDiscoveryDocument); + await UpdateApiResourceByInputAsync(identityResource, input); + + await CurrentUnitOfWork.CompleteAsync(); + + identityResource = await IdentityResourceRepository.InsertAsync(identityResource); + + return ObjectMapper.Map(identityResource); + } + + [Authorize(AbpIdentityServerPermissions.IdentityResources.Update)] + public virtual async Task UpdateAsync(Guid id, IdentityResourceCreateOrUpdateDto input) + { + var identityResource = await IdentityResourceRepository.GetAsync(id); + await UpdateApiResourceByInputAsync(identityResource, input); + identityResource = await IdentityResourceRepository.UpdateAsync(identityResource); + + await CurrentUnitOfWork.CompleteAsync(); + + return ObjectMapper.Map(identityResource); + } + + [Authorize(AbpIdentityServerPermissions.IdentityResources.Delete)] + public virtual async Task DeleteAsync(Guid id) + { + await IdentityResourceRepository.DeleteAsync(id); + } + + protected virtual async Task UpdateApiResourceByInputAsync(IdentityResource identityResource, IdentityResourceCreateOrUpdateDto input) + { + if (!string.Equals(identityResource.Name, input.Name, StringComparison.InvariantCultureIgnoreCase)) + { + identityResource.Name = input.Name; + } + if (!string.Equals(identityResource.Description, input.Description, StringComparison.InvariantCultureIgnoreCase)) + { + identityResource.Description = input.Description; + } + if (!string.Equals(identityResource.DisplayName, input.DisplayName, StringComparison.InvariantCultureIgnoreCase)) + { + identityResource.DisplayName = input.DisplayName; + } + identityResource.Emphasize = input.Emphasize; + identityResource.Enabled = input.Enabled; + identityResource.Required = input.Required; + identityResource.ShowInDiscoveryDocument = input.ShowInDiscoveryDocument; + + if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageClaims)) + { + // 删除不存在的UserClaim + identityResource.UserClaims.RemoveAll(claim => input.UserClaims.Any(inputClaim => claim.Type == inputClaim.Type)); + foreach (var inputClaim in input.UserClaims) + { + var userClaim = identityResource.FindUserClaim(inputClaim.Type); + if (userClaim == null) + { + identityResource.AddUserClaim(inputClaim.Type); + } + } + } + + if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageProperties)) + { + // 删除不存在的Property + identityResource.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key)); + foreach (var inputProp in input.Properties) + { + var identityResourceProperty = identityResource.FindProperty(inputProp.Key); + if (identityResourceProperty == null) + { + identityResource.AddProperty(inputProp.Key, inputProp.Value); + } + else + { + identityResourceProperty.Value = inputProp.Value; + } + } + } + } + } +} diff --git a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Datas/DataAppService.cs b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Datas/DataAppService.cs index e839bbb87..6b54283a1 100644 --- a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Datas/DataAppService.cs +++ b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Datas/DataAppService.cs @@ -57,7 +57,7 @@ namespace LINGYUN.Platform.Datas ); data = await DataRepository.InsertAsync(data); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(data); } @@ -129,7 +129,7 @@ namespace LINGYUN.Platform.Datas } data = await DataRepository.UpdateAsync(data); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(data); } @@ -159,7 +159,7 @@ namespace LINGYUN.Platform.Datas dataItem.AllowBeNull = input.AllowBeNull; await DataRepository.UpdateAsync(data); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(PlatformPermissions.DataDictionary.ManageItems)] @@ -182,7 +182,7 @@ namespace LINGYUN.Platform.Datas input.AllowBeNull); await DataRepository.UpdateAsync(data); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(PlatformPermissions.DataDictionary.ManageItems)] @@ -192,7 +192,7 @@ namespace LINGYUN.Platform.Datas data.RemoveItem(name); await DataRepository.UpdateAsync(data); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } } } diff --git a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Layouts/LayoutAppService.cs b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Layouts/LayoutAppService.cs index cd60dd2c8..137239963 100644 --- a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Layouts/LayoutAppService.cs +++ b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Layouts/LayoutAppService.cs @@ -40,7 +40,7 @@ namespace LINGYUN.Platform.Layouts CurrentTenant.Id); layout = await LayoutRepository.InsertAsync(layout); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(layout); } @@ -56,7 +56,7 @@ namespace LINGYUN.Platform.Layouts //} await LayoutRepository.DeleteAsync(layout); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } public virtual async Task GetAsync(Guid id) @@ -113,7 +113,7 @@ namespace LINGYUN.Platform.Layouts layout.Redirect = input.Redirect; } layout = await LayoutRepository.UpdateAsync(layout); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(layout); } diff --git a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Menus/MenuAppService.cs b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Menus/MenuAppService.cs index de31a4f1b..136496d3f 100644 --- a/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Menus/MenuAppService.cs +++ b/aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/Menus/MenuAppService.cs @@ -121,7 +121,7 @@ namespace LINGYUN.Platform.Menus } } - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(menu); } @@ -188,7 +188,7 @@ namespace LINGYUN.Platform.Menus menu.IsPublic = input.IsPublic; await MenuManager.UpdateAsync(menu); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); return ObjectMapper.Map(menu); } diff --git a/aspnet-core/modules/settings/LINGYUN.Abp.SettingManagement.Application/LINGYUN/Abp/SettingManagement/SettingAppService.cs b/aspnet-core/modules/settings/LINGYUN.Abp.SettingManagement.Application/LINGYUN/Abp/SettingManagement/SettingAppService.cs index 518df2bba..d8dd27adb 100644 --- a/aspnet-core/modules/settings/LINGYUN.Abp.SettingManagement.Application/LINGYUN/Abp/SettingManagement/SettingAppService.cs +++ b/aspnet-core/modules/settings/LINGYUN.Abp.SettingManagement.Application/LINGYUN/Abp/SettingManagement/SettingAppService.cs @@ -64,7 +64,7 @@ namespace LINGYUN.Abp.SettingManagement await EventBus.PublishAsync(new CurrentApplicationConfigurationCacheResetEventData()); }); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } [Authorize(AbpSettingManagementPermissions.Settings.Manager)] @@ -86,7 +86,7 @@ namespace LINGYUN.Abp.SettingManagement await EventBus.PublishAsync(new CurrentApplicationConfigurationCacheResetEventData()); }); - await CurrentUnitOfWork.SaveChangesAsync(); + await CurrentUnitOfWork.CompleteAsync(); } }