From 496d56580c1b6c253752591fd7933439689ff4fd Mon Sep 17 00:00:00 2001 From: malik masis Date: Mon, 7 Nov 2022 15:12:36 +0300 Subject: [PATCH 1/7] Create UserPasswordChangeRequestedEto.cs --- .../Abp/Users/UserPasswordChangeRequestedEto.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs diff --git a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs new file mode 100644 index 0000000000..f28424714b --- /dev/null +++ b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs @@ -0,0 +1,15 @@ +using System; +using Volo.Abp.EventBus; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.Users; + +[EventName("Volo.Abp.Users.UserPasswordChangeRequested")] +public class UserPasswordChangeRequestedEto : IMultiTenant +{ + public Guid? TenantId { get; set; } + + public string UserName { get; set; } + + public string Password { get; set; } +} From dea09e45b7d25d5af8054f2632971eb07bd4f691 Mon Sep 17 00:00:00 2001 From: malik masis Date: Mon, 7 Nov 2022 17:30:49 +0300 Subject: [PATCH 2/7] Added SetPasswordEventHandler --- .../Volo/Abp/Identity/IdentityUserManager.cs | 13 ++++++++++ .../Abp/Identity/SetPasswordEventHandler.cs | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index b1db0e01e5..90f2d3ecd7 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -253,4 +253,17 @@ public class IdentityUserManager : UserManager, IDomainService return await UpdateUserAsync(user); } + + public virtual async Task SetPasswordAsync(Guid? userId, string userName, string password) + { + if (!password.IsNullOrEmpty()) + { + var user = await UserRepository.GetAsync(userId.GetValueOrDefault(), cancellationToken: CancellationToken); + if (user != null) + { + (await RemovePasswordAsync(user)).CheckErrors(); + (await AddPasswordAsync(user, password)).CheckErrors(); + } + } + } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs new file mode 100644 index 0000000000..ec56dfe059 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Users; + +namespace Volo.Abp.Identity; +public class SetPasswordEventHandler : + IDistributedEventHandler, + ITransientDependency +{ + protected IdentityUserManager UserManager { get; } + + public SetPasswordEventHandler( + IdentityUserManager permissionManager) + { + UserManager = permissionManager; + } + + public async Task HandleEventAsync(UserPasswordChangeRequestedEto eventData) + { + await UserManager.SetPasswordAsync( + eventData.TenantId, + eventData.UserName, + eventData.Password); + } +} \ No newline at end of file From cf87863b82047bc346d9b28dd103576fb258a8e8 Mon Sep 17 00:00:00 2001 From: malik masis Date: Tue, 8 Nov 2022 15:31:58 +0300 Subject: [PATCH 3/7] Update UserPasswordChangeRequestedEto.cs --- .../Volo/Abp/Users/UserPasswordChangeRequestedEto.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs index f28424714b..d570acab95 100644 --- a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs +++ b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs @@ -1,13 +1,13 @@ using System; using Volo.Abp.EventBus; -using Volo.Abp.MultiTenancy; namespace Volo.Abp.Users; +[Serializable] [EventName("Volo.Abp.Users.UserPasswordChangeRequested")] -public class UserPasswordChangeRequestedEto : IMultiTenant +public class UserPasswordChangeRequestedEto { - public Guid? TenantId { get; set; } + public Guid TenantId { get; set; } public string UserName { get; set; } From 2d780db2587c131ad543811a4135cf9bc2f87fcb Mon Sep 17 00:00:00 2001 From: malik masis Date: Tue, 8 Nov 2022 15:32:18 +0300 Subject: [PATCH 4/7] Removed SetPasswordAsync --- .../Volo/Abp/Identity/IdentityUserManager.cs | 485 +++++++++--------- .../Abp/Identity/SetPasswordEventHandler.cs | 26 - 2 files changed, 236 insertions(+), 275 deletions(-) delete mode 100644 modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index 90f2d3ecd7..905745ed21 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -10,260 +10,247 @@ using Microsoft.Extensions.Options; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Services; +using Volo.Abp.Identity.Settings; +using Volo.Abp.Settings; using Volo.Abp.Threading; using Volo.Abp.Uow; -using Volo.Abp.Settings; -using Volo.Abp.Identity.Settings; namespace Volo.Abp.Identity; public class IdentityUserManager : UserManager, IDomainService { - protected IIdentityRoleRepository RoleRepository { get; } - protected IIdentityUserRepository UserRepository { get; } - protected IOrganizationUnitRepository OrganizationUnitRepository { get; } - protected ISettingProvider SettingProvider { get; } - protected ICancellationTokenProvider CancellationTokenProvider { get; } - - protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; - - public IdentityUserManager( - IdentityUserStore store, - IIdentityRoleRepository roleRepository, - IIdentityUserRepository userRepository, - IOptions optionsAccessor, - IPasswordHasher passwordHasher, - IEnumerable> userValidators, - IEnumerable> passwordValidators, - ILookupNormalizer keyNormalizer, - IdentityErrorDescriber errors, - IServiceProvider services, - ILogger logger, - ICancellationTokenProvider cancellationTokenProvider, - IOrganizationUnitRepository organizationUnitRepository, - ISettingProvider settingProvider) - : base( - store, - optionsAccessor, - passwordHasher, - userValidators, - passwordValidators, - keyNormalizer, - errors, - services, - logger) - { - OrganizationUnitRepository = organizationUnitRepository; - SettingProvider = settingProvider; - RoleRepository = roleRepository; - UserRepository = userRepository; - CancellationTokenProvider = cancellationTokenProvider; - } - - public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) - { - var result = await UpdatePasswordHash(user, password, validatePassword); - if (!result.Succeeded) - { - return result; - } - - return await CreateAsync(user); - } - - public virtual async Task GetByIdAsync(Guid id) - { - var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); - if (user == null) - { - throw new EntityNotFoundException(typeof(IdentityUser), id); - } - - return user; - } - - public virtual async Task SetRolesAsync([NotNull] IdentityUser user, - [NotNull] IEnumerable roleNames) - { - Check.NotNull(user, nameof(user)); - Check.NotNull(roleNames, nameof(roleNames)); - - var currentRoleNames = await GetRolesAsync(user); - - var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); - if (!result.Succeeded) - { - return result; - } - - result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); - if (!result.Succeeded) - { - return result; - } - - return IdentityResult.Success; - } - - public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) - { - var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); - return user.IsInOrganizationUnit(ouId); - } - - public virtual async Task IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - return user.IsInOrganizationUnit(ou.Id); - } - - public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId) - { - await AddToOrganizationUnitAsync( - await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), - await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) - ); - } - - public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) - { - return; - } - - await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); - - user.AddOrganizationUnit(ou.Id); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) - { - var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); - user.RemoveOrganizationUnit(ouId); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - user.RemoveOrganizationUnit(ou.Id); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) - { - await SetOrganizationUnitsAsync( - await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), - organizationUnitIds - ); - } - - public virtual async Task SetOrganizationUnitsAsync(IdentityUser user, params Guid[] organizationUnitIds) - { - Check.NotNull(user, nameof(user)); - Check.NotNull(organizationUnitIds, nameof(organizationUnitIds)); - - await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); - - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - //Remove from removed OUs - foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) - { - if (!organizationUnitIds.Contains(ouId)) - { - user.RemoveOrganizationUnit(ouId); - } - } - - //Add to added OUs - foreach (var organizationUnitId in organizationUnitIds) - { - if (!user.IsInOrganizationUnit(organizationUnitId)) - { - user.AddOrganizationUnit(organizationUnitId); - } - } - - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) - { - var maxCount = - await SettingProvider.GetAsync(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); - if (requestedCount > maxCount) - { - throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) - .WithData("MaxUserMembershipCount", maxCount); - } - } - - [UnitOfWork] - public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user, - bool includeDetails = false) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - return await OrganizationUnitRepository.GetListAsync( - user.OrganizationUnits.Select(t => t.OrganizationUnitId), - includeDetails, - cancellationToken: CancellationToken - ); - } - - [UnitOfWork] - public virtual async Task> GetUsersInOrganizationUnitAsync( - OrganizationUnit organizationUnit, - bool includeChildren = false) - { - if (includeChildren) - { - return await UserRepository - .GetUsersInOrganizationUnitWithChildrenAsync(organizationUnit.Code, CancellationToken); - } - else - { - return await UserRepository - .GetUsersInOrganizationUnitAsync(organizationUnit.Id, CancellationToken); - } - } - - public virtual async Task AddDefaultRolesAsync([NotNull] IdentityUser user) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, CancellationToken); - - foreach (var role in await RoleRepository.GetDefaultOnesAsync(cancellationToken: CancellationToken)) - { - if (!user.IsInRole(role.Id)) - { - user.AddRole(role.Id); - } - } - - return await UpdateUserAsync(user); - } - - public virtual async Task SetPasswordAsync(Guid? userId, string userName, string password) - { - if (!password.IsNullOrEmpty()) - { - var user = await UserRepository.GetAsync(userId.GetValueOrDefault(), cancellationToken: CancellationToken); - if (user != null) - { - (await RemovePasswordAsync(user)).CheckErrors(); - (await AddPasswordAsync(user, password)).CheckErrors(); - } - } - } + protected IIdentityRoleRepository RoleRepository { get; } + protected IIdentityUserRepository UserRepository { get; } + protected IOrganizationUnitRepository OrganizationUnitRepository { get; } + protected ISettingProvider SettingProvider { get; } + protected ICancellationTokenProvider CancellationTokenProvider { get; } + + protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; + + public IdentityUserManager( + IdentityUserStore store, + IIdentityRoleRepository roleRepository, + IIdentityUserRepository userRepository, + IOptions optionsAccessor, + IPasswordHasher passwordHasher, + IEnumerable> userValidators, + IEnumerable> passwordValidators, + ILookupNormalizer keyNormalizer, + IdentityErrorDescriber errors, + IServiceProvider services, + ILogger logger, + ICancellationTokenProvider cancellationTokenProvider, + IOrganizationUnitRepository organizationUnitRepository, + ISettingProvider settingProvider) + : base( + store, + optionsAccessor, + passwordHasher, + userValidators, + passwordValidators, + keyNormalizer, + errors, + services, + logger) + { + OrganizationUnitRepository = organizationUnitRepository; + SettingProvider = settingProvider; + RoleRepository = roleRepository; + UserRepository = userRepository; + CancellationTokenProvider = cancellationTokenProvider; + } + + public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) + { + var result = await UpdatePasswordHash(user, password, validatePassword); + if (!result.Succeeded) + { + return result; + } + + return await CreateAsync(user); + } + + public virtual async Task GetByIdAsync(Guid id) + { + var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); + if (user == null) + { + throw new EntityNotFoundException(typeof(IdentityUser), id); + } + + return user; + } + + public virtual async Task SetRolesAsync([NotNull] IdentityUser user, + [NotNull] IEnumerable roleNames) + { + Check.NotNull(user, nameof(user)); + Check.NotNull(roleNames, nameof(roleNames)); + + var currentRoleNames = await GetRolesAsync(user); + + var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + return IdentityResult.Success; + } + + public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) + { + var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); + return user.IsInOrganizationUnit(ouId); + } + + public virtual async Task IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + return user.IsInOrganizationUnit(ou.Id); + } + + public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId) + { + await AddToOrganizationUnitAsync( + await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), + await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) + ); + } + + public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) + { + return; + } + + await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); + + user.AddOrganizationUnit(ou.Id); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) + { + var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); + user.RemoveOrganizationUnit(ouId); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + user.RemoveOrganizationUnit(ou.Id); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) + { + await SetOrganizationUnitsAsync( + await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), + organizationUnitIds + ); + } + + public virtual async Task SetOrganizationUnitsAsync(IdentityUser user, params Guid[] organizationUnitIds) + { + Check.NotNull(user, nameof(user)); + Check.NotNull(organizationUnitIds, nameof(organizationUnitIds)); + + await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); + + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + //Remove from removed OUs + foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) + { + if (!organizationUnitIds.Contains(ouId)) + { + user.RemoveOrganizationUnit(ouId); + } + } + + //Add to added OUs + foreach (var organizationUnitId in organizationUnitIds) + { + if (!user.IsInOrganizationUnit(organizationUnitId)) + { + user.AddOrganizationUnit(organizationUnitId); + } + } + + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) + { + var maxCount = + await SettingProvider.GetAsync(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); + if (requestedCount > maxCount) + { + throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) + .WithData("MaxUserMembershipCount", maxCount); + } + } + + [UnitOfWork] + public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user, + bool includeDetails = false) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + return await OrganizationUnitRepository.GetListAsync( + user.OrganizationUnits.Select(t => t.OrganizationUnitId), + includeDetails, + cancellationToken: CancellationToken + ); + } + + [UnitOfWork] + public virtual async Task> GetUsersInOrganizationUnitAsync( + OrganizationUnit organizationUnit, + bool includeChildren = false) + { + if (includeChildren) + { + return await UserRepository + .GetUsersInOrganizationUnitWithChildrenAsync(organizationUnit.Code, CancellationToken); + } + else + { + return await UserRepository + .GetUsersInOrganizationUnitAsync(organizationUnit.Id, CancellationToken); + } + } + + public virtual async Task AddDefaultRolesAsync([NotNull] IdentityUser user) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, CancellationToken); + + foreach (var role in await RoleRepository.GetDefaultOnesAsync(cancellationToken: CancellationToken)) + { + if (!user.IsInRole(role.Id)) + { + user.AddRole(role.Id); + } + } + + return await UpdateUserAsync(user); + } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs deleted file mode 100644 index ec56dfe059..0000000000 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/SetPasswordEventHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.Users; - -namespace Volo.Abp.Identity; -public class SetPasswordEventHandler : - IDistributedEventHandler, - ITransientDependency -{ - protected IdentityUserManager UserManager { get; } - - public SetPasswordEventHandler( - IdentityUserManager permissionManager) - { - UserManager = permissionManager; - } - - public async Task HandleEventAsync(UserPasswordChangeRequestedEto eventData) - { - await UserManager.SetPasswordAsync( - eventData.TenantId, - eventData.UserName, - eventData.Password); - } -} \ No newline at end of file From 4d29c6c502168290281a74ad5c3cac2138fa5002 Mon Sep 17 00:00:00 2001 From: malik masis Date: Tue, 8 Nov 2022 15:47:15 +0300 Subject: [PATCH 5/7] Update UserPasswordChangeRequestedEto.cs --- .../Volo/Abp/Users/UserPasswordChangeRequestedEto.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs index d570acab95..e729316b02 100644 --- a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs +++ b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs @@ -1,13 +1,14 @@ using System; using Volo.Abp.EventBus; +using Volo.Abp.MultiTenancy; namespace Volo.Abp.Users; -[Serializable] +//[Serializable] [EventName("Volo.Abp.Users.UserPasswordChangeRequested")] -public class UserPasswordChangeRequestedEto +public class UserPasswordChangeRequestedEto : IMultiTenant { - public Guid TenantId { get; set; } + public Guid? TenantId { get; set; } public string UserName { get; set; } From 3ccc5f91cff217cb55089bccf6569d7fad35939f Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 9 Nov 2022 12:52:03 +0300 Subject: [PATCH 6/7] Update UserPasswordChangeRequestedEto.cs --- .../Volo/Abp/Users/UserPasswordChangeRequestedEto.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs index e729316b02..9bd028b5e2 100644 --- a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs +++ b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs @@ -4,13 +4,13 @@ using Volo.Abp.MultiTenancy; namespace Volo.Abp.Users; -//[Serializable] +[Serializable] [EventName("Volo.Abp.Users.UserPasswordChangeRequested")] public class UserPasswordChangeRequestedEto : IMultiTenant { - public Guid? TenantId { get; set; } + public Guid? TenantId { get; set; } - public string UserName { get; set; } + public string UserName { get; set; } - public string Password { get; set; } + public string Password { get; set; } } From 070dc794801728f3717c6868ea6204585dd93680 Mon Sep 17 00:00:00 2001 From: malik masis Date: Wed, 9 Nov 2022 17:50:38 +0300 Subject: [PATCH 7/7] Fixed space and tab issue --- .../Volo/Abp/Identity/IdentityUserManager.cs | 468 +++++++++--------- .../Users/UserPasswordChangeRequestedEto.cs | 6 +- 2 files changed, 237 insertions(+), 237 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index 905745ed21..d0520c9ef9 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -19,238 +19,238 @@ namespace Volo.Abp.Identity; public class IdentityUserManager : UserManager, IDomainService { - protected IIdentityRoleRepository RoleRepository { get; } - protected IIdentityUserRepository UserRepository { get; } - protected IOrganizationUnitRepository OrganizationUnitRepository { get; } - protected ISettingProvider SettingProvider { get; } - protected ICancellationTokenProvider CancellationTokenProvider { get; } - - protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; - - public IdentityUserManager( - IdentityUserStore store, - IIdentityRoleRepository roleRepository, - IIdentityUserRepository userRepository, - IOptions optionsAccessor, - IPasswordHasher passwordHasher, - IEnumerable> userValidators, - IEnumerable> passwordValidators, - ILookupNormalizer keyNormalizer, - IdentityErrorDescriber errors, - IServiceProvider services, - ILogger logger, - ICancellationTokenProvider cancellationTokenProvider, - IOrganizationUnitRepository organizationUnitRepository, - ISettingProvider settingProvider) - : base( - store, - optionsAccessor, - passwordHasher, - userValidators, - passwordValidators, - keyNormalizer, - errors, - services, - logger) - { - OrganizationUnitRepository = organizationUnitRepository; - SettingProvider = settingProvider; - RoleRepository = roleRepository; - UserRepository = userRepository; - CancellationTokenProvider = cancellationTokenProvider; - } - - public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) - { - var result = await UpdatePasswordHash(user, password, validatePassword); - if (!result.Succeeded) - { - return result; - } - - return await CreateAsync(user); - } - - public virtual async Task GetByIdAsync(Guid id) - { - var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); - if (user == null) - { - throw new EntityNotFoundException(typeof(IdentityUser), id); - } - - return user; - } - - public virtual async Task SetRolesAsync([NotNull] IdentityUser user, - [NotNull] IEnumerable roleNames) - { - Check.NotNull(user, nameof(user)); - Check.NotNull(roleNames, nameof(roleNames)); - - var currentRoleNames = await GetRolesAsync(user); - - var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); - if (!result.Succeeded) - { - return result; - } - - result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); - if (!result.Succeeded) - { - return result; - } - - return IdentityResult.Success; - } - - public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) - { - var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); - return user.IsInOrganizationUnit(ouId); - } - - public virtual async Task IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - return user.IsInOrganizationUnit(ou.Id); - } - - public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId) - { - await AddToOrganizationUnitAsync( - await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), - await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) - ); - } - - public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) - { - return; - } - - await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); - - user.AddOrganizationUnit(ou.Id); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) - { - var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); - user.RemoveOrganizationUnit(ouId); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - user.RemoveOrganizationUnit(ou.Id); - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) - { - await SetOrganizationUnitsAsync( - await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), - organizationUnitIds - ); - } - - public virtual async Task SetOrganizationUnitsAsync(IdentityUser user, params Guid[] organizationUnitIds) - { - Check.NotNull(user, nameof(user)); - Check.NotNull(organizationUnitIds, nameof(organizationUnitIds)); - - await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); - - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - //Remove from removed OUs - foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) - { - if (!organizationUnitIds.Contains(ouId)) - { - user.RemoveOrganizationUnit(ouId); - } - } - - //Add to added OUs - foreach (var organizationUnitId in organizationUnitIds) - { - if (!user.IsInOrganizationUnit(organizationUnitId)) - { - user.AddOrganizationUnit(organizationUnitId); - } - } - - await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); - } - - private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) - { - var maxCount = - await SettingProvider.GetAsync(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); - if (requestedCount > maxCount) - { - throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) - .WithData("MaxUserMembershipCount", maxCount); - } - } - - [UnitOfWork] - public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user, - bool includeDetails = false) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, - CancellationTokenProvider.Token); - - return await OrganizationUnitRepository.GetListAsync( - user.OrganizationUnits.Select(t => t.OrganizationUnitId), - includeDetails, - cancellationToken: CancellationToken - ); - } - - [UnitOfWork] - public virtual async Task> GetUsersInOrganizationUnitAsync( - OrganizationUnit organizationUnit, - bool includeChildren = false) - { - if (includeChildren) - { - return await UserRepository - .GetUsersInOrganizationUnitWithChildrenAsync(organizationUnit.Code, CancellationToken); - } - else - { - return await UserRepository - .GetUsersInOrganizationUnitAsync(organizationUnit.Id, CancellationToken); - } - } - - public virtual async Task AddDefaultRolesAsync([NotNull] IdentityUser user) - { - await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, CancellationToken); - - foreach (var role in await RoleRepository.GetDefaultOnesAsync(cancellationToken: CancellationToken)) - { - if (!user.IsInRole(role.Id)) - { - user.AddRole(role.Id); - } - } - - return await UpdateUserAsync(user); - } + protected IIdentityRoleRepository RoleRepository { get; } + protected IIdentityUserRepository UserRepository { get; } + protected IOrganizationUnitRepository OrganizationUnitRepository { get; } + protected ISettingProvider SettingProvider { get; } + protected ICancellationTokenProvider CancellationTokenProvider { get; } + + protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; + + public IdentityUserManager( + IdentityUserStore store, + IIdentityRoleRepository roleRepository, + IIdentityUserRepository userRepository, + IOptions optionsAccessor, + IPasswordHasher passwordHasher, + IEnumerable> userValidators, + IEnumerable> passwordValidators, + ILookupNormalizer keyNormalizer, + IdentityErrorDescriber errors, + IServiceProvider services, + ILogger logger, + ICancellationTokenProvider cancellationTokenProvider, + IOrganizationUnitRepository organizationUnitRepository, + ISettingProvider settingProvider) + : base( + store, + optionsAccessor, + passwordHasher, + userValidators, + passwordValidators, + keyNormalizer, + errors, + services, + logger) + { + OrganizationUnitRepository = organizationUnitRepository; + SettingProvider = settingProvider; + RoleRepository = roleRepository; + UserRepository = userRepository; + CancellationTokenProvider = cancellationTokenProvider; + } + + public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) + { + var result = await UpdatePasswordHash(user, password, validatePassword); + if (!result.Succeeded) + { + return result; + } + + return await CreateAsync(user); + } + + public virtual async Task GetByIdAsync(Guid id) + { + var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); + if (user == null) + { + throw new EntityNotFoundException(typeof(IdentityUser), id); + } + + return user; + } + + public virtual async Task SetRolesAsync([NotNull] IdentityUser user, + [NotNull] IEnumerable roleNames) + { + Check.NotNull(user, nameof(user)); + Check.NotNull(roleNames, nameof(roleNames)); + + var currentRoleNames = await GetRolesAsync(user); + + var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + return IdentityResult.Success; + } + + public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) + { + var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); + return user.IsInOrganizationUnit(ouId); + } + + public virtual async Task IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + return user.IsInOrganizationUnit(ou.Id); + } + + public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId) + { + await AddToOrganizationUnitAsync( + await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), + await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) + ); + } + + public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) + { + return; + } + + await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); + + user.AddOrganizationUnit(ou.Id); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) + { + var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); + user.RemoveOrganizationUnit(ouId); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + user.RemoveOrganizationUnit(ou.Id); + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) + { + await SetOrganizationUnitsAsync( + await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), + organizationUnitIds + ); + } + + public virtual async Task SetOrganizationUnitsAsync(IdentityUser user, params Guid[] organizationUnitIds) + { + Check.NotNull(user, nameof(user)); + Check.NotNull(organizationUnitIds, nameof(organizationUnitIds)); + + await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); + + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + //Remove from removed OUs + foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) + { + if (!organizationUnitIds.Contains(ouId)) + { + user.RemoveOrganizationUnit(ouId); + } + } + + //Add to added OUs + foreach (var organizationUnitId in organizationUnitIds) + { + if (!user.IsInOrganizationUnit(organizationUnitId)) + { + user.AddOrganizationUnit(organizationUnitId); + } + } + + await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); + } + + private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) + { + var maxCount = + await SettingProvider.GetAsync(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); + if (requestedCount > maxCount) + { + throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) + .WithData("MaxUserMembershipCount", maxCount); + } + } + + [UnitOfWork] + public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user, + bool includeDetails = false) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, + CancellationTokenProvider.Token); + + return await OrganizationUnitRepository.GetListAsync( + user.OrganizationUnits.Select(t => t.OrganizationUnitId), + includeDetails, + cancellationToken: CancellationToken + ); + } + + [UnitOfWork] + public virtual async Task> GetUsersInOrganizationUnitAsync( + OrganizationUnit organizationUnit, + bool includeChildren = false) + { + if (includeChildren) + { + return await UserRepository + .GetUsersInOrganizationUnitWithChildrenAsync(organizationUnit.Code, CancellationToken); + } + else + { + return await UserRepository + .GetUsersInOrganizationUnitAsync(organizationUnit.Id, CancellationToken); + } + } + + public virtual async Task AddDefaultRolesAsync([NotNull] IdentityUser user) + { + await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, CancellationToken); + + foreach (var role in await RoleRepository.GetDefaultOnesAsync(cancellationToken: CancellationToken)) + { + if (!user.IsInRole(role.Id)) + { + user.AddRole(role.Id); + } + } + + return await UpdateUserAsync(user); + } } diff --git a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs index 9bd028b5e2..7eb15137cf 100644 --- a/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs +++ b/modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/UserPasswordChangeRequestedEto.cs @@ -8,9 +8,9 @@ namespace Volo.Abp.Users; [EventName("Volo.Abp.Users.UserPasswordChangeRequested")] public class UserPasswordChangeRequestedEto : IMultiTenant { - public Guid? TenantId { get; set; } + public Guid? TenantId { get; set; } - public string UserName { get; set; } + public string UserName { get; set; } - public string Password { get; set; } + public string Password { get; set; } }