82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using FastEndpoints;
|
|
using System.Security.Claims;
|
|
using Socialize.Api.Modules.Identity.Data;
|
|
using Socialize.Api.Modules.Identity.Models;
|
|
using Socialize.Api.Infrastructure.Security;
|
|
|
|
namespace Socialize.Api.Modules.Identity.Handlers;
|
|
|
|
[PublicAPI]
|
|
public class GetCurrentUserQueryHandler(
|
|
IdentityService identityService)
|
|
: EndpointWithoutRequest<UserDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/users/profile");
|
|
Options(o => o.WithTags("Memberships"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
CancellationToken cancellationToken)
|
|
{
|
|
UserModel? userModel = await identityService.GetCurrentUserAsync();
|
|
|
|
if (userModel is null)
|
|
{
|
|
await SendNotFoundAsync(cancellationToken);
|
|
return;
|
|
}
|
|
|
|
IList<string> roles = await identityService.GetCurrentUserRolesAsync();
|
|
IList<Claim> claims = await identityService.GetCurrentUserClaimsAsync();
|
|
|
|
string? persona = claims
|
|
.Where(claim => claim.Type == KnownClaims.Persona)
|
|
.Select(claim => claim.Value)
|
|
.LastOrDefault();
|
|
|
|
List<Guid> workspaceIds = claims
|
|
.Where(claim => claim.Type == KnownClaims.WorkspaceScope)
|
|
.Select(claim => Guid.TryParse(claim.Value, out Guid id) ? id : Guid.Empty)
|
|
.Where(id => id != Guid.Empty)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
List<Guid> clientIds = claims
|
|
.Where(claim => claim.Type == KnownClaims.ClientScope)
|
|
.Select(claim => Guid.TryParse(claim.Value, out Guid id) ? id : Guid.Empty)
|
|
.Where(id => id != Guid.Empty)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
List<Guid> campaignIds = claims
|
|
.Where(claim => claim.Type == KnownClaims.CampaignScope)
|
|
.Select(claim => Guid.TryParse(claim.Value, out Guid id) ? id : Guid.Empty)
|
|
.Where(id => id != Guid.Empty)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
await SendOkAsync(
|
|
new UserDto
|
|
{
|
|
Id = userModel.Id,
|
|
Persona = persona,
|
|
AuthorizedWorkspaceIds = workspaceIds,
|
|
AuthorizedClientIds = clientIds,
|
|
AuthorizedCampaignIds = campaignIds,
|
|
Alias = userModel.Alias,
|
|
PortraitUrl = userModel.PortraitUrl,
|
|
Firstname = userModel.Firstname,
|
|
Lastname = userModel.Lastname,
|
|
Username = userModel.Username,
|
|
PhoneNumber = userModel.PhoneNumber,
|
|
Email = userModel.Email,
|
|
BirthDate = userModel.BirthDate,
|
|
Address = userModel.Address,
|
|
UserRoles = roles
|
|
},
|
|
cancellationToken);
|
|
}
|
|
}
|