Example #1
0
        public MediatorResponse <LoginResultViewModel> Index(LoginViewModel viewModel)
        {
            var validationResult = _loginViewModelServerValidator.Validate(viewModel);

            if (!validationResult.IsValid)
            {
                return(GetMediatorResponse <LoginResultViewModel>(LoginMediatorCodes.Index.ValidationError, null, validationResult));
            }

            var result = _candidateServiceProvider.Login(viewModel);

            if (result.UserStatus.HasValue)
            {
                if (result.UserStatus == UserStatuses.Locked)
                {
                    _userDataProvider.Push(UserDataItemNames.UnlockEmailAddress, result.EmailAddress);

                    return(GetMediatorResponse(LoginMediatorCodes.Index.AccountLocked, result));
                }

                if (result.IsAuthenticated)
                {
                    _userDataProvider.SetUserContext(result.EmailAddress, result.FullName, result.AcceptedTermsAndConditionsVersion);

                    if (result.UserStatus == UserStatuses.PendingActivation)
                    {
                        return(GetMediatorResponse(LoginMediatorCodes.Index.PendingActivation, result));
                    }

                    // Redirect to session return URL (if any).
                    var returnUrl = _userDataProvider.Pop(UserDataItemNames.SessionReturnUrl) ?? _userDataProvider.Pop(UserDataItemNames.ReturnUrl);
                    result.ReturnUrl = returnUrl;

                    if (result.AcceptedTermsAndConditionsVersion != _configurationManager.GetAppSetting <string>(Settings.TermsAndConditionsVersion))
                    {
                        return(!string.IsNullOrEmpty(returnUrl)
                            ? GetMediatorResponse(LoginMediatorCodes.Index.TermsAndConditionsNeedAccepted, result, parameters: returnUrl)
                            : GetMediatorResponse(LoginMediatorCodes.Index.TermsAndConditionsNeedAccepted, result));
                    }

                    if (!string.IsNullOrWhiteSpace(returnUrl))
                    {
                        return(GetMediatorResponse(LoginMediatorCodes.Index.ReturnUrl, result, parameters: returnUrl));
                    }

                    // Redirect to last viewed vacancy (if any).
                    var lastViewedVacancyId = _userDataProvider.Pop(CandidateDataItemNames.LastViewedVacancyId);

                    if (lastViewedVacancyId != null)
                    {
                        var candidate = _candidateServiceProvider.GetCandidate(result.EmailAddress);

                        var applicationStatus = _candidateServiceProvider.GetApplicationStatus(candidate.EntityId, int.Parse(lastViewedVacancyId));

                        if (applicationStatus.HasValue && applicationStatus.Value == ApplicationStatuses.Draft)
                        {
                            return(GetMediatorResponse(LoginMediatorCodes.Index.ApprenticeshipApply, result, parameters: lastViewedVacancyId));
                        }

                        return(GetMediatorResponse(LoginMediatorCodes.Index.ApprenticeshipDetails, result, parameters: lastViewedVacancyId));
                    }

                    return(GetMediatorResponse(LoginMediatorCodes.Index.Ok, result));
                }
            }

            return(GetMediatorResponse(LoginMediatorCodes.Index.LoginFailed, result, parameters: result.ViewModelMessage));
        }
Example #2
0
        public MediatorResponse <LoginResultViewModel> Index(LoginViewModel viewModel)
        {
            var validationResult = _loginViewModelServerValidator.Validate(viewModel);

            if (!validationResult.IsValid)
            {
                return(GetMediatorResponse <LoginResultViewModel>(LoginMediatorCodes.Index.ValidationError, null, validationResult));
            }

            var result = _candidateServiceProvider.Login(viewModel);

            if (result.UserStatus.HasValue)
            {
                if (result.UserStatus == UserStatuses.Locked)
                {
                    _userDataProvider.Push(UserDataItemNames.UnlockEmailAddress, result.EmailAddress);

                    return(GetMediatorResponse(LoginMediatorCodes.Index.AccountLocked, result));
                }

                if (result.IsAuthenticated)
                {
                    _logService.Info("User {0} successfully logged in. User Status: {1}", result.EmailAddress, result.UserStatus);

                    _userDataProvider.SetUserContext(result.EmailAddress, result.FullName, result.AcceptedTermsAndConditionsVersion);

                    if (result.UserStatus == UserStatuses.PendingActivation)
                    {
                        return(GetMediatorResponse(LoginMediatorCodes.Index.PendingActivation, result));
                    }

                    var candidate = _candidateServiceProvider.GetCandidate(result.EmailAddress);
                    SetUsersApplicationContext(candidate.EntityId);

                    // Redirect to session return URL (if any).
                    var returnUrl = _userDataProvider.Pop(UserDataItemNames.SessionReturnUrl) ?? _userDataProvider.Pop(UserDataItemNames.ReturnUrl);
                    result.ReturnUrl = returnUrl;

                    if (result.AcceptedTermsAndConditionsVersion != _configurationService.Get <CommonWebConfiguration>().TermsAndConditionsVersion)
                    {
                        return(returnUrl.IsValidReturnUrl()
                            ? GetMediatorResponse(LoginMediatorCodes.Index.TermsAndConditionsNeedAccepted, result, parameters: returnUrl)
                            : GetMediatorResponse(LoginMediatorCodes.Index.TermsAndConditionsNeedAccepted, result));
                    }

                    if (returnUrl.IsValidReturnUrl())
                    {
                        return(GetMediatorResponse(LoginMediatorCodes.Index.ReturnUrl, result, parameters: returnUrl));
                    }

                    // Redirect to last viewed vacancy (if any).
                    var lastViewedVacancy = _userDataProvider.PopLastViewedVacancy();

                    if (lastViewedVacancy != null)
                    {
                        switch (lastViewedVacancy.Type)
                        {
                        case VacancyType.Apprenticeship:
                        {
                            var applicationStatus = _candidateServiceProvider.GetApplicationStatus(candidate.EntityId, lastViewedVacancy.Id);

                            if (applicationStatus.HasValue && applicationStatus.Value == ApplicationStatuses.Draft)
                            {
                                return(GetMediatorResponse(LoginMediatorCodes.Index.ApprenticeshipApply, result, parameters: lastViewedVacancy.Id));
                            }

                            return(GetMediatorResponse(LoginMediatorCodes.Index.ApprenticeshipDetails, result, parameters: lastViewedVacancy.Id));
                        }

                        case VacancyType.Traineeship:
                            return(GetMediatorResponse(LoginMediatorCodes.Index.TraineeshipDetails, result, parameters: lastViewedVacancy.Id));
                        }
                    }

                    return(GetMediatorResponse(LoginMediatorCodes.Index.Ok, result));
                }
            }

            return(GetMediatorResponse(LoginMediatorCodes.Index.LoginFailed, result, parameters: result.ViewModelMessage));
        }