Use constants in EF entity configuration.

pull/4578/head
maliming 5 years ago
parent 6f6d238bb5
commit a40b35a089

@ -0,0 +1,7 @@
namespace Volo.Abp.IdentityServer.ApiResources
{
public class ApiResourceScopeConsts
{
public const int ScopeMaxLength = 200;
}
}

@ -1,22 +1,22 @@
namespace Volo.Abp.IdentityServer
namespace Volo.Abp.IdentityServer.ApiResources
{
public class SecretConsts
public class ApiResourceSecretConsts
{
/// <summary>
/// Default value: 250
/// </summary>
public static int TypeMaxLength { get; set; } = 250;
/// <summary>
/// Default value: 4000
/// </summary>
public static int ValueMaxLength { get; set; } = 4000;
public static int ValueMaxLengthValue { get; set; } = ValueMaxLength;
/// <summary>
/// Default value: 2000
/// </summary>
public static int DescriptionMaxLength { get; set; } = 2000;
}
}
}

@ -1,7 +0,0 @@
namespace Volo.Abp.IdentityServer.ApiResources
{
public class ApiScopeConsts
{
public const int NameMaxLength = 200;
}
}

@ -0,0 +1,9 @@
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScopeConsts
{
public const int NameMaxLength = 200;
public const int DisplayNameMaxLength = 200;
public const int DescriptionMaxLength = 1000;
}
}

@ -0,0 +1,7 @@
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScopeClaimConsts
{
public const int NameMaxLength = 200;
}
}

@ -0,0 +1,9 @@
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScopePropertyConsts
{
public const int KeyMaxLength = 250;
public const int ValueMaxLength = 2000;
public static int ValueMaxLengthValue { get; set; } = ValueMaxLength;
}
}

@ -0,0 +1,12 @@
namespace Volo.Abp.IdentityServer.Devices
{
public class DeviceFlowCodesConsts
{
public const int DeviceCodeMaxLength = 200;
public const int UserCodeMaxLength = 200;
public const int SubjectIdMaxLength = 200;
public const int ClientIdMaxLength = 200;
public const int DataMaxLength = 50000;
public static int DataMaxLengthValue { get; set; } = DataMaxLength;
}
}

@ -0,0 +1,9 @@
namespace Volo.Abp.IdentityServer.IdentityResources
{
public class IdentityResourcePropertyConsts
{
public const int KeyMaxLength = 250;
public const int ValueMaxLength = 2000;
public static int ValueMaxLengthValue { get; set; } = ValueMaxLength;
}
}

