/// <summary> /// It doesn't cache user profiles /// </summary> /// <param name="uow"></param> /// <param name="userDto"></param> protected void CacheUserDto(IdentityUow uow, UserDto userDto) { if (userDto == null) { return; } uow.Cache.SetCachedItemAsync(CacheKeyHelper.GetCacheKey <UserDto>(userDto.Key), userDto); uow.Cache.SetCachedItemAsync(CacheKeyHelper.GetCacheKey <UserDto>(userDto.Username), userDto); uow.Cache.SetCachedItemAsync(CacheKeyHelper.GetCacheKey <UserDto>(userDto.Email), userDto); uow.Cache.SetCachedItemAsync( CacheKeyHelper.GetCacheKey <UserDto>(userDto.Mobile.LocalNumberWithAreaCode), userDto); uow.Cache.SetCachedItemAsync( CacheKeyHelper.GetCacheKey <UserDto>(userDto.Mobile.LocalNumberWithAreaCodeInDigits), userDto); var userUpdatedEvent = new UserUpdatedEvent { UpdatedUser = userDto }; var userProfile = GetUserProfile(userDto.Key, _userProfileConfig.UserProfileOriginator); if (!string.IsNullOrWhiteSpace(userProfile?.Body)) { var basicMemberInfo = Serializer.Deserialize <BasicMemberInfo>(userProfile.Body); userUpdatedEvent.Roles = basicMemberInfo.Roles; } DispatchEvent(userUpdatedEvent); }
public override void Given() { _logger = Substitute.For <ILogger <HomeController> >(); _homeController = new HomeController(_logger); var httpContext = new ClaimsIdentityBuilder <HomeController>(_homeController) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(HttpContextAccessor.HttpContext.User.GetUserId(), CacheConstants.UserSessionActivityCacheKey); var routeData = new RouteData(); routeData.Values.Add("controller", "Home"); routeData.Values.Add("action", nameof(HomeController.Index)); var controllerActionDescriptor = new ControllerActionDescriptor { ControllerName = "Home", ActionName = nameof(HomeController.Index) }; var actionContext = new ActionContext(HttpContextAccessor.HttpContext, routeData, controllerActionDescriptor); _actionExecutingContext = new ActionExecutingContext(actionContext, new List <IFilterMetadata>(), new Dictionary <string, object>(), _homeController); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { try { if (context.Controller.GetType() != typeof(HomeController) && context.Controller.GetType() != typeof(TimeoutController) && !string.IsNullOrWhiteSpace(context.HttpContext.User.GetUserId())) { var cacheKey = CacheKeyHelper.GetCacheKey(context.HttpContext.User.GetUserId(), CacheConstants.UserSessionActivityCacheKey); await _cacheService.SetAsync(cacheKey, DateTime.UtcNow); } if (context.HttpContext.User.Identity.IsAuthenticated && !context.HttpContext.User.HasAccessToService() && IsAccessDenied(context)) { var routeValues = new RouteValueDictionary { { "controller", Constants.ErrorController }, { "action", nameof(ErrorController.ServiceAccessDenied) } }; context.Result = new RedirectToRouteResult(routeValues); await context.Result.ExecuteResultAsync(context); } else { await next(); } } catch (Exception exception) { _logger.LogError(exception, exception.Message); } }
public override void Given() { _resultsAndCertificationConfiguration = new ResultsAndCertificationConfiguration { DfeSignInSettings = new DfeSignInSettings { Timeout = 2 } }; _timeoutController = new TimeoutController(_resultsAndCertificationConfiguration, CacheService); var httpContext = new ClaimsIdentityBuilder <TimeoutController>(_timeoutController) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(HttpContextAccessor.HttpContext.User.GetUserId(), CacheConstants.UserSessionActivityCacheKey); var routeData = new RouteData(); routeData.Values.Add("controller", "Timeout"); routeData.Values.Add("action", nameof(TimeoutController.GetActiveDurationAsync)); var controllerActionDescriptor = new ControllerActionDescriptor { ControllerName = "Timeout", ActionName = nameof(TimeoutController.GetActiveDurationAsync) }; var actionContext = new ActionContext(HttpContextAccessor.HttpContext, routeData, controllerActionDescriptor); _actionExecutingContext = new ActionExecutingContext(actionContext, new List <IFilterMetadata>(), new Dictionary <string, object>(), _timeoutController); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); ResultLoader = Substitute.For <IResultLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <ResultController> >(); Controller = new ResultController(ResultLoader, CacheService, Logger); AoUkprn = 1234567890; ViewModel = new ManageCoreResultViewModel { ProfileId = 1, ResultId = 11, SelectedGradeCode = "PCG1" }; var httpContext = new ClaimsIdentityBuilder <ResultController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.ResultCacheKey); MockResult = new ChangeResultResponse(); }
public UserDto GetUser(Guid userKey, string userProfileOriginator) { UserDto userDto = null; Execute(uow => { var cacheKey = CacheKeyHelper.GetCacheKey <UserDto>(userKey); userDto = uow.Cache.GetCachedItem <UserDto>(cacheKey); if (userDto != null) { userDto.UserProfile = GetUserProfile(uow, userDto.Key, userProfileOriginator); return; } var user = uow.Store.FirstOrDefault <User>(x => x.Key == userKey); if (user == null) { return; } userDto = MappingService.Map <UserDto>(user); userDto.UserProfile = GetUserProfile(uow, userDto.Key, userProfileOriginator); CacheUserDto(uow, userDto); }); return(userDto); }
public async Task SignOut() { await _cacheService.RemoveAsync <DateTime>(CacheKeyHelper.GetCacheKey(User.GetUserId(), CacheConstants.UserSessionActivityCacheKey)); await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); }
public async Task ActivityTimeout() { var userId = User.GetUserId(); TempData.Set(Constants.UserSessionActivityId, userId); await _cacheService.RemoveAsync <DateTime>(CacheKeyHelper.GetCacheKey(userId, CacheConstants.UserSessionActivityCacheKey)); await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); RegistrationLoader = Substitute.For <IRegistrationLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <RegistrationController> >(); Controller = new RegistrationController(RegistrationLoader, CacheService, Logger); var httpContext = new ClaimsIdentityBuilder <RegistrationController>(Controller) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.RegistrationCacheKey); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { try { if (context.Controller.GetType() != typeof(Controllers.HomeController) && context.Controller.GetType() != typeof(Controllers.TimeoutController) && !string.IsNullOrWhiteSpace(context.HttpContext.User.GetUserId())) { var cacheKey = CacheKeyHelper.GetCacheKey(context.HttpContext.User.GetUserId(), CacheConstants.UserSessionActivityCacheKey); await _cacheService.SetAsync(cacheKey, DateTime.UtcNow); } await next(); } catch (Exception exception) { _logger.LogError(exception, exception.Message); } }
private ClientDto AddOrUpdateClient(IdentityUow uow, ClientDto clientDto) { var client = MappingService.Map <Client>(clientDto).AsNewEntity(); client = uow.Store.AddOrUpdate(client, x => x.ClientId == clientDto.ClientId); if (clientDto.AllowedScopes != null) { foreach (var allowedScopeDto in clientDto.AllowedScopes) { var allowedScope = MappingService.Map <ClientScope>(allowedScopeDto).WithClient(client); uow.Store.AddOrUpdate(allowedScope, x => x.ClientKey == client.Key && x.Scope == allowedScopeDto.Scope); } } if (clientDto.ClientSecrets != null) { foreach (var clientSecretDto in clientDto.ClientSecrets) { var clientSecret = MappingService.Map <ClientSecret>(clientSecretDto).WithClient(client); uow.Store.AddOrUpdate(clientSecret, x => x.ClientKey == client.Key && x.Value == clientSecretDto.Value); } } if (clientDto.RedirectUris != null) { foreach (var redirectUriDto in clientDto.RedirectUris) { var redirectUri = MappingService.Map <ClientRedirectUri>(redirectUriDto).WithClient(client); uow.Store.AddOrUpdate(redirectUri, x => x.ClientKey == client.Key && x.Uri == redirectUriDto.Uri); } } var updatedClientDto = MappingService.Map <ClientDto>(client); CollectClientAssociatedInfo(uow, updatedClientDto); var cacheKey = CacheKeyHelper.GetCacheKey <ClientDto>(client.ClientId); uow.Cache.SetCachedItemAsync(cacheKey, updatedClientDto).Wait(); return(updatedClientDto); }
public override void Setup() { Ukprn = 12345; HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); Logger = Substitute.For <ILogger <AssessmentController> >(); AssessmentLoader = Substitute.For <IAssessmentLoader>(); CacheService = Substitute.For <ICacheService>(); Controller = new AssessmentController(AssessmentLoader, CacheService, Logger); var httpContext = new ClaimsIdentityBuilder <AssessmentController>(Controller) .Add(CustomClaimTypes.Ukprn, Ukprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = string.Concat(CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.AssessmentCacheKey), Common.Helpers.Constants.AssessmentsUploadSuccessfulViewModel); }
public override void Setup() { TrainingProviderLoader = Substitute.For <ITrainingProviderLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <TrainingProviderController> >(); Controller = new TrainingProviderController(TrainingProviderLoader, CacheService, Logger); ProviderUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <TrainingProviderController>(Controller) .Add(CustomClaimTypes.Ukprn, ProviderUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.TrainingProviderCacheKey); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); AssessmentLoader = Substitute.For <IAssessmentLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <AssessmentController> >(); Controller = new AssessmentController(AssessmentLoader, CacheService, Logger); ProfileId = 1; AoUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <AssessmentController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.AssessmentCacheKey); }
public override void Setup() { _resultsAndCertificationConfiguration = new ResultsAndCertificationConfiguration { DfeSignInSettings = new DfeSignInSettings { Timeout = 2 } }; CacheService = Substitute.For <ICacheService>(); HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); Controller = new TimeoutController(_resultsAndCertificationConfiguration, CacheService); var httpContext = new ClaimsIdentityBuilder <TimeoutController>(Controller) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.UserSessionActivityCacheKey); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); RegistrationLoader = Substitute.For <IRegistrationLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <ManageRegistrationController> >(); Controller = new ManageRegistrationController(RegistrationLoader, CacheService, Logger); ProfileId = 1; AoUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <ManageRegistrationController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = string.Concat(CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.RegistrationCacheKey), Common.Helpers.Constants.ChangeRegistrationProviderCoreNotSupportedViewModel); }
private ClientDto GetClient(IdentityUow uow, string clientId) { var cacheKey = CacheKeyHelper.GetCacheKey <ClientDto>(clientId); var clientDto = uow.Cache.GetCachedItem <ClientDto>(cacheKey); if (clientDto != null) { return(clientDto); } clientDto = MappingService.Map <ClientDto>(uow.Store.FirstOrDefault <Client>(x => x.ClientId == clientId)); if (clientDto == null) { return(null); } CollectClientAssociatedInfo(uow, clientDto); uow.Cache.SetCachedItemAsync(cacheKey, clientDto).Wait(); return(clientDto); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); RegistrationLoader = Substitute.For <IRegistrationLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <ManageRegistrationController> >(); Controller = new ManageRegistrationController(RegistrationLoader, CacheService, Logger); ProfileId = 1; AoUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <ManageRegistrationController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.ReregisterCacheKey); ViewModel = new ReregisterSpecialismQuestionViewModel(); }
private UserDto GetUser(IdentityUow uow, string userProfileOriginator, string usernameOrEmailOrMobileNumber) { var cacheKey = GetUserCacheKey(usernameOrEmailOrMobileNumber); var userDto = uow.Cache.GetCachedItem <UserDto>(cacheKey); if (userDto != null) { userDto.UserProfile = GetUserProfile(uow, userDto.Key, userProfileOriginator); return(userDto); } var localNumberInDigits = usernameOrEmailOrMobileNumber.GetNumberInDigits(); if (!string.IsNullOrWhiteSpace(localNumberInDigits)) { cacheKey = CacheKeyHelper.GetCacheKey <UserDto>(localNumberInDigits); userDto = uow.Cache.GetCachedItem <UserDto>(cacheKey); } if (userDto != null) { userDto.UserProfile = GetUserProfile(uow, userDto.Key, userProfileOriginator); return(userDto); } var user = uow.Store.FirstOrDefault <User>( x => x.Email.Address == usernameOrEmailOrMobileNumber || x.Username == usernameOrEmailOrMobileNumber || x.Mobile.LocalNumberWithAreaCode == usernameOrEmailOrMobileNumber || x.Mobile.LocalNumberWithAreaCodeInDigits == localNumberInDigits ); if (user == null) { return(null); } userDto = MappingService.Map <UserDto>(user); userDto.UserProfile = GetUserProfile(uow, userDto.Key, userProfileOriginator); CacheUserDto(uow, userDto); return(userDto); }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); RegistrationLoader = Substitute.For <IRegistrationLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <ManageRegistrationController> >(); Controller = new ManageRegistrationController(RegistrationLoader, CacheService, Logger); AoUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <ManageRegistrationController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = string.Concat(CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.RegistrationCacheKey), Common.Helpers.Constants.WithdrawRegistrationConfirmationViewModel); MockResult = new WithdrawRegistrationResponse { ProfileId = 1, Uln = 123456789, IsRequestFromProviderAndCorePage = true }; }
public override void Setup() { HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); RegistrationLoader = Substitute.For <IRegistrationLoader>(); CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <ManageRegistrationController> >(); Controller = new ManageRegistrationController(RegistrationLoader, CacheService, Logger); AoUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <ManageRegistrationController>(Controller) .Add(CustomClaimTypes.Ukprn, AoUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.RegistrationCacheKey); ViewModel = new ChangeDateofBirthViewModel { Day = "1", Month = "2", Year = "2000" }; MockResult = new ManageRegistrationResponse(); }
public override void Setup() { ResultsAndCertificationConfiguration = new ResultsAndCertificationConfiguration { SoaAvailableDate = "10/08/2021".ToDateTime() }; CacheService = Substitute.For <ICacheService>(); Logger = Substitute.For <ILogger <StatementOfAchievementController> >(); StatementOfAchievementLoader = Substitute.For <IStatementOfAchievementLoader>(); ProviderAddressLoader = Substitute.For <IProviderAddressLoader>(); Controller = new StatementOfAchievementController(StatementOfAchievementLoader, ProviderAddressLoader, CacheService, ResultsAndCertificationConfiguration, Logger); ProviderUkprn = 1234567890; var httpContext = new ClaimsIdentityBuilder <StatementOfAchievementController>(Controller) .Add(CustomClaimTypes.Ukprn, ProviderUkprn.ToString()) .Add(CustomClaimTypes.UserId, Guid.NewGuid().ToString()) .Build() .HttpContext; HttpContextAccessor = Substitute.For <IHttpContextAccessor>(); HttpContextAccessor.HttpContext.Returns(httpContext); CacheKey = CacheKeyHelper.GetCacheKey(httpContext.User.GetUserId(), CacheConstants.SoaCacheKey); }
private static string GetUserCacheKey(string usernameOrEmailOrMobileNumber) { return(CacheKeyHelper.GetCacheKey <UserDto>(usernameOrEmailOrMobileNumber)); }
private static string GetUserProfileCacheKey(Guid userKey, string profileOriginator) { return(CacheKeyHelper.GetCacheKey <UserProfileDto>(profileOriginator + userKey)); }
private static string GetCacheKey(string key) { return(CacheKeyHelper.GetCacheKey <AuthorizationCode>(key)); }