aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/ApiTokens/GetTokensRoute.cs
blob: ee46b341654a2ffd317c08f3bc9fdd70a3dac348 (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
32
33
34
35
36
namespace IOL.GreatOffice.Api.Endpoints.V1.ApiTokens;

public class GetTokensRoute : RouteBaseSync.WithoutRequest.WithResult<ActionResult<List<GetTokensRoute.ResponseModel>>>
{
    private readonly MainAppDatabase _database;

    public GetTokensRoute(MainAppDatabase database) {
        _database = database;
    }

    public class ResponseModel
    {
        public DateTime ExpiryDate { get; set; }
        public bool AllowRead { get; set; }
        public bool AllowCreate { get; set; }
        public bool AllowUpdate { get; set; }
        public bool AllowDelete { get; set; }
        public bool HasExpired => ExpiryDate < AppDateTime.UtcNow;
    }

    /// <summary>
    /// Get all tokens, both active and inactive.
    /// </summary>
    /// <returns>A list of tokens</returns>
    [ApiVersion(ApiSpecV1.VERSION_STRING)]
    [HttpGet("~/v{version:apiVersion}/api-tokens")]
    public override ActionResult<List<ResponseModel>> Handle() {
        return Ok(_database.AccessTokens.Where(c => c.User.Id == LoggedInUser.Id).Select(c => new ResponseModel() {
            AllowCreate = c.AllowCreate,
            AllowRead = c.AllowRead,
            AllowDelete = c.AllowDelete,
            AllowUpdate = c.AllowUpdate,
            ExpiryDate = c.ExpiryDate
        }));
    }
}