@ -4,7 +4,7 @@ using System.Linq;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Auditing;
namespace Volo.Abp.IdentityServer.ApiResources
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScope : FullAuditedAggregateRoot<Guid>
{

@ -1,7 +1,7 @@
using System;
using JetBrains.Annotations;
namespace Volo.Abp.IdentityServer.ApiResources
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScopeClaim : UserClaim
{

@ -2,7 +2,7 @@
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.IdentityServer.ApiResources
namespace Volo.Abp.IdentityServer.ApiScopes
{
public class ApiScopeProperty : Entity
{

@ -3,7 +3,7 @@ using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
namespace Volo.Abp.IdentityServer.ApiResources
namespace Volo.Abp.IdentityServer.ApiScopes
{
public interface IApiScopeRepository : IBasicRepository<ApiScope>
{

@ -2,6 +2,7 @@
using System.Security.Claims;
using AutoMapper;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Stores;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.IdentityResources;
using Volo.Abp.ObjectMapping;
@ -43,7 +44,7 @@ namespace Volo.Abp.IdentityServer
public virtual async Task<IEnumerable<IdentityServer4.Models.ApiScope>> FindApiScopesByNameAsync(IEnumerable<string> scopeNames)
{
var scopes = await ApiScopeRepository.GetListByNameAsync(scopeNames.ToArray(), includeDetails: true);
return ObjectMapper.Map<List<Volo.Abp.IdentityServer.ApiResources.ApiScope>, List<IdentityServer4.Models.ApiScope>>(scopes);
return ObjectMapper.Map<List<Volo.Abp.IdentityServer.ApiScopes.ApiScope>, List<IdentityServer4.Models.ApiScope>>(scopes);
}
/// <summary>
@ -76,7 +77,7 @@ namespace Volo.Abp.IdentityServer
return new Resources(
ObjectMapper.Map<List<Volo.Abp.IdentityServer.IdentityResources.IdentityResource>, List<IdentityServer4.Models.IdentityResource>>(identityResources),
ObjectMapper.Map<List<Volo.Abp.IdentityServer.ApiResources.ApiResource>, List<IdentityServer4.Models.ApiResource>>(apiResources),
ObjectMapper.Map<List<Volo.Abp.IdentityServer.ApiResources.ApiScope>, List<IdentityServer4.Models.ApiScope>>(apiScopes));
ObjectMapper.Map<List<Volo.Abp.IdentityServer.ApiScopes.ApiScope>, List<IdentityServer4.Models.ApiScope>>(apiScopes));
}
}
}

@ -83,16 +83,14 @@ namespace Volo.Abp.IdentityServer.ApiResources
public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default)
{
var scopeClaims = DbContext.Set<ApiScopeClaim>().Where(sc => sc.ApiScopeId == id);
foreach (var scopeClaim in scopeClaims)
var resourceClaims = DbContext.Set<ApiResourceClaim>().Where(sc => sc.ApiResourceId == id);
foreach (var scopeClaim in resourceClaims)
{
DbContext.Set<ApiScopeClaim>().Remove(scopeClaim);
DbContext.Set<ApiResourceClaim>().Remove(scopeClaim);
}
var scopes = DbContext.Set<ApiResourceScope>().Where(s => s.ApiResourceId == id);
foreach (var scope in scopes)
var resourceScopes = DbContext.Set<ApiResourceScope>().Where(s => s.ApiResourceId == id);
foreach (var scope in resourceScopes)
{
DbContext.Set<ApiResourceScope>().Remove(scope);
}

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
namespace Volo.Abp.IdentityServer.ApiResources

@ -2,6 +2,7 @@
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
@ -12,20 +13,41 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
[ConnectionStringName(AbpIdentityServerDbProperties.ConnectionStringName)]
public interface IIdentityServerDbContext : IEfCoreDbContext
{
#region ApiResource
DbSet<ApiResource> ApiResources { get; set; }
DbSet<ApiResourceSecret> ApiSecrets { get; set; }
DbSet<ApiResourceSecret> ApiResourceSecrets { get; set; }
DbSet<ApiResourceClaim> ApiResourceClaims { get; set; }
DbSet<ApiResourceScope> ApiScopes { get; set; }
DbSet<ApiResourceScope> ApiResourceScopes { get; set; }
#endregion
#region ApiScope
DbSet<ApiScope> ApiScopes { get; set; }
DbSet<ApiScopeClaim> ApiScopeClaims { get; set; }
DbSet<ApiScopeProperty> ApiScopeProperties { get; set; }
#endregion
#region IdentityResource
DbSet<IdentityResource> IdentityResources { get; set; }
DbSet<IdentityResourceClaim> IdentityClaims { get; set; }
DbSet<IdentityResourceProperty> IdentityResourceProperties { get; set; }
#endregion
#region Client
DbSet<Client> Clients { get; set; }
DbSet<ClientGrantType> ClientGrantTypes { get; set; }
@ -46,6 +68,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
DbSet<ClientProperty> ClientProperties { get; set; }
#endregion
DbSet<PersistedGrant> PersistedGrants { get; set; }
DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }

@ -2,6 +2,7 @@
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
@ -12,20 +13,41 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
[ConnectionStringName(AbpIdentityServerDbProperties.ConnectionStringName)]
public class IdentityServerDbContext : AbpDbContext<IdentityServerDbContext>, IIdentityServerDbContext
{
#region ApiResource
public DbSet<ApiResource> ApiResources { get; set; }
public DbSet<ApiResourceSecret> ApiSecrets { get; set; }
public DbSet<ApiResourceSecret> ApiResourceSecrets { get; set; }
public DbSet<ApiResourceClaim> ApiResourceClaims { get; set; }
public DbSet<ApiResourceScope> ApiScopes { get; set; }
public DbSet<ApiResourceScope> ApiResourceScopes { get; set; }
#endregion
#region ApiScope
public DbSet<ApiScope> ApiScopes { get; set; }
public DbSet<ApiScopeClaim> ApiScopeClaims { get; set; }
public DbSet<ApiScopeProperty> ApiScopeProperties { get; set; }
#endregion
#region IdentityResource
public DbSet<IdentityResource> IdentityResources { get; set; }
public DbSet<IdentityResourceClaim> IdentityClaims { get; set; }
public DbSet<IdentityResourceProperty> IdentityResourceProperties { get; set; }
#endregion
#region Client
public DbSet<Client> Clients { get; set; }
public DbSet<ClientGrantType> ClientGrantTypes { get; set; }
@ -46,6 +68,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
public DbSet<ClientProperty> ClientProperties { get; set; }
#endregion
public DbSet<PersistedGrant> PersistedGrants { get; set; }
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }

@ -6,10 +6,16 @@ using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.EntityFrameworkCore.ValueComparers;
using Volo.Abp.EntityFrameworkCore.ValueConverters;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
using Volo.Abp.IdentityServer.IdentityResources;
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
using Client = Volo.Abp.IdentityServer.Clients.Client;
using ClientClaim = Volo.Abp.IdentityServer.Clients.ClientClaim;
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
using PersistedGrant = Volo.Abp.IdentityServer.Grants.PersistedGrant;
namespace Volo.Abp.IdentityServer.EntityFrameworkCore
{
@ -28,6 +34,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
optionsAction?.Invoke(options);
#region Client
builder.Entity<Client>(b =>
{
b.ToTable(options.TablePrefix + "Clients", options.Schema);
@ -123,16 +131,16 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.ClientId, x.Type, x.Value});
b.Property(x => x.Type).HasMaxLength(SecretConsts.TypeMaxLength).IsRequired();
b.Property(x => x.Type).HasMaxLength(ApiResourceSecretConsts.TypeMaxLength).IsRequired();
if (IsDatabaseProvider(builder, options, EfCoreDatabaseProvider.MySql, EfCoreDatabaseProvider.Oracle))
{
SecretConsts.ValueMaxLengthValue = 300;
ApiResourceSecretConsts.ValueMaxLengthValue = 300;
}
b.Property(x => x.Value).HasMaxLength(SecretConsts.ValueMaxLengthValue).IsRequired();
b.Property(x => x.Value).HasMaxLength(ApiResourceSecretConsts.ValueMaxLengthValue).IsRequired();
b.Property(x => x.Description).HasMaxLength(SecretConsts.DescriptionMaxLength);
b.Property(x => x.Description).HasMaxLength(ApiResourceSecretConsts.DescriptionMaxLength);
});
builder.Entity<ClientClaim>(b =>
@ -206,6 +214,10 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasIndex(x => x.Expiration);
});
#endregion
#region IdentityResource
builder.Entity<IdentityResource>(b =>
{
b.ToTable(options.TablePrefix + "IdentityResources", options.Schema);
@ -216,6 +228,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.Property(x => x.DisplayName).HasMaxLength(IdentityResourceConsts.DisplayNameMaxLength);
b.Property(x => x.Description).HasMaxLength(IdentityResourceConsts.DescriptionMaxLength);
b.HasIndex(x => x.Name).IsUnique();
b.HasMany(x => x.UserClaims).WithOne().HasForeignKey(x => x.IdentityResourceId).IsRequired();
b.HasMany(x => x.Properties).WithOne().HasForeignKey(x => x.IdentityResourceId).IsRequired();
});
@ -239,11 +253,19 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.IdentityResourceId, x.Key});
b.Property(x => x.Key).HasMaxLength(250).IsRequired();
b.Property(x => x.Value).HasMaxLength(2000).IsRequired();
b.Property(x => x.Key).HasMaxLength(IdentityResourcePropertyConsts.KeyMaxLength).IsRequired();
if (IsDatabaseProvider(builder, options, EfCoreDatabaseProvider.MySql, EfCoreDatabaseProvider.Oracle))
{
IdentityResourcePropertyConsts.ValueMaxLengthValue = 300;
}
b.Property(x => x.Value).HasMaxLength(IdentityResourcePropertyConsts.ValueMaxLengthValue).IsRequired();
});
builder.Entity<ApiResource>(b =>
#endregion
#region ApiResource
builder.Entity<ApiResource>(b =>
{
b.ToTable(options.TablePrefix + "ApiResources", options.Schema);
@ -269,15 +291,15 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.ApiResourceId, x.Type, x.Value});
b.Property(x => x.Type).HasMaxLength(SecretConsts.TypeMaxLength).IsRequired();
b.Property(x => x.Description).HasMaxLength(SecretConsts.DescriptionMaxLength);
b.Property(x => x.Type).HasMaxLength(ApiResourceSecretConsts.TypeMaxLength).IsRequired();
b.Property(x => x.Description).HasMaxLength(ApiResourceSecretConsts.DescriptionMaxLength);
if (IsDatabaseProvider(builder, options, EfCoreDatabaseProvider.MySql, EfCoreDatabaseProvider.Oracle))
{
SecretConsts.ValueMaxLengthValue = 300;
ApiResourceSecretConsts.ValueMaxLengthValue = 300;
}
b.Property(x => x.Value).HasMaxLength(SecretConsts.ValueMaxLengthValue).IsRequired();
b.Property(x => x.Value).HasMaxLength(ApiResourceSecretConsts.ValueMaxLengthValue).IsRequired();
});
builder.Entity<ApiResourceClaim>(b =>
@ -299,24 +321,28 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.ApiResourceId, x.Scope});
b.Property(x => x.Scope).HasMaxLength(ApiScopeConsts.NameMaxLength).IsRequired();
b.Property(x => x.Scope).HasMaxLength(ApiResourceScopeConsts.ScopeMaxLength).IsRequired();
});
#endregion
#region ApiScope
builder.Entity<ApiScope>(b =>
{
b.ToTable(options.TablePrefix + "ApiScopes", options.Schema);
b.ConfigureByConvention();
b.Property(x => x.Name).HasMaxLength(200).IsRequired();
b.Property(x => x.DisplayName).HasMaxLength(200);
b.Property(x => x.Description).HasMaxLength(1000);
b.Property(x => x.Name).HasMaxLength(ApiScopeConsts.NameMaxLength).IsRequired();
b.Property(x => x.DisplayName).HasMaxLength(ApiScopeConsts.DisplayNameMaxLength);
b.Property(x => x.Description).HasMaxLength(ApiScopeConsts.DescriptionMaxLength);
b.HasIndex(x => x.Name).IsUnique();
b.HasMany(x => x.UserClaims).WithOne().HasForeignKey(x => x.ApiScopeId).IsRequired();
//Identity Server does not configure the relationship of Properties
//TODO: Identity Server does not configure the relationship of Properties
//b.HasMany(x => x.Properties).WithOne().HasForeignKey(x => x.ApiScopeId).IsRequired();
});
@ -329,7 +355,7 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.ApiScopeId, x.Name, x.Type});
b.Property(x => x.Type).HasMaxLength(UserClaimConsts.TypeMaxLength).IsRequired();
b.Property(x => x.Name).HasMaxLength(ApiScopeConsts.NameMaxLength).IsRequired();
b.Property(x => x.Name).HasMaxLength(ApiScopeClaimConsts.NameMaxLength).IsRequired();
});
builder.Entity<ApiScopeProperty>(b =>
@ -340,28 +366,42 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
b.HasKey(x => new {x.ApiScopeId, x.Key});
b.Property(x => x.Key).HasMaxLength(250).IsRequired();
//oracle?
b.Property(x => x.Value).HasMaxLength(2000).IsRequired();
b.Property(x => x.Key).HasMaxLength(ApiScopePropertyConsts.KeyMaxLength).IsRequired();
if (IsDatabaseProvider(builder, options, EfCoreDatabaseProvider.MySql, EfCoreDatabaseProvider.Oracle))
{
ApiScopePropertyConsts.ValueMaxLengthValue = 300;
}
b.Property(x => x.Value).HasMaxLength(ApiScopePropertyConsts.ValueMaxLengthValue).IsRequired();
});
#endregion
#region DeviceFlowCodes
builder.Entity<DeviceFlowCodes>(b =>
{
b.ToTable(options.TablePrefix + "DeviceFlowCodes", options.Schema);
b.ConfigureByConvention();
b.Property(x => x.DeviceCode).HasMaxLength(200).IsRequired();
b.Property(x => x.UserCode).HasMaxLength(200).IsRequired();
b.Property(x => x.SubjectId).HasMaxLength(200);
b.Property(x => x.ClientId).HasMaxLength(200).IsRequired();
b.Property(x => x.DeviceCode).HasMaxLength(DeviceFlowCodesConsts.DeviceCodeMaxLength).IsRequired();
b.Property(x => x.UserCode).HasMaxLength(DeviceFlowCodesConsts.UserCodeMaxLength).IsRequired();
b.Property(x => x.SubjectId).HasMaxLength(DeviceFlowCodesConsts.SubjectIdMaxLength);
b.Property(x => x.ClientId).HasMaxLength(DeviceFlowCodesConsts.ClientIdMaxLength).IsRequired();
b.Property(x => x.Expiration).IsRequired();
b.Property(x => x.Data).HasMaxLength(50000).IsRequired();
if (IsDatabaseProvider(builder, options, EfCoreDatabaseProvider.MySql))
{
DeviceFlowCodesConsts.DataMaxLengthValue = 10000; //TODO: MySQL accepts 20.000. We can consider to change in v3.0.
}
b.Property(x => x.Data).HasMaxLength(DeviceFlowCodesConsts.DataMaxLengthValue).IsRequired();
b.HasIndex(x => new {x.UserCode}).IsUnique();
b.HasIndex(x => x.DeviceCode).IsUnique();
b.HasIndex(x => x.Expiration);
});
#endregion
}
private static bool IsDatabaseProvider(

@ -1,6 +1,7 @@
using MongoDB.Driver;
using Volo.Abp.Data;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.ApiScopes;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
@ -14,6 +15,8 @@ namespace Volo.Abp.IdentityServer.MongoDB
{
public IMongoCollection<ApiResource> ApiResources => Collection<ApiResource>();
public IMongoCollection<ApiScope> ApiScopes => Collection<ApiScope>();
public IMongoCollection<Client> Clients => Collection<Client>();
public IMongoCollection<IdentityResource> IdentityResources => Collection<IdentityResource>();

Loading…
Cancel
Save