Example #1
0
        public static async Task Seed(this IContainer container)
        {
            await using var scope = container.BeginLifetimeScope();
            var mediator = scope.Resolve <IMediator>();
            var authenticationService = scope.Resolve <AuthenticationService>();

            var logIn       = new LogIn("12312312312");
            var logInResult = await mediator.Send(logIn);

            if (!logInResult.TryGet(out var voter))
            {
                var createVoter = new CreateVoter("12312312312");
                await mediator.Send(createVoter)
                .OnError(error => throw new InvalidOperationException($"cannot create voter while seeding: {error}"));

                logIn = new LogIn("12312312312");
                voter = await mediator.Send(logIn)
                        .OnError(error => throw new InvalidOperationException($"cannot create voter while seeding: {error}"));
            }

            var identity = new VoterIdentity(voter.Id, voter.Pesel, voter.IsAdministrator);

            authenticationService.SetIdentity(identity);

            var getQuestionsCount = new GetQuestionsCount();
            var questionsCount    = await mediator.Send(getQuestionsCount)
                                    .OnError(error => throw new InvalidOperationException($"cannot get questions count while seeding: {error}"));

            if (questionsCount.Count == 0)
            {
                var createQuestion = new CreateQuestion("Some question?", new[] { "Answer 1", "Answer 2", "Answer 3" });
                await mediator.Send(createQuestion)
                .OnError(error => throw new InvalidOperationException($"cannot create question while seeding: {error}"));
            }
        }
Example #2
0
        public AppBootstrapper(Action <ConfigurableBootstrapperConfigurator> configuration, bool enableViewSupportWhichMakesTheUnitTestsReallySlow = false) : base(with => {
            if (enableViewSupportWhichMakesTheUnitTestsReallySlow)
            {
                // Do not use view support if it is not needed. Makes the unit tests much slower.
                with.ViewLocationProvider <FileSystemViewLocationProvider>();
                with.ViewFactory <TestingViewFactory>();
            }
            configuration(with);
        }) {
            JsonSerializer = new CustomJsonSerializer();
            InternalConfiguration.Serializers.Clear();
            InternalConfiguration.Serializers.Add(JsonSerializer.GetType());

            BeforeRequest.AddItemToEndOfPipeline(context => {
                if (AuthenticatedUser == null)
                {
                    context.CurrentUser = null;
                }
                else
                {
                    var userIdentity    = new VoterIdentity(AuthenticatedUser);
                    context.CurrentUser = userIdentity;
                }
                return(null);
            });

            OnError = OnError
                      + ErrorPipelines.HandleModelBindingException()
                      + ErrorPipelines.HandleRequestValidationException()
                      + ErrorPipelines.HandleSecurityException();
        }
Example #3
0
        private async Task LogIn(string pesel)
        {
            var logIn = new LogIn(pesel);

            VoterViewModel logInResult;

            try
            {
                logInResult = await _mediator.Send(logIn)
                              .OnError(error => throw new InvalidOperationException(error.ToString()));
            }
            catch (Exception exception)
            {
                exception.WriteToConsole();
                return;
            }

            var identity = new VoterIdentity(
                logInResult.Id,
                logInResult.Pesel,
                logInResult.IsAdministrator);

            _authenticationService.SetIdentity(identity);

            _consoleState.Identity = identity;
        }
        private void SetIdentity(VoterViewModel user)
        {
            var identity = new VoterIdentity(user.Id, user.Pesel, user.IsAdministrator);

            var authenticationService = Container.Resolve <AuthenticationService>();

            authenticationService.SetIdentity(identity);
        }
Example #5
0
        public async Task <IActionResult> LogIn([FromBody] LogIn request)
        {
            var response = await _mediator.Send(request);

            if (!response.TryGet(out var voterViewModel))
            {
                return(HandleErrors(response, Ok));
            }

            var voterIdentity = new VoterIdentity(
                voterViewModel.Id,
                voterViewModel.Pesel,
                voterViewModel.IsAdministrator);

            var token = _authenticationService.GenerateToken(voterIdentity);

            var logInResponse = new
            {
                Token = token,
                Voter = voterViewModel
            };

            return(Ok(logInResponse));
        }