public bool Register(RegisterViewModel model)
        {
            _logger.Debug("Calling CandidateServiceProvider to register a new candidate");

            try
            {
                var candidate = _mapper.Map <RegisterViewModel, Candidate>(model);
                candidate.RegistrationDetails.AcceptedTermsAndConditionsVersion = _configurationService.Get <CommonWebConfiguration>().TermsAndConditionsVersion;

                _candidateService.Register(candidate, model.Password);
                _authenticationTicketService.SetAuthenticationCookie(candidate.EntityId.ToString(), UserRoleNames.Unactivated);

                return(true);
            }
            catch (CustomException e)
            {
                var message = string.Format("Candidate registration failed for {0}.", model.EmailAddress);

                if (e.Code == Domain.Entities.ErrorCodes.EntityStateError)
                {
                    _logger.Info(message, e);
                }
                else
                {
                    _logger.Error(message, e);
                }
                return(false);
            }
            catch (Exception e)
            {
                _logger.Error("Candidate registration failed for " + model.EmailAddress, e);
                return(false);
            }
        }
        public ActivationViewModel Activate(ActivationViewModel model, Guid candidateId)
        {
            _logger.Info(
                "Calling CandidateServiceProvider to activate user with Id={0}",
                candidateId);

            try
            {
                _candidateService.Activate(model.EmailAddress, model.ActivationCode);
                _authenticationTicketService.SetAuthenticationCookie(_httpContext.Response.Cookies,
                                                                     candidateId.ToString(),
                                                                     UserRoleNames.Activated);

                return(new ActivationViewModel(model.EmailAddress, model.ActivationCode, ActivateUserState.Activated));
            }
            catch (CustomException e)
            {
                switch (e.Code)
                {
                case Domain.Entities.ErrorCodes.EntityStateError:
                    _logger.Error("Candidate was in an invalid state for activation:" + model.EmailAddress, e);
                    return(new ActivationViewModel(model.EmailAddress, model.ActivationCode, ActivateUserState.Error, ActivationPageMessages.ActivationFailed));

                default:
                    _logger.Info("Candidate activation failed for " + model.EmailAddress, e);
                    return(new ActivationViewModel(model.EmailAddress, model.ActivationCode, ActivateUserState.InvalidCode, ActivationPageMessages.ActivationCodeIncorrect));
                }
            }
            catch (Exception e)
            {
                _logger.Error("Candidate activation failed for " + model.EmailAddress, e);
                return(new ActivationViewModel(model.EmailAddress, model.ActivationCode, ActivateUserState.Error, ActivationPageMessages.ActivationFailed));
            }
        }
Esempio n. 3
0
        private ActionResult SetAuthenticationCookieAndRedirectToAction(string candidateEmail)
        {
            var candidate = _candidateServiceProvider.GetCandidate(candidateEmail);

            //todo: refactor - similar to stuff in login controller... move to ILoginServiceProvider
            //todo: test this
            _authenticationTicketService.SetAuthenticationCookie(HttpContext.Response.Cookies, candidate.EntityId.ToString(), UserRoleNames.Activated);
            UserData.SetUserContext(candidate.RegistrationDetails.EmailAddress,
                                    candidate.RegistrationDetails.FirstName + " " + candidate.RegistrationDetails.LastName,
                                    candidate.RegistrationDetails.AcceptedTermsAndConditionsVersion);

            // ReturnUrl takes precedence over last view vacnacy id.
            var returnUrl = UserData.Pop(UserDataItemNames.ReturnUrl);

            // Clear last viewed vacancy and distance (if any).
            var lastViewedVacancyId = UserData.Pop(CandidateDataItemNames.LastViewedVacancyId);

            UserData.Pop(CandidateDataItemNames.VacancyDistance);

            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                return(Redirect(Server.UrlDecode(returnUrl)));
            }

            if (lastViewedVacancyId != null)
            {
                return(RedirectToRoute(CandidateRouteNames.ApprenticeshipDetails, new { id = int.Parse(lastViewedVacancyId) }));
            }

            return(RedirectToRoute(CandidateRouteNames.ApprenticeshipSearch));
        }
Esempio n. 4
0
        public MediatorResponse <PasswordResetViewModel> ResetPassword(PasswordResetViewModel resetViewModel)
        {
            //Password Reset Code is verified in VerifyPasswordReset.
            //Initially assume the reset code is valid as a full check requires hitting the repo.
            resetViewModel.IsPasswordResetCodeValid = true;

            var validationResult = _passwordResetViewModelServerValidator.Validate(resetViewModel);

            if (!validationResult.IsValid)
            {
                return(GetMediatorResponse(LoginMediatorCodes.ResetPassword.FailedValidation, resetViewModel, validationResult));
            }

            resetViewModel = _candidateServiceProvider.VerifyPasswordReset(resetViewModel);

            if (resetViewModel.HasError())
            {
                return(GetMediatorResponse(LoginMediatorCodes.ResetPassword.FailedToResetPassword, resetViewModel, resetViewModel.ViewModelMessage, UserMessageLevel.Warning));
            }

            if (resetViewModel.UserStatus == UserStatuses.Locked)
            {
                return(GetMediatorResponse(LoginMediatorCodes.ResetPassword.UserAccountLocked, resetViewModel));
            }

            if (!resetViewModel.IsPasswordResetCodeValid)
            {
                validationResult = _passwordResetViewModelServerValidator.Validate(resetViewModel);
                return(GetMediatorResponse(LoginMediatorCodes.ResetPassword.InvalidResetCode, resetViewModel, validationResult));
            }

            var candidate = _candidateServiceProvider.GetCandidate(resetViewModel.EmailAddress);

            SetUsersApplicationContext(candidate.EntityId);
            _authenticationTicketService.SetAuthenticationCookie(candidate.EntityId.ToString(), UserRoleNames.Activated);

            return(GetMediatorResponse(LoginMediatorCodes.ResetPassword.SuccessfullyResetPassword, resetViewModel, PasswordResetPageMessages.SuccessfulPasswordReset, UserMessageLevel.Success));
        }