blob: decc5bd913e774e12f19f14a666f26efb0987f95 (
plain) (
blame)
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
41
42
43
44
|
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
namespace Quality.Storage.Api.Services.Admin;
public class UserService(AppDatabase database, ILogger<UserService> logger)
{
public bool CanCreateAccount(string username) {
if (username.IsNullOrWhiteSpace()) {
return false;
}
var normalisedUsername = username.Trim();
return database.Users.All(c => c.Username != normalisedUsername);
}
public async Task LogInUserAsync(HttpContext httpContext, IEnumerable<Claim> claims) {
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var authenticationProperties = new AuthenticationProperties {
AllowRefresh = true,
IssuedUtc = DateTimeOffset.UtcNow,
};
await httpContext.SignInAsync(principal, authenticationProperties);
logger.LogInformation("Logged in user {userId}", principal.FindFirstValue(AppClaims.USER_ID));
}
public async Task LogOutUserAsync(HttpContext httpContext, CancellationToken cancellationToken = default) {
await httpContext.SignOutAsync();
logger.LogInformation("Logged out user {userId}", httpContext.User.FindFirstValue(AppClaims.USER_ID));
}
public async Task MarkUserAsDeletedAsync(Guid userId, Guid actorId) {
var user = database.Users.FirstOrDefault(c => c.Id == userId);
if (user == default) {
logger.LogInformation("Tried to delete unknown user {userId}", userId);
return;
}
user.SetDeleted(actorId);
await database.SaveChangesAsync();
}
}
|