using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Tiku.Application.Auth; using Tiku.Application.Security; using Tiku.Domain.Tenancy; namespace Tiku.Api.Controllers; [ApiController] [Tags("租户端-当前租户")] [Authorize(Policy = TikuPolicies.CurrentTenantMember)] [Route("api/tenant/context")] public sealed class TenantsController( ICurrentUser currentUser, ITenantContext currentTenant, ICurrentIdentityQueryService identityQueries) : ControllerBase { [HttpGet("current")] [EndpointSummary("查询当前租户")] [EndpointDescription("返回当前请求租户及当前用户在该租户内的成员角色。")] public async Task> GetCurrent(CancellationToken cancellationToken) { if (currentUser.UserId is null || currentTenant.TenantId is null) throw new TenantAccessDeniedException(); var membership = await identityQueries.GetTenantMembershipAsync( currentUser.UserId.Value, currentTenant.TenantId.Value, cancellationToken); if (membership is null) throw new TenantAccessDeniedException(); return Ok(new CurrentTenantResponse( membership.TenantId, membership.TenantName, membership.TenantSlug, membership.Status, membership.Role)); } } /// /// 当前请求租户和当前用户在该租户内的权限摘要。 /// /// 租户 ID。 /// 租户名称。 /// 租户编码。 /// 租户状态。 /// 当前用户在租户内的角色。 public sealed record CurrentTenantResponse( Guid TenantId, string TenantName, string TenantSlug, TenantStatus Status, TenantRole Role);