105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
using Abstractions;
|
|
using HashidsNet;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Security;
|
|
using W542.GandalfReborn.Data.Database;
|
|
using W542.GandalfReborn.Data.Entities.Base;
|
|
using W542.GandalfReborn.Data.Entities.Security;
|
|
using W542.GandalfReborn.Data.Entities.Tenant;
|
|
|
|
namespace W542.GandalfReborn;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
public class TestController(IHashids hashids, ApplicationContext context, InvokerContext invokerContext) : ControllerBase
|
|
{
|
|
[HttpGet("[action]")]
|
|
public IActionResult Get()
|
|
{
|
|
// return all the user claims in all identities
|
|
return Ok((Invoker)User);
|
|
}
|
|
|
|
[HttpPost("tenant")]
|
|
public async Task<IActionResult> AddTenant([FromBody] CreateTenantCommand command)
|
|
{
|
|
var invoker = invokerContext.Invoker!;
|
|
|
|
var authorities = await context.AuthorityEntities.Where(x => x.Type == AuthorityType.Tenant).ToListAsync();
|
|
|
|
var tenantSubjectRelationEntity = new TenantSubjectRelationEntity
|
|
{
|
|
Tenant = new TenantEntity
|
|
{
|
|
Visibility = EntityVisibility.Active,
|
|
OwnerId = invoker.SubjectId,
|
|
Name = command.Name
|
|
},
|
|
SubjectId = invoker.SubjectId,
|
|
InternalAuthorities = authorities.ToHashSet()
|
|
};
|
|
|
|
await context.AddAsync(tenantSubjectRelationEntity);
|
|
await context.SaveChangesAsync();
|
|
|
|
return Ok(tenantSubjectRelationEntity.Tenant);
|
|
}
|
|
|
|
[HttpPut("tenant/{id:long}")]
|
|
public async Task<IActionResult> UpdateTenant(long id, [FromBody] UpdateTenantCommand command)
|
|
{
|
|
var invoker = (Invoker)User;
|
|
|
|
var tenant = context.Tenants.Single(x => x.Id == id);
|
|
|
|
tenant.Name = command.Name;
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
return Ok(tenant);
|
|
}
|
|
|
|
|
|
[HttpGet("tenant")]
|
|
public async Task<IActionResult> GetTenants()
|
|
{
|
|
var invoker = (Invoker)User;
|
|
|
|
var tenants = await context.TenantSubjectRelations.Where(x => x.SubjectId == invoker.SubjectId).Select(x => hashids.EncodeLong(x.TenantId)).ToListAsync();
|
|
|
|
return Ok(tenants);
|
|
}
|
|
|
|
[GrAuthorize(Type = AuthorityType.Tenant, Authorities = [TenantAuthority.Read], ParameterName = "id")]
|
|
[HttpGet("tenant/{id}")]
|
|
public async Task<IActionResult> GetTenant(string id)
|
|
{
|
|
if(!hashids.TryDecodeSingleLong(id, out var decodedId)) return BadRequest("One does not simply use a invalid id.");
|
|
|
|
var tenant = await context.Tenants.Where(x => x.Id == decodedId).SingleOrDefaultAsync();
|
|
|
|
if(tenant is null) return BadRequest("One does not simply request unknown tenant.");
|
|
|
|
return Ok(tenant);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("hashid/encode/{id:long}")]
|
|
public IActionResult GetHashId(long id)
|
|
{
|
|
return Ok(hashids.EncodeLong(id));
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("hashid/decode/{id}")]
|
|
public IActionResult GetHashId(string id)
|
|
{
|
|
return Ok(hashids.DecodeSingleLong(id));
|
|
}
|
|
}
|
|
|
|
public record UpdateTenantCommand(string Name);
|
|
public record CreateTenantCommand(string Name); |