| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 9 | | |
| | | 10 | | namespace SVETA.Api.Helpers.Authorize |
| | | 11 | | { |
| | | 12 | | public class MethodAuthorizationHandler : AuthorizationHandler<MethodRequirement> |
| | | 13 | | { |
| | | 14 | | private ILogger<MethodAuthorizationHandler> _logger; |
| | | 15 | | private IMethodRolesService _methodRolesService; |
| | 0 | 16 | | public MethodAuthorizationHandler(ILogger<MethodAuthorizationHandler> logger, |
| | 0 | 17 | | IMethodRolesService methodRolesService) |
| | 0 | 18 | | { |
| | 0 | 19 | | _logger = logger; |
| | 0 | 20 | | _methodRolesService = methodRolesService; |
| | 0 | 21 | | } |
| | | 22 | | protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MethodRequirement requiremen |
| | 0 | 23 | | { |
| | 0 | 24 | | var roles = context.User.FindAll(c => c.Type == ClaimTypes.Role); |
| | 0 | 25 | | if (roles != null ) |
| | 0 | 26 | | { |
| | 0 | 27 | | var baseRoles = _methodRolesService.GetMethodRoles(requirement.Method); |
| | 0 | 28 | | if (baseRoles == null) |
| | 0 | 29 | | { |
| | 0 | 30 | | return Task.CompletedTask; |
| | | 31 | | } |
| | 0 | 32 | | foreach(var role in roles.ToList()) |
| | 0 | 33 | | { |
| | 0 | 34 | | if (baseRoles.Any(d => d.Roles != null && d.Roles.Name == role.Value)) |
| | 0 | 35 | | { |
| | 0 | 36 | | context.Succeed(requirement); |
| | 0 | 37 | | break; |
| | | 38 | | } |
| | 0 | 39 | | } |
| | 0 | 40 | | } |
| | 0 | 41 | | return Task.CompletedTask; |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |