< Summary

Information
Class: LGDXRobotCloud.Data.Services.InitialiseDataRunner
Assembly: LGDXRobotCloud.Data
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.Data/Services/InitialiseDataRunner.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 56
Coverable lines: 56
Total lines: 82
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
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(...)0%2040%
StartAsync()0%7280%
StopAsync(...)100%210%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.Data/Services/InitialiseDataRunner.cs

#LineLine coverage
 1using System.Security.Claims;
 2using LGDXRobotCloud.Data.DbContexts;
 3using LGDXRobotCloud.Data.Entities;
 4using LGDXRobotCloud.Utilities.Helpers;
 5using Microsoft.AspNetCore.Identity;
 6using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
 7
 8namespace LGDXRobotCloud.Data.Services;
 9
 010public class InitialiseDataRunner(
 011    LgdxContext context,
 012    UserManager<LgdxUser> userManager,
 013    IConfiguration configuration
 014  ) : IHostedService
 15{
 016  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 017  private readonly UserManager<LgdxUser> _userManager = userManager;
 018  private readonly IConfiguration _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration
 19
 20  public async Task StartAsync(CancellationToken cancellationToken)
 021  {
 22    /*
 23     * Identity
 24     */
 25    // Roles
 026    var defaultRoles = LgdxRolesHelper.DefaultRoles;
 027    foreach (var (key, value) in defaultRoles)
 028    {
 029      var role = new LgdxRole{
 030        Id = key.ToString(),
 031        Name = value.Name,
 032        NormalizedName = value.Name.ToUpper(),
 033      };
 034      var roleStore = new RoleStore<LgdxRole>(_context);
 035      if (!_context.Roles.Any(r => r.Name == role.Name))
 036      {
 37        // Create Role
 038        await roleStore.CreateAsync(role, cancellationToken);
 39        // Add claims for role
 040        foreach (var scope in value.Scopes)
 041        {
 042          var claim = new Claim("scope", scope);
 043          await roleStore.AddClaimAsync(role, claim, cancellationToken);
 044        }
 045      }
 046    }
 47    // Admin User
 048    var firstUser = new LgdxUser
 049    {
 050      Id = Guid.CreateVersion7().ToString(),
 051      Email = _configuration["email"],
 052      EmailConfirmed = true,
 053      LockoutEnabled = true,
 054      Name = _configuration["fullName"],
 055      NormalizedEmail = _configuration["email"]!.ToUpper(),
 056      NormalizedUserName = _configuration["userName"]!.ToUpper(),
 057      SecurityStamp = Guid.NewGuid().ToString(),
 058      UserName = _configuration["userName"]
 059    };
 60
 061    if (!_context.Users.Any(u => u.UserName == firstUser.UserName))
 062    {
 063      var password = new PasswordHasher<LgdxUser>();
 064      var hashed = password.HashPassword(firstUser, _configuration["password"]!);
 065      firstUser.PasswordHash = hashed;
 66
 067      var userStore = new UserStore<LgdxUser>(_context);
 068      await userStore.CreateAsync(firstUser, cancellationToken);
 069    }
 70    // Assign user to roles
 071    LgdxUser? user = await _userManager.FindByEmailAsync(firstUser.Email!);
 072    var result = await _userManager.AddToRolesAsync(user!, ["Global Administrator"]);
 073    await _context.SaveChangesAsync(cancellationToken);
 74
 075    Environment.Exit(0);
 076  }
 77
 78  public Task StopAsync(CancellationToken cancellationToken)
 079  {
 080    return Task.CompletedTask;
 081  }
 82}