< Summary

Information
Class: OzonOrdersWeb.Authorization.PageAccessHandler
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Authorization/PageAccessHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 72
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
HandleRequirementAsync()0%156120%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Authorization/PageAccessHandler.cs

#LineLine coverage
 1using System.Security.Claims;
 2using Microsoft.AspNetCore.Authorization;
 3using Microsoft.AspNetCore.Http;
 4using Microsoft.Extensions.Logging;
 5using Microsoft.Extensions.Options;
 6using OzonDomains.Models.Authorization.Enums;
 7using Services.Authorization;
 8using Services.Authorization.Services;
 9
 10namespace OzonOrdersWeb.Authorization;
 11
 12/// <summary>
 13/// Проверяет доступ пользователя к странице на основе HttpContext.Items + IPermissionService.
 14/// </summary>
 15public 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
 022    public PageAccessHandler(
 023        IPermissionService permissions,
 024        IHttpContextAccessor http,
 025        ILogger<PageAccessHandler> logger,
 026        IOptions<AppAuthorizationOptions> options)
 27    {
 028        _permissions = permissions;
 029        _http = http;
 030        _logger = logger;
 031        _options = options.Value;
 032    }
 33
 34    protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PageAccessRequirement requ
 35    {
 036        var ctx = _http.HttpContext;
 037        if (ctx is null) return;
 38
 039        var userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
 040        if (string.IsNullOrWhiteSpace(userId))
 41        {
 042            return;
 43        }
 44
 045        var pageCode = ctx.Items[PageAuthorizeAttribute.PageCodeItemKey] as string;
 046        var accessType = ctx.Items[PageAuthorizeAttribute.AccessTypeItemKey] as AccessType? ?? AccessType.View;
 47
 048        if (string.IsNullOrWhiteSpace(pageCode))
 49        {
 50            // Страница не идентифицирована — по настройке разрешаем (для необъявленных действий)
 051            if (_options.AllowUnregisteredPages)
 52            {
 053                context.Succeed(requirement);
 54            }
 55            else
 56            {
 057                _logger.LogWarning("Отказ в доступе: код страницы не задан");
 58            }
 059            return;
 60        }
 61
 062        var allowed = await _permissions.HasPageAccessAsync(userId, pageCode, accessType, ctx.RequestAborted);
 063        if (allowed)
 64        {
 065            context.Succeed(requirement);
 66        }
 67        else
 68        {
 069            _logger.LogWarning("Отказ в доступе: пользователь {UserId} не имеет прав {Access} на страницу {PageCode}", u
 70        }
 071    }
 72}