diff --git a/services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs b/services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs index 4a514392..44a481aa 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs @@ -11,5 +11,6 @@ public const string DefaultAdminEmailAddress = "admin@abp.io"; public const string DefaultAdminPassword = "1q2w3E*"; + public const string CustomerRoleName = "customer"; } } diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityPublicRoleDataSeeder.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityPublicRoleDataSeeder.cs new file mode 100644 index 00000000..0ef2c65e --- /dev/null +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityPublicRoleDataSeeder.cs @@ -0,0 +1,51 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; +using Volo.Abp.Identity; +using IdentityRole = Volo.Abp.Identity.IdentityRole; + +namespace EShopOnAbp.IdentityService.DbMigrations; + +public class IdentityPublicRoleDataSeeder : IDataSeedContributor, ITransientDependency +{ + private readonly IIdentityRoleRepository _roleRepository; + private readonly ILookupNormalizer _lookupNormalizer; + private readonly IGuidGenerator _guidGenerator; + private readonly IdentityRoleManager _roleManager; + + public IdentityPublicRoleDataSeeder( + IIdentityRoleRepository roleRepository, + ILookupNormalizer lookupNormalizer, + IGuidGenerator guidGenerator, + IdentityRoleManager roleManager) + { + _roleRepository = roleRepository; + _lookupNormalizer = lookupNormalizer; + _guidGenerator = guidGenerator; + _roleManager = roleManager; + } + + public async Task SeedAsync(DataSeedContext context) + { + await CreateCustomerRoleAsync(); + } + + private async Task CreateCustomerRoleAsync() + { + //"customer" role + var customerRole = + await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName(IdentityServiceDbProperties.CustomerRoleName)); + if (customerRole == null) + { + customerRole = new IdentityRole(_guidGenerator.Create(), IdentityServiceDbProperties.CustomerRoleName) + { + IsStatic = true, + IsPublic = true + }; + + (await _roleManager.CreateAsync(customerRole)).CheckErrors(); + } + } +} \ No newline at end of file diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/Handlers/IdentityUserCreationHandler.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/Handlers/IdentityUserCreationHandler.cs new file mode 100644 index 00000000..fdcb33aa --- /dev/null +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/Handlers/IdentityUserCreationHandler.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Identity; +using Volo.Abp.Uow; +using Volo.Abp.Users; + +namespace EShopOnAbp.IdentityService.Handlers; + +public class IdentityUserCreationHandler : IDistributedEventHandler>, ITransientDependency +{ + private readonly IdentityUserManager _identityUserManager; + + public IdentityUserCreationHandler(IdentityUserManager identityUserManager) + { + _identityUserManager = identityUserManager; + } + + [UnitOfWork] + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + if (eventData.Entity.Email != IdentityServiceDbProperties.DefaultAdminEmailAddress) + { + var identityUser = await _identityUserManager.FindByIdAsync(eventData.Entity.Id.ToString()); + await _identityUserManager.AddToRoleAsync(identityUser, IdentityServiceDbProperties.CustomerRoleName); + } + } +} \ No newline at end of file