private ProviderUserSearchResultsViewModel GetProviderUsers(string ukprn)
        {
            var provider = _providerService.GetProvider(ukprn);

            var viewModel = new ProviderUserSearchResultsViewModel
            {
                SearchViewModel = new ProviderUserSearchViewModel(),
                ProviderName    = $"{provider.TradingName} ({provider.Ukprn})",
                ProviderUsers   = _providerUserProvider.GetProviderUsers(ukprn).ToList()
            };

            return(viewModel);
        }
Пример #2
0
        public MediatorResponse <AuthorizeResponseViewModel> Authorize(ClaimsPrincipal principal)
        {
            var viewModel = new AuthorizeResponseViewModel();

            if (string.IsNullOrEmpty(principal?.Identity?.Name))
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.EmptyUsername, viewModel, AuthorizeMessages.EmptyUsername, UserMessageLevel.Error));
            }

            var username = principal.Identity.Name;

            viewModel.Username = username;

            var userProfile = _providerUserProvider.GetUserProfileViewModel(username);

            if (userProfile != null)
            {
                viewModel.EmailAddress         = userProfile.EmailAddress;
                viewModel.EmailAddressVerified = userProfile.EmailAddressVerified;
            }

            var ukprn = principal.GetUkprn();

            if (string.IsNullOrWhiteSpace(ukprn))
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.MissingProviderIdentifier, viewModel, AuthorizeMessages.MissingProviderIdentifier, UserMessageLevel.Error));
            }

            if (!principal.IsInRole(Roles.Faa))
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.MissingServicePermission, viewModel, AuthorizeMessages.MissingServicePermission, UserMessageLevel.Warning));
            }

            var provider = _providerProvider.GetProviderViewModel(ukprn);

            if (provider == null)
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.NoProviderProfile, viewModel, AuthorizeMessages.NoProviderProfile, UserMessageLevel.Info));
            }

            viewModel.ProviderId = provider.ProviderId;

            if (provider.ProviderSiteViewModels.Count() < MinProviderSites)
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.FailedMinimumSitesCountCheck, viewModel, AuthorizeMessages.FailedMinimumSitesCountCheck, UserMessageLevel.Warning));
            }

            if (userProfile == null)
            {
                var isFirstUser = !_providerUserProvider.GetProviderUsers(ukprn).Any();

                if (isFirstUser)
                {
                    return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.FirstUser, viewModel, AuthorizeMessages.FirstUser, UserMessageLevel.Info));
                }

                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.NoUserProfile, viewModel, AuthorizeMessages.NoUserProfile, UserMessageLevel.Info));
            }

            if (!userProfile.EmailAddressVerified)
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.EmailAddressNotVerified, viewModel,
                                           AuthorizeMessages.EmailAddressNotVerified, UserMessageLevel.Info));
            }

            if (!provider.IsMigrated)
            {
                return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.ProviderNotMigrated, viewModel));
            }

            return(GetMediatorResponse(ProviderUserMediatorCodes.Authorize.Ok, viewModel));
        }