| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Options; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.HostedServices; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Hosted service that initializes the admin user and role from <see cref="DefaultAdminUserOptions"/> configuration if |
| | | 12 | | /// </summary> |
| | | 13 | | [UsedImplicitly] |
| | 0 | 14 | | public class AdminUserInitializer( |
| | 0 | 15 | | IUserStore userStore, |
| | 0 | 16 | | IRoleStore roleStore, |
| | 0 | 17 | | IUserManager userManager, |
| | 0 | 18 | | IRoleManager roleManager, |
| | 0 | 19 | | IOptions<DefaultAdminUserOptions> options, |
| | 0 | 20 | | ILogger<AdminUserInitializer> logger) |
| | | 21 | | : BackgroundTask |
| | | 22 | | { |
| | | 23 | | public override async Task StartAsync(CancellationToken cancellationToken) |
| | | 24 | | { |
| | 0 | 25 | | var adminUserName = options.Value.AdminUserName; |
| | 0 | 26 | | var adminPassword = options.Value.AdminPassword; |
| | 0 | 27 | | var adminRoleName = options.Value.AdminRoleName; |
| | 0 | 28 | | var adminRolePermissions = options.Value.AdminRolePermissions; |
| | 0 | 29 | | if (string.IsNullOrWhiteSpace(adminRoleName)) |
| | | 30 | | { |
| | 0 | 31 | | logger.LogWarning("AdminRoleName is not configured. Skipping admin role and user creation."); |
| | 0 | 32 | | return; |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | var existingRole = await roleStore.FindAsync(new() { Id = adminRoleName }, cancellationToken); |
| | | 36 | | |
| | 0 | 37 | | if (existingRole == null) |
| | | 38 | | { |
| | 0 | 39 | | var roleResult = await roleManager.CreateRoleAsync( |
| | 0 | 40 | | adminRoleName, |
| | 0 | 41 | | adminRolePermissions.ToList(), |
| | 0 | 42 | | adminRoleName, |
| | 0 | 43 | | cancellationToken); |
| | | 44 | | |
| | 0 | 45 | | logger.LogInformation("Admin role '{RoleName}' created successfully with {PermissionCount} permissions.", |
| | 0 | 46 | | roleResult.Role.Name, |
| | 0 | 47 | | roleResult.Role.Permissions.Count); |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 0 | 51 | | logger.LogInformation("Admin role '{RoleName}' already exists. Skipping creation.", adminRoleName); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | var roleToAssign = adminRoleName; |
| | | 55 | | |
| | | 56 | | // Create user if configured |
| | 0 | 57 | | if (string.IsNullOrWhiteSpace(adminUserName) || string.IsNullOrWhiteSpace(adminPassword)) |
| | | 58 | | { |
| | 0 | 59 | | logger.LogWarning("AdminUserName and/or AdminPassword not configured in DefaultAdminUserOptions. Skipping ad |
| | 0 | 60 | | return; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // Check if user already exists |
| | 0 | 64 | | var existingUser = await userStore.FindAsync(new() { Name = adminUserName }, cancellationToken); |
| | | 65 | | |
| | 0 | 66 | | if (existingUser != null) |
| | | 67 | | { |
| | 0 | 68 | | logger.LogInformation("Admin user '{User}' already exists. Skipping creation.", adminUserName); |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // Create the admin user |
| | 0 | 73 | | var result = await userManager.CreateUserAsync( |
| | 0 | 74 | | adminUserName, |
| | 0 | 75 | | adminPassword, |
| | 0 | 76 | | new List<string> { roleToAssign }, |
| | 0 | 77 | | cancellationToken); |
| | | 78 | | |
| | 0 | 79 | | logger.LogInformation("Admin user '{Name}' created successfully with role '{Role}'.", result.User.Name, roleToAs |
| | 0 | 80 | | } |
| | | 81 | | } |