< Summary

Information
Class: OzonOrdersWeb.Middleware.ImpersonationMiddleware
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Middleware/ImpersonationMiddleware.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 46
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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%
InvokeAsync()0%110100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Middleware/ImpersonationMiddleware.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2using Microsoft.AspNetCore.Identity;
 3using OzonRepositories.Context.Identity;
 4
 5namespace OzonOrdersWeb.Middleware
 6{
 7    public class ImpersonationMiddleware
 8    {
 9        private readonly RequestDelegate _next;
 10
 011        public ImpersonationMiddleware(RequestDelegate next)
 12        {
 013            _next = next;
 014        }
 15
 16        public async Task InvokeAsync(HttpContext context,
 17            UserManager<CustomIdentityUser> userManager,
 18            SignInManager<CustomIdentityUser> signInManager)
 19        {
 020            var path = context.Request.Path;
 021            if (path.StartsWithSegments("/Impersonation", out var remaining))
 22            {
 23                // Mark the request so other middleware can recognise this as an impersonation entry
 024                context.Items["IsImpersonationEntry"] = true;
 25                // Если есть cookie с Id пользователя для имперсонации, используем его для установки User
 026                if (context.Request.Cookies.TryGetValue("ImpersonationUserId", out var userId) && !string.IsNullOrEmpty(
 27                {
 028                    var user = await userManager.FindByIdAsync(userId);
 029                    if (user != null)
 30                    {
 031                        var principal = await signInManager.CreateUserPrincipalAsync(user);
 032                        if (principal != null)
 33                        {
 034                            context.User = principal;
 35                        }
 36                    }
 37                }
 38
 39                // Убираем префикс /Impersonation из пути и продолжаем обработку как обычно
 040                context.Request.Path = remaining;
 41            }
 42
 043            await _next(context);
 044        }
 45    }
 46}