-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApiControllerBase.cs
45 lines (38 loc) · 1.27 KB
/
ApiControllerBase.cs
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
37
38
39
40
41
42
43
44
45
using Headway.Core.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Headway.WebApi.Controllers
{
[ApiController]
[EnableCors("local")]
[Route("[controller]")]
[Authorize(Roles = Core.Constants.Claims.HEADWAY_USER)]
public abstract class ApiControllerBase<T> : ControllerBase
{
protected readonly IRepository repository;
protected readonly ILogger<T> logger;
protected ApiControllerBase(IRepository repository, ILogger<T> logger)
{
this.repository = repository;
this.logger = logger;
}
protected string GetUserClaim()
{
var identity = (ClaimsIdentity)HttpContext.User.Identity;
var claim = identity.FindFirst(ClaimTypes.Email);
repository.SetUser(claim.Value);
return claim.Value;
}
protected async Task<bool> IsAuthorisedAsync(string permission)
{
var claim = GetUserClaim();
return await repository
.IsAuthorisedAsync(claim, permission)
.ConfigureAwait(false);
}
}
}