Browse Source

efcore layer for organization unit

pull/2563/head
Mehmet Perk 7 years ago
parent
commit
fbd0d067a0
  1. 38
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs
  2. 2
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs
  3. 4
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs
  4. 4
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs
  5. 40
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs
  6. 3
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs

38
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.Auditing;
using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
using Volo.Abp.Users; using Volo.Abp.Users;
@ -125,6 +126,11 @@ namespace Volo.Abp.Identity
/// </summary> /// </summary>
public virtual ICollection<IdentityUserToken> Tokens { get; protected set; } public virtual ICollection<IdentityUserToken> Tokens { get; protected set; }
/// <summary>
/// Navigation property for this organization units.
/// </summary>
public virtual ICollection<OrganizationUnitUser> OrganizationUnits { get; protected set; }
protected IdentityUser() protected IdentityUser()
{ {
ExtraProperties = new Dictionary<string, object>(); ExtraProperties = new Dictionary<string, object>();
@ -148,6 +154,7 @@ namespace Volo.Abp.Identity
Claims = new Collection<IdentityUserClaim>(); Claims = new Collection<IdentityUserClaim>();
Logins = new Collection<IdentityUserLogin>(); Logins = new Collection<IdentityUserLogin>();
Tokens = new Collection<IdentityUserToken>(); Tokens = new Collection<IdentityUserToken>();
OrganizationUnits = new Collection<OrganizationUnitUser>();
ExtraProperties = new Dictionary<string, object>(); ExtraProperties = new Dictionary<string, object>();
} }
@ -277,6 +284,37 @@ namespace Volo.Abp.Identity
Tokens.RemoveAll(t => t.LoginProvider == loginProvider && t.Name == name); 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() public override string ToString()
{ {
return $"{base.ToString()}, UserName = {UserName}"; return $"{base.ToString()}, UserName = {UserName}";

2
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs

@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Users.EntityFrameworkCore; using Volo.Abp.Users.EntityFrameworkCore;
@ -16,6 +17,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
options.AddRepository<IdentityUser, EfCoreIdentityUserRepository>(); options.AddRepository<IdentityUser, EfCoreIdentityUserRepository>();
options.AddRepository<IdentityRole, EfCoreIdentityRoleRepository>(); options.AddRepository<IdentityRole, EfCoreIdentityRoleRepository>();
options.AddRepository<IdentityClaimType, EfCoreIdentityClaimTypeRepository>(); options.AddRepository<IdentityClaimType, EfCoreIdentityClaimTypeRepository>();
options.AddRepository<OrganizationUnit, EfCoreOrganizationUnitRepository>();
}); });
} }
} }

4
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs

@ -15,9 +15,5 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
DbSet<IdentityClaimType> ClaimTypes { get; set; } DbSet<IdentityClaimType> ClaimTypes { get; set; }
DbSet<OrganizationUnit> OrganizationUnits { get; set; } DbSet<OrganizationUnit> OrganizationUnits { get; set; }
DbSet<OrganizationUnitRole> OrganizationUnitRoles { get; set; }
DbSet<OrganizationUnitUser> OrganizationUnitUsers { get; set; }
} }
} }

4
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs

@ -19,10 +19,6 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
public DbSet<OrganizationUnit> OrganizationUnits { get; set; } public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
public DbSet<OrganizationUnitRole> OrganizationUnitRoles { get; set; }
public DbSet<OrganizationUnitUser> OrganizationUnitUsers { get; set; }
public IdentityDbContext(DbContextOptions<IdentityDbContext> options) public IdentityDbContext(DbContextOptions<IdentityDbContext> options)
: base(options) : base(options)
{ {

40
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContextModelBuilderExtensions.cs

@ -2,6 +2,7 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling; using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.Users.EntityFrameworkCore; using Volo.Abp.Users.EntityFrameworkCore;
namespace Volo.Abp.Identity.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.Description).HasMaxLength(IdentityClaimTypeConsts.MaxDescriptionLength);
b.Property(uc => uc.ConcurrencyStamp).IsRequired().IsConcurrencyToken().HasMaxLength(IdentityClaimTypeConsts.MaxConcurrencyStampLength).HasColumnName(nameof(IdentityClaimType.ConcurrencyStamp)); b.Property(uc => uc.ConcurrencyStamp).IsRequired().IsConcurrencyToken().HasMaxLength(IdentityClaimTypeConsts.MaxConcurrencyStampLength).HasColumnName(nameof(IdentityClaimType.ConcurrencyStamp));
}); });
builder.Entity<OrganizationUnit>(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<OrganizationUnitRole>(b =>
{
b.ToTable(options.TablePrefix + "OrganizationUnitRoles", options.Schema);
b.HasKey(ou => new { ou.OrganizationUnitId, ou.RoleId });
b.HasOne<OrganizationUnit>().WithMany().HasForeignKey(ou => ou.OrganizationUnitId).IsRequired();
b.HasOne<IdentityRole>().WithMany().HasForeignKey(ou => ou.RoleId).IsRequired();
b.HasIndex(ou => new { ou.RoleId, ou.OrganizationUnitId });
});
builder.Entity<OrganizationUnitUser>(b =>
{
b.ToTable(options.TablePrefix + "OrganizationUnitUsers", options.Schema);
b.HasKey(ou => new { ou.OrganizationUnitId, ou.UserId });
b.HasOne<OrganizationUnit>().WithMany().HasForeignKey(ou => ou.OrganizationUnitId).IsRequired();
b.HasOne<IdentityUser>().WithMany().HasForeignKey(ou => ou.UserId).IsRequired();
b.HasIndex(ou => new { ou.UserId, ou.OrganizationUnitId });
});
} }
} }
} }

3
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.Roles)
.Include(x => x.Logins) .Include(x => x.Logins)
.Include(x => x.Claims) .Include(x => x.Claims)
.Include(x => x.Tokens); .Include(x => x.Tokens)
.Include(x => x.OrganizationUnits);
} }
public static IQueryable<IdentityRole> IncludeDetails(this IQueryable<IdentityRole> queryable, bool include = true) public static IQueryable<IdentityRole> IncludeDetails(this IQueryable<IdentityRole> queryable, bool include = true)

Loading…
Cancel
Save