| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Identity.Entities; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | 5 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Persistence.EFCore.Modules.Identity; |
| | | 8 | | |
| | | 9 | | internal class Configurations : IEntityTypeConfiguration<User>, IEntityTypeConfiguration<Application>, IEntityTypeConfig |
| | | 10 | | { |
| | 3 | 11 | | private static Expression<Func<ICollection<string>, string>> StringCollectionToStringConverter => v => string.Join(" |
| | 3 | 12 | | private static Expression<Func<string, ICollection<string>>> StringToStringCollectionConverter => v => v.Split(",", |
| | | 13 | | |
| | 1 | 14 | | private static readonly ValueComparer<ICollection<string>> StringCollectionComparer = new( |
| | 1 | 15 | | (c1, c2) => c1!.SequenceEqual(c2!), |
| | 1 | 16 | | c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())), |
| | 1 | 17 | | c => c.ToList()); |
| | | 18 | | |
| | | 19 | | public void Configure(EntityTypeBuilder<User> builder) |
| | | 20 | | { |
| | | 21 | | // Uniqueness is per tenant, not global. A global unique index made it impossible for two tenants |
| | | 22 | | // to hold a role or user of the same name -- so "roles are configurable per tenant" could not be |
| | | 23 | | // true, however the rest of the stack behaved. |
| | 1 | 24 | | builder.HasIndex(x => new { x.TenantId, x.Name }).HasDatabaseName($"IX_{nameof(User)}_{nameof(User.TenantId)}_{n |
| | 1 | 25 | | builder.HasIndex(x => x.TenantId).HasDatabaseName($"IX_{nameof(User)}_{nameof(User.TenantId)}"); |
| | 1 | 26 | | builder.Property(x => x.Roles).HasColumnName("Roles").HasConversion(StringCollectionToStringConverter, StringToS |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void Configure(EntityTypeBuilder<Application> builder) |
| | | 30 | | { |
| | 1 | 31 | | builder.HasIndex(x => new { x.TenantId, x.ClientId }).HasDatabaseName($"IX_{nameof(Application)}_{nameof(Applica |
| | 1 | 32 | | builder.HasIndex(x => new { x.TenantId, x.Name }).HasDatabaseName($"IX_{nameof(Application)}_{nameof(Application |
| | 1 | 33 | | builder.HasIndex(x => x.TenantId).HasDatabaseName($"IX_{nameof(Application)}_{nameof(Application.TenantId)}"); |
| | 1 | 34 | | builder.Property(x => x.Roles).HasColumnName("Roles").HasConversion(StringCollectionToStringConverter, StringToS |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public void Configure(EntityTypeBuilder<Role> builder) |
| | | 38 | | { |
| | 1 | 39 | | builder.HasIndex(x => new { x.TenantId, x.Name }).HasDatabaseName($"IX_{nameof(Role)}_{nameof(Role.TenantId)}_{n |
| | 1 | 40 | | builder.HasIndex(x => x.TenantId).HasDatabaseName($"IX_{nameof(Role)}_{nameof(Role.TenantId)}"); |
| | 1 | 41 | | builder.Property(x => x.Permissions).HasColumnName("Permissions").HasConversion(StringCollectionToStringConverte |
| | 1 | 42 | | } |
| | | 43 | | } |