| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Microsoft.AspNetCore.Authorization; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using OzonDomains.Models.Authorization.Enums; |
| | | 7 | | using Services.Authorization; |
| | | 8 | | using Services.Authorization.Services; |
| | | 9 | | |
| | | 10 | | namespace OzonOrdersWeb.Authorization; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Проверяет доступ пользователя к странице на основе HttpContext.Items + IPermissionService. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class PageAccessHandler : AuthorizationHandler<PageAccessRequirement> |
| | | 16 | | { |
| | | 17 | | private readonly IPermissionService _permissions; |
| | | 18 | | private readonly IHttpContextAccessor _http; |
| | | 19 | | private readonly ILogger<PageAccessHandler> _logger; |
| | | 20 | | private readonly AppAuthorizationOptions _options; |
| | | 21 | | |
| | 0 | 22 | | public PageAccessHandler( |
| | 0 | 23 | | IPermissionService permissions, |
| | 0 | 24 | | IHttpContextAccessor http, |
| | 0 | 25 | | ILogger<PageAccessHandler> logger, |
| | 0 | 26 | | IOptions<AppAuthorizationOptions> options) |
| | | 27 | | { |
| | 0 | 28 | | _permissions = permissions; |
| | 0 | 29 | | _http = http; |
| | 0 | 30 | | _logger = logger; |
| | 0 | 31 | | _options = options.Value; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PageAccessRequirement requ |
| | | 35 | | { |
| | 0 | 36 | | var ctx = _http.HttpContext; |
| | 0 | 37 | | if (ctx is null) return; |
| | | 38 | | |
| | 0 | 39 | | var userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
| | 0 | 40 | | if (string.IsNullOrWhiteSpace(userId)) |
| | | 41 | | { |
| | 0 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | var pageCode = ctx.Items[PageAuthorizeAttribute.PageCodeItemKey] as string; |
| | 0 | 46 | | var accessType = ctx.Items[PageAuthorizeAttribute.AccessTypeItemKey] as AccessType? ?? AccessType.View; |
| | | 47 | | |
| | 0 | 48 | | if (string.IsNullOrWhiteSpace(pageCode)) |
| | | 49 | | { |
| | | 50 | | // Страница не идентифицирована — по настройке разрешаем (для необъявленных действий) |
| | 0 | 51 | | if (_options.AllowUnregisteredPages) |
| | | 52 | | { |
| | 0 | 53 | | context.Succeed(requirement); |
| | | 54 | | } |
| | | 55 | | else |
| | | 56 | | { |
| | 0 | 57 | | _logger.LogWarning("Отказ в доступе: код страницы не задан"); |
| | | 58 | | } |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | var allowed = await _permissions.HasPageAccessAsync(userId, pageCode, accessType, ctx.RequestAborted); |
| | 0 | 63 | | if (allowed) |
| | | 64 | | { |
| | 0 | 65 | | context.Succeed(requirement); |
| | | 66 | | } |
| | | 67 | | else |
| | | 68 | | { |
| | 0 | 69 | | _logger.LogWarning("Отказ в доступе: пользователь {UserId} не имеет прав {Access} на страницу {PageCode}", u |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | } |