feat: add current user and tenant endpoints

This commit is contained in:
xiong
2026-07-26 12:55:31 +08:00
parent 2b02bbef7b
commit 09720237ef
11 changed files with 544 additions and 7 deletions

View File

@@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Tiku.Application.Security;
using Tiku.Domain.Tenancy;
using Tiku.Infrastructure.Persistence;
namespace Tiku.Api.Controllers;
[ApiController]
[Authorize(Policy = TikuPolicies.AuthenticatedUser)]
[Route("api/me")]
public sealed class MeController(
ICurrentUser currentUser,
TikuDbContext dbContext) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<MeResponse>> Get(CancellationToken cancellationToken)
{
if (currentUser.UserId is null)
{
return Unauthorized();
}
var user = await dbContext.Users.FindAsync([currentUser.UserId.Value], cancellationToken);
if (user is null)
{
return Unauthorized();
}
var memberships = await dbContext.TenantMemberships
.Where(membership =>
membership.UserId == user.Id &&
membership.Status == MembershipStatus.Active)
.Join(
dbContext.Tenants,
membership => membership.TenantId,
tenant => tenant.Id,
(membership, tenant) => new TenantMembershipResponse(
tenant.Id,
tenant.Name,
tenant.Slug,
membership.Role,
membership.Status))
.ToArrayAsync(cancellationToken);
return Ok(new MeResponse(
user.Id,
user.Phone,
user.Email,
user.Name,
memberships));
}
}
public sealed record MeResponse(
Guid UserId,
string? Phone,
string? Email,
string? Name,
IReadOnlyCollection<TenantMembershipResponse> Tenants);
public sealed record TenantMembershipResponse(
Guid TenantId,
string TenantName,
string TenantSlug,
TenantRole Role,
MembershipStatus Status);