| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using OzonDomains.Models.Authorization.Enums; |
| | | 6 | | using OzonOrdersWeb.Authorization; |
| | | 7 | | using OzonOrdersWeb.WebServices.Transactions.Grid.Definitions; |
| | | 8 | | using Services.Authorization; |
| | | 9 | | using Services.Authorization.Services; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.WebServices.Transactions.Grid.Authorization; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Права грида транзакций: <c>{slug}</c> маршрута → <see cref="TransactionDefinition.PageCode"/> (код классической |
| | | 15 | | /// страницы) → <see cref="IPermissionService.HasPageAccessAsync"/>. Роли перенастраивать не нужно. |
| | | 16 | | /// Контроллер помечен [ServiceEndpoint], поэтому авто-авторизация по AppPages его не трогает — проверка здесь обязатель |
| | | 17 | | /// </summary> |
| | | 18 | | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] |
| | | 19 | | public sealed class TransactionGridAuthorizeAttribute : Attribute, IAsyncAuthorizationFilter |
| | | 20 | | { |
| | | 21 | | private readonly AccessType _access; |
| | | 22 | | |
| | 106 | 23 | | public TransactionGridAuthorizeAttribute(AccessType access = AccessType.View) |
| | | 24 | | { |
| | 106 | 25 | | _access = access; |
| | 106 | 26 | | } |
| | | 27 | | |
| | 4 | 28 | | public AccessType Access => _access; |
| | | 29 | | |
| | | 30 | | public async Task OnAuthorizationAsync(AuthorizationFilterContext context) |
| | | 31 | | { |
| | 0 | 32 | | var user = context.HttpContext.User; |
| | 0 | 33 | | if (user.Identity?.IsAuthenticated != true) |
| | | 34 | | { |
| | 0 | 35 | | context.Result = new ChallengeResult(); |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var slug = context.RouteData.Values.TryGetValue("slug", out var raw) ? raw?.ToString() : null; |
| | 0 | 40 | | var provider = context.HttpContext.RequestServices.GetRequiredService<ITransactionDefinitionProvider>(); |
| | 0 | 41 | | var definition = await provider.GetBySlugAsync(slug, context.HttpContext.RequestAborted); |
| | 0 | 42 | | if (definition == null) |
| | | 43 | | { |
| | 0 | 44 | | context.Result = new NotFoundResult(); |
| | 0 | 45 | | return; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | context.HttpContext.Items[PageAuthorizeAttribute.PageCodeItemKey] = definition.PageCode; |
| | 0 | 49 | | context.HttpContext.Items[PageAuthorizeAttribute.AccessTypeItemKey] = _access; |
| | 0 | 50 | | context.HttpContext.Items[DefinitionItemKey] = definition; |
| | | 51 | | |
| | 0 | 52 | | if (user.IsInRole(AuthorizationConstants.SuperAdminRoleName) |
| | 0 | 53 | | || user.IsInRole("Admin") |
| | 0 | 54 | | || user.HasClaim(c => c.Type == PermissionClaimsTransformation.SuperAdminClaimType && c.Value == "true")) |
| | | 55 | | { |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | var userId = user.FindFirstValue(ClaimTypes.NameIdentifier); |
| | 0 | 60 | | if (string.IsNullOrWhiteSpace(userId)) |
| | | 61 | | { |
| | 0 | 62 | | context.Result = new ForbidResult(); |
| | 0 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | var permissions = context.HttpContext.RequestServices.GetRequiredService<IPermissionService>(); |
| | 0 | 67 | | var allowed = await permissions.HasPageAccessAsync(userId, definition.PageCode, _access, context.HttpContext.Req |
| | 0 | 68 | | if (!allowed) |
| | | 69 | | { |
| | 0 | 70 | | context.Result = new ForbidResult(); |
| | | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary>Ключ в HttpContext.Items, под которым фильтр кладёт найденное определение для контроллера.</summary> |
| | | 75 | | public const string DefinitionItemKey = "__TransactionsGrid_Definition"; |
| | | 76 | | } |