aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/Models/LoggedInUserModel.cs
blob: 46cea50cb903a4a3f1c49b62b5a2b1c31a87c579 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace Quality.Storage.Api.Models;

public class LoggedInUserModel
{
    public LoggedInUserModel() { }

    public LoggedInUserModel(ClaimsPrincipal user) {
        Username = user.FindFirstValue(AppClaims.USERNAME);
        Id = user.FindFirstValue(AppClaims.USER_ID).AsGuid();
        Role = UserRoleHelper.FromString(user.FindFirstValue(AppClaims.USER_ROLE));
    }

    public string Username { get; set; }
    public Guid Id { get; set; }
    public UserRole Role { get; set; }

    public class Public
    {
        public string Id { get; set; }
        public string Username { get; set; }
        public string Role { get; set; }
    }

    public static Public ForThePeople(HttpContext httpContext) {
        return new Public {
            Id = httpContext.User.FindFirstValue(AppClaims.USER_ID),
            Username = httpContext.User.FindFirstValue(AppClaims.USERNAME),
            Role = httpContext.User.FindFirstValue(AppClaims.USER_ROLE)
        };
    }
}