Пример #1
0
        public async Task Invoke(HttpContext context, IPermissionsCache permissionsCache, IAuthRepository authRepo)
        {
            string serial        = context.Request.Headers[BeepClaimTypes.PermissionsSerial];
            int    environmentId = Convert.ToInt32(context.Request.Headers[BeepClaimTypes.EnvironmentId]);
            int    userId        = Convert.ToInt32(context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);

            if (!string.IsNullOrEmpty(serial) && serial.ToLower() != "updating" &&
                environmentId > 0 && userId > 0)
            {
                PermissionsChacheResult chacheResult = permissionsCache.SerialsMatch(userId, environmentId, serial);
                switch (chacheResult)
                {
                case PermissionsChacheResult.DoNotMatch:
                    context.Response.AddCustomHeader("permissions_changed", "true");
                    break;

                case PermissionsChacheResult.NotCached:
                    IEnumerable <Permission> userPermissions = authRepo.GetAllUserPermissions(userId).Result;
                    permissionsCache.AddEntriesForUser(userId, userPermissions);
                    break;
                }
            }

            await _next(context);
        }
Пример #2
0
        public override Task TokenValidated(TokenValidatedContext context)
        {
            string serial        = context.Request.Headers["PermissionsSerial"];
            int    environmentId = Convert.ToInt32(context.Request.Headers["EnvironmentId"]);
            int    userId        = Convert.ToInt32(context.Principal.FindFirst(ClaimTypes.NameIdentifier).Value);

            PermissionsChacheResult chacheResult = _cache.SerialsMatch(userId, environmentId, serial);

            if (chacheResult == PermissionsChacheResult.NotCached)
            {
                IEnumerable <Permission> userPermissions = _authRepo.GetAllUserPermissions(userId).Result;
                _cache.AddEntriesForUser(userId, userPermissions);
            }

            return(Task.CompletedTask);
        }