| | 1 | | using System.Security.Claims; |
| | 2 | | using LGDXRobotCloud.Data.DbContexts; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 5 | | using Microsoft.AspNetCore.Identity; |
| | 6 | | using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
| | 7 | |
|
| | 8 | | namespace LGDXRobotCloud.Data.Services; |
| | 9 | |
|
| 0 | 10 | | public class InitialiseDataRunner( |
| 0 | 11 | | LgdxContext context, |
| 0 | 12 | | UserManager<LgdxUser> userManager, |
| 0 | 13 | | IConfiguration configuration |
| 0 | 14 | | ) : IHostedService |
| | 15 | | { |
| 0 | 16 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| 0 | 17 | | private readonly UserManager<LgdxUser> _userManager = userManager; |
| 0 | 18 | | private readonly IConfiguration _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration |
| | 19 | |
|
| | 20 | | public async Task StartAsync(CancellationToken cancellationToken) |
| 0 | 21 | | { |
| | 22 | | /* |
| | 23 | | * Identity |
| | 24 | | */ |
| | 25 | | // Roles |
| 0 | 26 | | var defaultRoles = LgdxRolesHelper.DefaultRoles; |
| 0 | 27 | | foreach (var (key, value) in defaultRoles) |
| 0 | 28 | | { |
| 0 | 29 | | var role = new LgdxRole{ |
| 0 | 30 | | Id = key.ToString(), |
| 0 | 31 | | Name = value.Name, |
| 0 | 32 | | NormalizedName = value.Name.ToUpper(), |
| 0 | 33 | | }; |
| 0 | 34 | | var roleStore = new RoleStore<LgdxRole>(_context); |
| 0 | 35 | | if (!_context.Roles.Any(r => r.Name == role.Name)) |
| 0 | 36 | | { |
| | 37 | | // Create Role |
| 0 | 38 | | await roleStore.CreateAsync(role, cancellationToken); |
| | 39 | | // Add claims for role |
| 0 | 40 | | foreach (var scope in value.Scopes) |
| 0 | 41 | | { |
| 0 | 42 | | var claim = new Claim("scope", scope); |
| 0 | 43 | | await roleStore.AddClaimAsync(role, claim, cancellationToken); |
| 0 | 44 | | } |
| 0 | 45 | | } |
| 0 | 46 | | } |
| | 47 | | // Admin User |
| 0 | 48 | | var firstUser = new LgdxUser |
| 0 | 49 | | { |
| 0 | 50 | | Id = Guid.CreateVersion7().ToString(), |
| 0 | 51 | | Email = _configuration["email"], |
| 0 | 52 | | EmailConfirmed = true, |
| 0 | 53 | | LockoutEnabled = true, |
| 0 | 54 | | Name = _configuration["fullName"], |
| 0 | 55 | | NormalizedEmail = _configuration["email"]!.ToUpper(), |
| 0 | 56 | | NormalizedUserName = _configuration["userName"]!.ToUpper(), |
| 0 | 57 | | SecurityStamp = Guid.NewGuid().ToString(), |
| 0 | 58 | | UserName = _configuration["userName"] |
| 0 | 59 | | }; |
| | 60 | |
|
| 0 | 61 | | if (!_context.Users.Any(u => u.UserName == firstUser.UserName)) |
| 0 | 62 | | { |
| 0 | 63 | | var password = new PasswordHasher<LgdxUser>(); |
| 0 | 64 | | var hashed = password.HashPassword(firstUser, _configuration["password"]!); |
| 0 | 65 | | firstUser.PasswordHash = hashed; |
| | 66 | |
|
| 0 | 67 | | var userStore = new UserStore<LgdxUser>(_context); |
| 0 | 68 | | await userStore.CreateAsync(firstUser, cancellationToken); |
| 0 | 69 | | } |
| | 70 | | // Assign user to roles |
| 0 | 71 | | LgdxUser? user = await _userManager.FindByEmailAsync(firstUser.Email!); |
| 0 | 72 | | var result = await _userManager.AddToRolesAsync(user!, ["Global Administrator"]); |
| 0 | 73 | | await _context.SaveChangesAsync(cancellationToken); |
| | 74 | |
|
| 0 | 75 | | Environment.Exit(0); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public Task StopAsync(CancellationToken cancellationToken) |
| 0 | 79 | | { |
| 0 | 80 | | return Task.CompletedTask; |
| 0 | 81 | | } |
| | 82 | | } |