From 2c1d6bfbf5d4923b2bd16004634dd4789dbef2a6 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Fri, 29 May 2020 23:54:07 +0300 Subject: [PATCH 1/5] added RemoveAllRoles and RemoveAllMembers to organizationUnitRepository --- .../Identity/IOrganizationUnitRepository.cs | 11 ++++++ .../Volo/Abp/Identity/IdentityUserManager.cs | 1 - .../EfCoreIdentityUserRepository.cs | 29 +++++++--------- .../EfCoreOrganizationUnitRepository.cs | 34 ++++++++++++++++++- .../MongoOrganizationUnitRepository.cs | 34 +++++++++++++++++-- .../OrganizationUnitRepository_Tests.cs | 24 +++++++++++++ 6 files changed, 113 insertions(+), 20 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs index def41cdc8a..bf698ca54f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs @@ -64,9 +64,20 @@ namespace Volo.Abp.Identity bool includeDetails = false, CancellationToken cancellationToken = default ); + Task GetMembersCountAsync( OrganizationUnit organizationUnit, CancellationToken cancellationToken = default ); + + Task RemoveAllRolesAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default + ); + + Task RemoveAllMembersAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default + ); } } 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 f90694349a..d22eef5af6 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 @@ -96,7 +96,6 @@ namespace Volo.Abp.Identity return IdentityResult.Success; } - public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) { var user = await IdentityUserRepository.GetAsync(userId, cancellationToken: CancellationToken); diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs index c1bee0449a..f2e82cfb77 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs @@ -152,15 +152,16 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userOrg.UserId == id select ou; - var orgUserRoleQuery = DbContext.Set().Where(q => userOrganizationsQuery.Select(t => t.Id).Contains(q.OrganizationUnitId)) + var orgUserRoleQuery = DbContext.Set() + .Where(q => userOrganizationsQuery + .Select(t => t.Id) + .Contains(q.OrganizationUnitId)) .Select(t => t.RoleId); var orgRoles = DbContext.Roles.Where(q => orgUserRoleQuery.Contains(q.Id)); var resultQuery = query.Union(orgRoles); return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); - - //return await query.ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task GetCountAsync( @@ -201,22 +202,18 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userOu.OrganizationUnitId == organizationUnitId select user; return await query.ToListAsync(GetCancellationToken(cancellationToken)); - } - - public async Task> GetUsersInOrganizationsListAsync( - List organizationUnitIds, - CancellationToken cancellationToken = default - ) - { - //var userIds = DbContext.Set() - // .Where(q => organizationUnitIds.Contains(q.OrganizationUnitId)) - // .Select(u => u.UserId); - //var query = DbContext.Users.Where(u => userIds.Contains(u.Id)); + } + + public async Task> GetUsersInOrganizationsListAsync( + List organizationUnitIds, + CancellationToken cancellationToken = default + ) + { var query = from userOu in DbContext.Set() join user in DbSet on userOu.UserId equals user.Id where organizationUnitIds.Contains(userOu.OrganizationUnitId) select user; - return await query.ToListAsync(GetCancellationToken(cancellationToken)); + return await query.ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetUsersInOrganizationUnitWithChildrenAsync( @@ -235,6 +232,6 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); - } + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs index 350f64ffe7..812c655140 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs @@ -102,7 +102,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public virtual async Task GetRolesCountAsync( OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) - { + { var query = from organizationRole in DbContext.Set() join role in DbContext.Roles on organizationRole.RoleId equals role.Id where organizationRole.OrganizationUnitId == organizationUnit.Id @@ -156,5 +156,37 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { return GetQueryable().IncludeDetails(); } + + public virtual Task RemoveAllRolesAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default) + { + var ouRolesQuery = DbContext.Set() + .Where(q => q.OrganizationUnitId == organizationUnit.Id); + + DbContext.Set().RemoveRange(ouRolesQuery); + return Task.FromResult(0); + //Can be long running process that could be made available for cancellation perhaps + //return Task.Run(() => + // DbContext.Set().RemoveRange(ouRolesQuery), + // GetCancellationToken(cancellationToken) + //); + } + + public virtual Task RemoveAllMembersAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default) + { + var ouMembersQuery = DbContext.Set() + .Where(q => q.OrganizationUnitId == organizationUnit.Id); + + DbContext.Set().RemoveRange(ouMembersQuery); + return Task.FromResult(0); + //Can be long running process that could be made available for cancellation perhaps + //return Task.Run(() => + // DbContext.Set().RemoveRange(ouMembersQuery), + // GetCancellationToken(cancellationToken) + //); + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index ae735300fd..4c63ce703d 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -1,17 +1,22 @@ -using MongoDB.Driver; +using MongoDB.Bson; +using MongoDB.Driver; using MongoDB.Driver.Linq; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; +using Volo.Abp.Uow; namespace Volo.Abp.Identity.MongoDB { - public class MongoOrganizationUnitRepository : MongoDbRepository, IOrganizationUnitRepository + public class MongoOrganizationUnitRepository + : MongoDbRepository, + IOrganizationUnitRepository { public MongoOrganizationUnitRepository( IMongoDbContextProvider dbContextProvider) @@ -134,5 +139,30 @@ namespace Volo.Abp.Identity.MongoDB .As>() .CountAsync(GetCancellationToken(cancellationToken)); } + + public virtual Task RemoveAllRolesAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + { + organizationUnit.Roles.Clear(); + return Task.FromResult(0); + } + + public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + { + var users = await DbContext.Users.AsQueryable() + .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)) + ; + + //var filter = Builders.Filter.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)); + + //var update = Builders.Update.PullFilter(y => y.OrganizationUnits, iou=> iou); + //var result = await DbContext.Users.UpdateManyAsync(filter, update); + + for (int i = 0; i < users.Count; i++) + { + users[i].RemoveOrganizationUnit(organizationUnit.Id); + } + } } } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs index 6f6835396d..bbb7d649c9 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs @@ -151,5 +151,29 @@ namespace Volo.Abp.Identity rolesCount.ShouldBeGreaterThan(1); } + + [Fact] + public async Task RemoveAllMembersOfOrganizationUnit() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var membersCount = await _organizationUnitRepository.GetMembersCountAsync(ou); + membersCount.ShouldBeGreaterThan(1); + + await _organizationUnitRepository.RemoveAllMembersAsync(ou); + var newCount = await _organizationUnitRepository.GetMembersCountAsync(ou); + newCount.ShouldBe(0); + } + + [Fact] + public async Task RemoveAllRolesOfOrganizationUnit() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var rolesCount = await _organizationUnitRepository.GetRolesCountAsync(ou); + rolesCount.ShouldBeGreaterThan(1); + + await _organizationUnitRepository.RemoveAllRolesAsync(ou); + var newCount = await _organizationUnitRepository.GetRolesCountAsync(ou); + newCount.ShouldBe(0); + } } } From d99a00164a83c37afadc45fa4943ff081d4c01d6 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 2 Jun 2020 04:43:04 +0300 Subject: [PATCH 2/5] Updated IdentityUserManager Added includeDetails for query --- .../Volo/Abp/Identity/IdentityUserManager.cs | 3 ++- .../Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs | 2 +- 2 files changed, 3 insertions(+), 2 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 d22eef5af6..2400743ab3 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 @@ -190,12 +190,13 @@ namespace Volo.Abp.Identity } [UnitOfWork] - public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user) + public virtual async Task> GetOrganizationUnitsAsync(IdentityUser user, bool includeDetails = false) { await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); return await OrganizationUnitRepository.GetListAsync( user.OrganizationUnits.Select(t => t.OrganizationUnitId), + includeDetails, cancellationToken: CancellationToken ); } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index 4c63ce703d..88db03a7f2 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -158,7 +158,7 @@ namespace Volo.Abp.Identity.MongoDB //var update = Builders.Update.PullFilter(y => y.OrganizationUnits, iou=> iou); //var result = await DbContext.Users.UpdateManyAsync(filter, update); - + //TODO: gterdem - Not working, not saving/updating the user. Fix for (int i = 0; i < users.Count; i++) { users[i].RemoveOrganizationUnit(organizationUnit.Id); From d82f472dba43e23ad3faaca39e5cd0ce8998e704 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 2 Jun 2020 16:21:09 +0300 Subject: [PATCH 3/5] OrganizationUnitManager delete update Internally remove members and roles of deleting organization unit --- .../Volo/Abp/Identity/OrganizationUnitManager.cs | 4 ++++ .../MongoDB/MongoOrganizationUnitRepository.cs | 13 ++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs index bfaa522dd0..fb5a5278f2 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs @@ -80,6 +80,10 @@ namespace Volo.Abp.Identity await OrganizationUnitRepository.DeleteAsync(child); } + var organizationUnit = await OrganizationUnitRepository.GetAsync(id); + + await OrganizationUnitRepository.RemoveAllMembersAsync(organizationUnit); + await OrganizationUnitRepository.RemoveAllRolesAsync(organizationUnit); await OrganizationUnitRepository.DeleteAsync(id); } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index 88db03a7f2..943d570153 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -151,17 +151,12 @@ namespace Volo.Abp.Identity.MongoDB var users = await DbContext.Users.AsQueryable() .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .As>() - .ToListAsync(GetCancellationToken(cancellationToken)) - ; - - //var filter = Builders.Filter.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)); + .ToListAsync(GetCancellationToken(cancellationToken)); - //var update = Builders.Update.PullFilter(y => y.OrganizationUnits, iou=> iou); - //var result = await DbContext.Users.UpdateManyAsync(filter, update); - //TODO: gterdem - Not working, not saving/updating the user. Fix - for (int i = 0; i < users.Count; i++) + foreach (var user in users) { - users[i].RemoveOrganizationUnit(organizationUnit.Id); + user.RemoveOrganizationUnit(organizationUnit.Id); + DbContext.Users.ReplaceOne(u => u.Id == user.Id, user); } } } From 133fbf3780b093ddcee4624c1b557111ce7de834 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Tue, 2 Jun 2020 18:51:22 +0300 Subject: [PATCH 4/5] BookStore sample - added enum localization --- docs/en/Tutorials/Part-1.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/en/Tutorials/Part-1.md b/docs/en/Tutorials/Part-1.md index be812cc162..bd08cbdcd5 100644 --- a/docs/en/Tutorials/Part-1.md +++ b/docs/en/Tutorials/Part-1.md @@ -607,7 +607,7 @@ namespace Acme.BookStore.Web.Menus { //<-- added the below code context.Menu.AddItem( - new ApplicationMenuItem("BooksStore", l["Menu:BookStore"]) + new ApplicationMenuItem("BooksStore", l["Menu:BookStore"], icon: "fa fa-book") .AddItem( new ApplicationMenuItem("BooksStore.Books", l["Menu:Books"], url: "/Books") ) @@ -646,7 +646,16 @@ Open the `en.json` (*English translations*) file and add the below localization "Type": "Type", "Price": "Price", "CreationTime": "Creation time", - "AreYouSureToDelete": "Are you sure you want to delete this item?" + "AreYouSureToDelete": "Are you sure you want to delete this item?", + "Enum:BookType:0": "Undefined", + "Enum:BookType:1": "Adventure", + "Enum:BookType:2": "Biography", + "Enum:BookType:3": "Dystopia", + "Enum:BookType:4": "Fantastic", + "Enum:BookType:5": "Horror", + "Enum:BookType:6": "Science", + "Enum:BookType:7": "ScienceFiction", + "Enum:BookType:8": "Poetry" } } ```` @@ -716,7 +725,11 @@ $(function () { ajax: abp.libs.datatables.createAjax(acme.bookStore.book.getList), columnDefs: [ { data: "name" }, - { data: "type" }, + { data: "type", + render: function(data){ + return l('Enum:BookType:' + data); + } + }, { data: "publishDate" }, { data: "price" }, { data: "creationTime" } From 8461ea0b5855583cff4b0c88ec7e764a428f0a2a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 3 Jun 2020 01:45:13 +0300 Subject: [PATCH 5/5] fix(core): throwing error resolves #4171 --- npm/ng-packs/packages/core/src/lib/states/config.state.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/states/config.state.ts b/npm/ng-packs/packages/core/src/lib/states/config.state.ts index 5c5338a948..0618f127c5 100644 --- a/npm/ng-packs/packages/core/src/lib/states/config.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/config.state.ts @@ -239,8 +239,8 @@ export class ConfigState { ? of(null) : dispatch(new SetLanguage(defaultLang, false)); }), - catchError(err => { - dispatch(new RestOccurError(new HttpErrorResponse({ status: 0, error: err }))); + catchError((err: HttpErrorResponse) => { + dispatch(new RestOccurError(err)); return throwError(err); }), );