IdentityDataSeedResult returns result of the operation

pull/765/head
Halil ibrahim Kalkan 6 years ago
parent a0404e574e
commit 1426a20ff7

@ -5,7 +5,7 @@ namespace Volo.Abp.Identity
{
public interface IIdentityDataSeeder
{
Task SeedAsync(
Task<IdentityDataSeedResult> SeedAsync(
string adminUserPassword,
Guid? tenantId = null);
}

@ -0,0 +1,9 @@
namespace Volo.Abp.Identity
{
public class IdentityDataSeedResult
{
public bool CreatedAdminUser { get; set; }
public bool CreatedAdminRole { get; set; }
}
}

@ -35,10 +35,12 @@ namespace Volo.Abp.Identity
}
[UnitOfWork]
public virtual async Task SeedAsync(
public virtual async Task<IdentityDataSeedResult> SeedAsync(
string adminUserPassword,
Guid? tenantId = null)
{
var result = new IdentityDataSeedResult();
const string adminUserName = "admin";
const string adminRoleName = "admin";
@ -46,11 +48,12 @@ namespace Volo.Abp.Identity
var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName));
if (adminUser != null)
{
return;
return result;
}
adminUser = new IdentityUser(_guidGenerator.Create(), adminUserName, "admin@abp.io", tenantId);
CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword));
result.CreatedAdminUser = true;
//"admin" role
var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));
@ -62,12 +65,13 @@ namespace Volo.Abp.Identity
adminRole.IsPublic = true;
CheckIdentityErrors(await _roleManager.CreateAsync(adminRole));
result.CreatedAdminRole = true;
}
CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName));
}
return result;
}
protected void CheckIdentityErrors(IdentityResult identityResult) //TODO: This is temporary and duplicate code!
{

@ -195,19 +195,22 @@ namespace MyCompanyName.MyProjectName
{
AsyncHelper.RunSync(async () =>
{
await scope.ServiceProvider
var identitySeedResult = await scope.ServiceProvider
.GetRequiredService<IIdentityDataSeeder>()
.SeedAsync(
"1q2w3E*"
);
await scope.ServiceProvider
.GetRequiredService<IPermissionDataSeeder>()
.SeedAsync(
RolePermissionValueProvider.ProviderName,
"admin",
IdentityPermissions.GetAll().Union(MyProjectNamePermissions.GetAll())
);
if (identitySeedResult.CreatedAdminRole)
{
await scope.ServiceProvider
.GetRequiredService<IPermissionDataSeeder>()
.SeedAsync(
RolePermissionValueProvider.ProviderName,
"admin",
IdentityPermissions.GetAll().Union(MyProjectNamePermissions.GetAll())
);
}
});
}
}

Loading…
Cancel
Save