diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityDataSeeder.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityDataSeeder.cs index b5c2817341..0ff9e5a55a 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityDataSeeder.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityDataSeeder.cs @@ -5,7 +5,7 @@ namespace Volo.Abp.Identity { public interface IIdentityDataSeeder { - Task SeedAsync( + Task SeedAsync( string adminUserPassword, Guid? tenantId = null); } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeedResult.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeedResult.cs new file mode 100644 index 0000000000..fe6c5bdf17 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeedResult.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.Identity +{ + public class IdentityDataSeedResult + { + public bool CreatedAdminUser { get; set; } + + public bool CreatedAdminRole { get; set; } + } +} \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs index a0ba942ab6..1345ab89bd 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs @@ -35,10 +35,12 @@ namespace Volo.Abp.Identity } [UnitOfWork] - public virtual async Task SeedAsync( + public virtual async Task 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! { diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs b/templates/mvc/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs index 2ca7aa2067..a18f4bb458 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs @@ -195,19 +195,22 @@ namespace MyCompanyName.MyProjectName { AsyncHelper.RunSync(async () => { - await scope.ServiceProvider + var identitySeedResult = await scope.ServiceProvider .GetRequiredService() .SeedAsync( "1q2w3E*" ); - await scope.ServiceProvider - .GetRequiredService() - .SeedAsync( - RolePermissionValueProvider.ProviderName, - "admin", - IdentityPermissions.GetAll().Union(MyProjectNamePermissions.GetAll()) - ); + if (identitySeedResult.CreatedAdminRole) + { + await scope.ServiceProvider + .GetRequiredService() + .SeedAsync( + RolePermissionValueProvider.ProviderName, + "admin", + IdentityPermissions.GetAll().Union(MyProjectNamePermissions.GetAll()) + ); + } }); } }