Browse Source

Added customer role

pull/196/head
Galip Tolga Erdem 3 years ago
parent
commit
c97a1543c9
  1. 1
      services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs
  2. 51
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityPublicRoleDataSeeder.cs
  3. 29
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/Handlers/IdentityUserCreationHandler.cs

1
services/identity/src/EShopOnAbp.IdentityService.Domain/IdentityServiceDbProperties.cs

@ -11,5 +11,6 @@
public const string DefaultAdminEmailAddress = "admin@abp.io"; public const string DefaultAdminEmailAddress = "admin@abp.io";
public const string DefaultAdminPassword = "1q2w3E*"; public const string DefaultAdminPassword = "1q2w3E*";
public const string CustomerRoleName = "customer";
} }
} }

51
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();
}
}
}

29
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<EntityCreatedEto<UserEto>>, ITransientDependency
{
private readonly IdentityUserManager _identityUserManager;
public IdentityUserCreationHandler(IdentityUserManager identityUserManager)
{
_identityUserManager = identityUserManager;
}
[UnitOfWork]
public virtual async Task HandleEventAsync(EntityCreatedEto<UserEto> eventData)
{
if (eventData.Entity.Email != IdentityServiceDbProperties.DefaultAdminEmailAddress)
{
var identityUser = await _identityUserManager.FindByIdAsync(eventData.Entity.Id.ToString());
await _identityUserManager.AddToRoleAsync(identityUser, IdentityServiceDbProperties.CustomerRoleName);
}
}
}
Loading…
Cancel
Save