< Summary

Information
Class: Elsa.Identity.HostedServices.AdminUserInitializer
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/HostedServices/AdminUserInitializer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 81
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartAsync()0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/HostedServices/AdminUserInitializer.cs

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Options;
 4using JetBrains.Annotations;
 5using Microsoft.Extensions.Logging;
 6using Microsoft.Extensions.Options;
 7
 8namespace 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]
 014public class AdminUserInitializer(
 015    IUserStore userStore,
 016    IRoleStore roleStore,
 017    IUserManager userManager,
 018    IRoleManager roleManager,
 019    IOptions<DefaultAdminUserOptions> options,
 020    ILogger<AdminUserInitializer> logger)
 21    : BackgroundTask
 22{
 23    public override async Task StartAsync(CancellationToken cancellationToken)
 24    {
 025        var adminUserName = options.Value.AdminUserName;
 026        var adminPassword = options.Value.AdminPassword;
 027        var adminRoleName = options.Value.AdminRoleName;
 028        var adminRolePermissions = options.Value.AdminRolePermissions;
 029        if (string.IsNullOrWhiteSpace(adminRoleName))
 30        {
 031            logger.LogWarning("AdminRoleName is not configured. Skipping admin role and user creation.");
 032            return;
 33        }
 34
 035        var existingRole = await roleStore.FindAsync(new() { Id = adminRoleName }, cancellationToken);
 36
 037        if (existingRole == null)
 38        {
 039            var roleResult = await roleManager.CreateRoleAsync(
 040                adminRoleName,
 041                adminRolePermissions.ToList(),
 042                adminRoleName,
 043                cancellationToken);
 44
 045            logger.LogInformation("Admin role '{RoleName}' created successfully with {PermissionCount} permissions.",
 046                roleResult.Role.Name,
 047                roleResult.Role.Permissions.Count);
 48        }
 49        else
 50        {
 051            logger.LogInformation("Admin role '{RoleName}' already exists. Skipping creation.", adminRoleName);
 52        }
 53
 054        var roleToAssign = adminRoleName;
 55
 56        // Create user if configured
 057        if (string.IsNullOrWhiteSpace(adminUserName) || string.IsNullOrWhiteSpace(adminPassword))
 58        {
 059            logger.LogWarning("AdminUserName and/or AdminPassword not configured in DefaultAdminUserOptions. Skipping ad
 060            return;
 61        }
 62
 63        // Check if user already exists
 064        var existingUser = await userStore.FindAsync(new() { Name = adminUserName }, cancellationToken);
 65
 066        if (existingUser != null)
 67        {
 068            logger.LogInformation("Admin user '{User}' already exists. Skipping creation.", adminUserName);
 069            return;
 70        }
 71
 72        // Create the admin user
 073        var result = await userManager.CreateUserAsync(
 074            adminUserName,
 075            adminPassword,
 076            new List<string> { roleToAssign },
 077            cancellationToken);
 78
 079        logger.LogInformation("Admin user '{Name}' created successfully with role '{Role}'.", result.User.Name, roleToAs
 080    }
 81}