[FA-55] Finished aside from deactivation/integration events

This commit is contained in:
gamer147
2025-12-29 14:09:41 -05:00
parent c0290cc5af
commit 01d3b94050
19 changed files with 377 additions and 75 deletions

View File

@@ -1,21 +1,21 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace FictionArchive.Service.UserService.Services.AuthenticationClient.Authentik;
public class AuthentikAddUserRequest
{
[JsonPropertyName("username")]
[JsonProperty("username")]
public required string Username { get; set; }
[JsonPropertyName("name")]
[JsonProperty("name")]
public required string DisplayName { get; set; }
[JsonPropertyName("email")]
[JsonProperty("email")]
public required string Email { get; set; }
[JsonPropertyName("is_active")]
[JsonProperty("is_active")]
public bool IsActive { get; set; } = true;
[JsonPropertyName("type")]
[JsonProperty("type")]
public string Type { get; } = "external";
}

View File

@@ -1,4 +1,6 @@
using System.Net.Http.Json;
using System.Text;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace FictionArchive.Service.UserService.Services.AuthenticationClient.Authentik;
@@ -6,11 +8,16 @@ public class AuthentikClient : IAuthenticationServiceClient
{
private readonly HttpClient _httpClient;
private readonly ILogger<AuthentikClient> _logger;
private readonly AuthentikConfiguration _configuration;
public AuthentikClient(HttpClient httpClient, ILogger<AuthentikClient> logger)
public AuthentikClient(
HttpClient httpClient,
ILogger<AuthentikClient> logger,
IOptions<AuthentikConfiguration> configuration)
{
_httpClient = httpClient;
_logger = logger;
_configuration = configuration.Value;
}
public async Task<AuthentikUserResponse?> CreateUserAsync(string username, string email, string displayName)
@@ -25,7 +32,9 @@ public class AuthentikClient : IAuthenticationServiceClient
try
{
var response = await _httpClient.PostAsJsonAsync("/api/v3/core/users/", request);
var json = JsonConvert.SerializeObject(request);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("/api/v3/core/users/", content);
if (!response.IsSuccessStatusCode)
{
@@ -36,7 +45,8 @@ public class AuthentikClient : IAuthenticationServiceClient
return null;
}
var userResponse = await response.Content.ReadFromJsonAsync<AuthentikUserResponse>();
var responseJson = await response.Content.ReadAsStringAsync();
var userResponse = JsonConvert.DeserializeObject<AuthentikUserResponse>(responseJson);
_logger.LogInformation("Successfully created user {Username} in Authentik with pk {Pk}",
username, userResponse?.Pk);
@@ -54,7 +64,7 @@ public class AuthentikClient : IAuthenticationServiceClient
try
{
var response = await _httpClient.PostAsync(
$"/api/v3/core/users/{authentikUserId}/recovery_email/",
$"/api/v3/core/users/{authentikUserId}/recovery_email/?email_stage={_configuration.EmailStageId}",
null);
if (!response.IsSuccessStatusCode)

View File

@@ -4,4 +4,5 @@ public class AuthentikConfiguration
{
public string BaseUrl { get; set; } = string.Empty;
public string ApiToken { get; set; } = string.Empty;
public string EmailStageId { get; set; }
}

View File

@@ -1,27 +1,27 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace FictionArchive.Service.UserService.Services.AuthenticationClient.Authentik;
public class AuthentikUserResponse
{
[JsonPropertyName("pk")]
[JsonProperty("pk")]
public int Pk { get; set; }
[JsonPropertyName("username")]
[JsonProperty("username")]
public string Username { get; set; } = string.Empty;
[JsonPropertyName("name")]
[JsonProperty("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("email")]
[JsonProperty("email")]
public string Email { get; set; } = string.Empty;
[JsonPropertyName("is_active")]
[JsonProperty("is_active")]
public bool IsActive { get; set; }
[JsonPropertyName("is_superuser")]
[JsonProperty("is_superuser")]
public bool IsSuperuser { get; set; }
[JsonPropertyName("uid")]
[JsonProperty("uid")]
public string Uid { get; set; } = string.Empty;
}

View File

@@ -118,4 +118,18 @@ public class UserManagementService
{
return _dbContext.Users.AsQueryable();
}
/// <summary>
/// Gets all users invited by a specific user.
/// </summary>
/// <param name="inviterId">The ID of the user who sent the invites</param>
/// <returns>List of users invited by the specified user</returns>
public async Task<List<User>> GetInvitedByUserAsync(Guid inviterId)
{
return await _dbContext.Users
.AsQueryable()
.Where(u => u.InviterId == inviterId)
.OrderByDescending(u => u.CreatedTime)
.ToListAsync();
}
}