Browse Source
feat(identity): added custom identity resource seeder, default preset avatarUrl resourcepull/396/head
committed by
GitHub
9 changed files with 143 additions and 14 deletions
@ -0,0 +1,73 @@ |
|||||
|
using Microsoft.Extensions.Options; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Guids; |
||||
|
using Volo.Abp.Identity; |
||||
|
using Volo.Abp.IdentityServer.IdentityResources; |
||||
|
|
||||
|
namespace LINGYUN.Abp.IdentityServer.IdentityResources |
||||
|
{ |
||||
|
public class CustomIdentityResourceDataSeeder : ICustomIdentityResourceDataSeeder, ITransientDependency |
||||
|
{ |
||||
|
protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } |
||||
|
protected IIdentityResourceRepository IdentityResourceRepository { get; } |
||||
|
protected IGuidGenerator GuidGenerator { get; } |
||||
|
protected CustomIdentityResourceDataSeederOptions Options { get; } |
||||
|
|
||||
|
public CustomIdentityResourceDataSeeder( |
||||
|
IIdentityResourceRepository identityResourceRepository, |
||||
|
IGuidGenerator guidGenerator, |
||||
|
IIdentityClaimTypeRepository claimTypeRepository, |
||||
|
IOptions<CustomIdentityResourceDataSeederOptions> options) |
||||
|
{ |
||||
|
IdentityResourceRepository = identityResourceRepository; |
||||
|
GuidGenerator = guidGenerator; |
||||
|
ClaimTypeRepository = claimTypeRepository; |
||||
|
Options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task CreateCustomResourcesAsync() |
||||
|
{ |
||||
|
foreach (var resource in Options.Resources) |
||||
|
{ |
||||
|
foreach (var claimType in resource.UserClaims) |
||||
|
{ |
||||
|
await AddClaimTypeIfNotExistsAsync(claimType); |
||||
|
} |
||||
|
|
||||
|
await AddIdentityResourceIfNotExistsAsync(resource); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task AddIdentityResourceIfNotExistsAsync(IdentityServer4.Models.IdentityResource resource) |
||||
|
{ |
||||
|
if (await IdentityResourceRepository.CheckNameExistAsync(resource.Name)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
await IdentityResourceRepository.InsertAsync( |
||||
|
new IdentityResource( |
||||
|
GuidGenerator.Create(), |
||||
|
resource |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task AddClaimTypeIfNotExistsAsync(string claimType) |
||||
|
{ |
||||
|
if (await ClaimTypeRepository.AnyAsync(claimType)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
await ClaimTypeRepository.InsertAsync( |
||||
|
new IdentityClaimType( |
||||
|
GuidGenerator.Create(), |
||||
|
claimType, |
||||
|
isStatic: true |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
using IdentityServer4.Models; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace LINGYUN.Abp.IdentityServer.IdentityResources |
||||
|
{ |
||||
|
public class CustomIdentityResourceDataSeederOptions |
||||
|
{ |
||||
|
public IList<IdentityResource> Resources { get; } |
||||
|
public CustomIdentityResourceDataSeederOptions() |
||||
|
{ |
||||
|
Resources = new List<IdentityResource>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace LINGYUN.Abp.IdentityServer.IdentityResources |
||||
|
{ |
||||
|
public interface ICustomIdentityResourceDataSeeder |
||||
|
{ |
||||
|
Task CreateCustomResourcesAsync(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using IdentityServer4.Models; |
||||
|
|
||||
|
namespace AuthServer.IdentityResources |
||||
|
{ |
||||
|
public class CustomIdentityResources |
||||
|
{ |
||||
|
public class AvatarUrl : IdentityResource |
||||
|
{ |
||||
|
public static string ClaimType { get; set; } = "avatarUrl"; |
||||
|
public AvatarUrl() |
||||
|
{ |
||||
|
Name = ClaimType; |
||||
|
DisplayName = "Your avatar url"; |
||||
|
Emphasize = true; |
||||
|
UserClaims = new string[] { ClaimType }; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue