From fbd0d067a0a30dca72fb9730bce176a5e01ad8bd Mon Sep 17 00:00:00 2001 From: Mehmet Perk Date: Fri, 3 Jan 2020 00:02:36 +0300 Subject: [PATCH] efcore layer for organization unit --- .../Volo/Abp/Identity/IdentityUser.cs | 38 ++++++++++++++++++ .../AbpIdentityEntityFrameworkCoreModule.cs | 2 + .../EntityFrameworkCore/IIdentityDbContext.cs | 4 -- .../EntityFrameworkCore/IdentityDbContext.cs | 4 -- ...IdentityDbContextModelBuilderExtensions.cs | 40 +++++++++++++++++++ .../IdentityEfCoreQueryableExtensions.cs | 3 +- 6 files changed, 82 insertions(+), 9 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs index c6ad077456..2afc246394 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Identity; using Volo.Abp.Auditing; using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Guids; +using Volo.Abp.Identity.Organizations; using Volo.Abp.ObjectMapping; using Volo.Abp.Users; @@ -125,6 +126,11 @@ namespace Volo.Abp.Identity /// public virtual ICollection Tokens { get; protected set; } + /// + /// Navigation property for this organization units. + /// + public virtual ICollection OrganizationUnits { get; protected set; } + protected IdentityUser() { ExtraProperties = new Dictionary(); @@ -148,6 +154,7 @@ namespace Volo.Abp.Identity Claims = new Collection(); Logins = new Collection(); Tokens = new Collection(); + OrganizationUnits = new Collection(); ExtraProperties = new Dictionary(); } @@ -277,6 +284,37 @@ namespace Volo.Abp.Identity Tokens.RemoveAll(t => t.LoginProvider == loginProvider && t.Name == name); } + public virtual void AddOrganizationUnit(Guid organizationUnitId) + { + Check.NotNull(organizationUnitId, nameof(organizationUnitId)); + + if (IsInOrganizationUnit(organizationUnitId)) + { + return; + } + + OrganizationUnits.Add(new OrganizationUnitUser(TenantId, Id, organizationUnitId)); + } + + public virtual void RemoveOrganizationUnit(Guid organizationUnitId) + { + Check.NotNull(organizationUnitId, nameof(organizationUnitId)); + + if (IsInOrganizationUnit(organizationUnitId)) + { + return; + } + + OrganizationUnits.RemoveAll(ou => ou.OrganizationUnitId == organizationUnitId); + } + + public virtual bool IsInOrganizationUnit(Guid organizationUnitId) + { + Check.NotNull(organizationUnitId, nameof(organizationUnitId)); + + return OrganizationUnits.Any(ou => ou.OrganizationUnitId == organizationUnitId); + } + public override string ToString() { return $"{base.ToString()}, UserName = {UserName}"; diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs index b3ff5c368b..ab8176a733 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Identity.Organizations; using Volo.Abp.Modularity; using Volo.Abp.Users.EntityFrameworkCore; @@ -16,6 +17,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore options.AddRepository(); options.AddRepository(); options.AddRepository(); + options.AddRepository(); }); } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs index b5d7007767..06cd9ac2c9 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs @@ -15,9 +15,5 @@ namespace Volo.Abp.Identity.EntityFrameworkCore DbSet ClaimTypes { get; set; } DbSet OrganizationUnits { get; set; } - - DbSet OrganizationUnitRoles { get; set; } - - DbSet OrganizationUnitUsers { get; set; } } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs index 576a5d2347..0acf396912 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs @@ -19,10 +19,6 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public DbSet OrganizationUnits { get; set; } - public DbSet OrganizationUnitRoles { get; set; } - - public DbSet OrganizationUnitUsers { get; set; } - public IdentityDbContext(DbContextOptions options) : base(options) { diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs index 76ac038c05..759fe9a1b8 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs @@ -2,6 +2,7 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; +using Volo.Abp.Identity.Organizations; using Volo.Abp.Users.EntityFrameworkCore; namespace Volo.Abp.Identity.EntityFrameworkCore @@ -137,6 +138,45 @@ namespace Volo.Abp.Identity.EntityFrameworkCore b.Property(uc => uc.Description).HasMaxLength(IdentityClaimTypeConsts.MaxDescriptionLength); b.Property(uc => uc.ConcurrencyStamp).IsRequired().IsConcurrencyToken().HasMaxLength(IdentityClaimTypeConsts.MaxConcurrencyStampLength).HasColumnName(nameof(IdentityClaimType.ConcurrencyStamp)); }); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "OrganizationUnits", options.Schema); + + b.ConfigureFullAuditedAggregateRoot(); + + b.Property(ou => ou.Code).IsRequired().HasMaxLength(OrganizationUnitConsts.MaxCodeLength).HasColumnName(nameof(OrganizationUnit.Code)); + b.Property(ou => ou.DisplayName).IsRequired().HasMaxLength(OrganizationUnitConsts.MaxDisplayNameLength).HasColumnName(nameof(OrganizationUnit.DisplayName)); + + b.HasMany(ou => ou.Children).WithOne().HasForeignKey(ou => ou.ParentId); + + b.HasIndex(ou => ou.ParentId); + }); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "OrganizationUnitRoles", options.Schema); + + b.HasKey(ou => new { ou.OrganizationUnitId, ou.RoleId }); + + b.HasOne().WithMany().HasForeignKey(ou => ou.OrganizationUnitId).IsRequired(); + b.HasOne().WithMany().HasForeignKey(ou => ou.RoleId).IsRequired(); + + b.HasIndex(ou => new { ou.RoleId, ou.OrganizationUnitId }); + }); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "OrganizationUnitUsers", options.Schema); + + b.HasKey(ou => new { ou.OrganizationUnitId, ou.UserId }); + + b.HasOne().WithMany().HasForeignKey(ou => ou.OrganizationUnitId).IsRequired(); + b.HasOne().WithMany().HasForeignKey(ou => ou.UserId).IsRequired(); + + b.HasIndex(ou => new { ou.UserId, ou.OrganizationUnitId }); + }); + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs index 8cd598c673..b16f1e4234 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs @@ -16,7 +16,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .Include(x => x.Roles) .Include(x => x.Logins) .Include(x => x.Claims) - .Include(x => x.Tokens); + .Include(x => x.Tokens) + .Include(x => x.OrganizationUnits); } public static IQueryable IncludeDetails(this IQueryable queryable, bool include = true)