|
|
|
@ -6,6 +6,7 @@ |
|
|
|
// All rights reserved.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
@ -13,6 +14,7 @@ using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Squidex.Domain.Users; |
|
|
|
using Squidex.Infrastructure.Log; |
|
|
|
using Squidex.Shared.Identity; |
|
|
|
using Squidex.Shared.Users; |
|
|
|
|
|
|
|
@ -53,6 +55,8 @@ namespace Squidex.Config.Identity |
|
|
|
var userManager = app.ApplicationServices.GetService<UserManager<IUser>>(); |
|
|
|
var userFactory = app.ApplicationServices.GetService<IUserFactory>(); |
|
|
|
|
|
|
|
var log = app.ApplicationServices.GetService<ISemanticLog>(); |
|
|
|
|
|
|
|
if (options.IsAdminConfigured()) |
|
|
|
{ |
|
|
|
var adminEmail = options.AdminEmail; |
|
|
|
@ -60,28 +64,18 @@ namespace Squidex.Config.Identity |
|
|
|
|
|
|
|
Task.Run(async () => |
|
|
|
{ |
|
|
|
var user = await userManager.FindByEmailAsync(adminEmail); |
|
|
|
|
|
|
|
async Task userInitAsync(IUser theUser) |
|
|
|
{ |
|
|
|
await userManager.RemovePasswordAsync(theUser); |
|
|
|
await userManager.ChangePasswordAsync(theUser, null, adminPass); |
|
|
|
await userManager.AddToRoleAsync(theUser, SquidexRoles.Administrator); |
|
|
|
} |
|
|
|
|
|
|
|
if (user != null) |
|
|
|
if ((userManager.SupportsQueryableUsers && !userManager.Users.Any())) |
|
|
|
{ |
|
|
|
if (options.EnforceAdmin) |
|
|
|
try |
|
|
|
{ |
|
|
|
await userInitAsync(user); |
|
|
|
} |
|
|
|
await userManager.CreateAsync(userFactory, adminEmail, adminEmail, adminPass); |
|
|
|
} |
|
|
|
else if ((userManager.SupportsQueryableUsers && !userManager.Users.Any()) || options.EnforceAdmin) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
user = userFactory.Create(adminEmail); |
|
|
|
|
|
|
|
await userManager.CreateAsync(user); |
|
|
|
await userInitAsync(user); |
|
|
|
log.LogError(ex, w => w |
|
|
|
.WriteProperty("action", "createAdmin") |
|
|
|
.WriteProperty("status", "failed")); |
|
|
|
} |
|
|
|
} |
|
|
|
}).Wait(); |
|
|
|
} |
|
|
|
|