-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapUsersHostedService.cs
More file actions
40 lines (34 loc) · 1.35 KB
/
Copy pathBootstrapUsersHostedService.cs
File metadata and controls
40 lines (34 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace SalesAPI;
/// <summary>
/// Ensures bootstrap accounts exist after the host has started.
/// Runs in the hosted-service pipeline (not fire-and-forget Task.Run).
/// </summary>
public sealed class BootstrapUsersHostedService : IHostedService
{
private readonly DatabaseHelper _db;
private readonly ILogger<BootstrapUsersHostedService> _logger;
public BootstrapUsersHostedService(DatabaseHelper db, ILogger<BootstrapUsersHostedService> logger)
{
_db = db;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
try
{
if (_db.GetUserByUsername("admin") == null)
{
if (!_db.CreateUser("admin", "admin123", UserRoles.Admin))
_logger.LogWarning("Bootstrap admin account was not created.");
}
if (!_db.EnsureUserRoleAndPassword("Matthew", "Revelation@717", UserRoles.Management))
_logger.LogWarning("Bootstrap Management account for Matthew was not updated.");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Bootstrap user setup failed; API will continue running.");
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}