Further admin api work

This commit is contained in:
Emma [it/its]@Rory&
2025-10-05 21:34:42 +02:00
parent abb1b570a4
commit e1483e9f90
17 changed files with 642 additions and 20 deletions
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Spacebar.AdminApi.Models;
using Spacebar.Db.Contexts;
using Spacebar.Db.Models;
using Spacebar.RabbitMqUtilities;
namespace Spacebar.AdminAPI.Controllers.Media;
[ApiController]
[Route("/media/user")]
public class UserMediaController(ILogger<UserMediaController> logger, SpacebarDbContext db, RabbitMQService mq, IServiceProvider sp) : ControllerBase {
[HttpGet("{userId}/attachments")]
public async IAsyncEnumerable<Attachment> GetAttachmentsByUser(string userId) {
var db2 = sp.CreateScope().ServiceProvider.GetService<SpacebarDbContext>();
var attachments = db.Attachments
// .IgnoreAutoIncludes()
.Where(x => x.Message!.AuthorId == userId)
.AsAsyncEnumerable();
await foreach (var attachment in attachments) {
attachment.Message = await db2.Messages.FindAsync(attachment.MessageId);
// attachment.Message.Author = await db2.Users.FindAsync(attachment.Message.AuthorId);
yield return attachment;
}
}
}
@@ -146,6 +146,13 @@ public class UserController(ILogger<UserController> logger, SpacebarDbContext db
yield break;
}
user.Data = "{}";
user.Deleted = true;
user.Disabled = true;
user.Rights = 0;
db.Users.Update(user);
await db.SaveChangesAsync();
var factory = new ConnectionFactory {
Uri = new Uri("amqp://guest:guest@127.0.0.1/")
};
@@ -2,6 +2,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using ArcaneLibs.Extensions;
using Microsoft.IdentityModel.Tokens;
using Spacebar.AdminAPI.Services;
using Spacebar.Db.Contexts;
using Spacebar.Db.Models;
@@ -54,7 +55,8 @@ public class AuthenticationMiddleware(RequestDelegate next) {
if (!_userCache.ContainsKey(token)) {
var db = sp.GetRequiredService<SpacebarDbContext>();
user = await db.Users.FindAsync(res.ClaimsIdentity.Claims.First(x => x.Type == "id").Value)
var config = sp.GetRequiredService<Configuration>();
user = await db.Users.FindAsync(config.OverrideUid ?? res.ClaimsIdentity.Claims.First(x => x.Type == "id").Value)
?? throw new InvalidOperationException();
_userCache[token] = user;
_userCacheExpiry[token] = DateTime.Now.AddMinutes(5);
@@ -7,7 +7,7 @@ using Spacebar.Db.Models;
namespace Spacebar.AdminAPI.Services;
public class AuthenticationService(SpacebarDbContext db) {
public class AuthenticationService(SpacebarDbContext db, Configuration config) {
private static Dictionary<string, User> _userCache = new();
private static Dictionary<string, DateTime> _userCacheExpiry = new();
@@ -37,6 +37,6 @@ public class AuthenticationService(SpacebarDbContext db) {
throw new UnauthorizedAccessException();
}
return await db.Users.FindAsync(res.ClaimsIdentity.Claims.First(x => x.Type == "id").Value) ?? throw new InvalidOperationException();
return await db.Users.FindAsync(config.OverrideUid ?? res.ClaimsIdentity.Claims.First(x => x.Type == "id").Value) ?? throw new InvalidOperationException();
}
}
@@ -4,4 +4,6 @@ public class Configuration {
public Configuration(IConfiguration configuration) {
configuration.GetRequiredSection("SpacebarAdminApi").Bind(this);
}
public string? OverrideUid { get; set; }
}