Пример #1
0
        public void GetPollsParticipantsShouldReturnAllParticipantsFromPoll()
        {
            var options   = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("Database_For_Tests").Options;
            var dbContext = new ApplicationDbContext(options);
            var service   = new PollsService(dbContext);

            dbContext.Polls.Add(new Poll());
            var user = new LessplasticUser
            {
                UserName = "******"
            };
            var secondUser = new LessplasticUser
            {
                UserName = "******"
            };

            dbContext.Users.Add(user);
            dbContext.Users.Add(secondUser);
            dbContext.SaveChanges();

            var poll = dbContext.Polls.First();

            service.AddParticipant(poll, "test");
            service.AddParticipant(poll, "test2");

            var participants = service.GetPollsParticipants(poll.Id);

            Assert.Equal(2, participants.Length);
        }
Пример #2
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new LessplasticUser {
                    UserName = Input.Username, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    var townExists = this.context.Towns.FirstOrDefault(t => t.TownName == Input.TownName);

                    if (townExists == null)
                    {
                        var town = new Town
                        {
                            TownName = Input.TownName
                        };
                        town.Users.Add(user);
                        this.context.Towns.Add(town);
                        this.context.SaveChanges();
                    }
                    else
                    {
                        townExists.Users.Add(user);
                        this.context.SaveChanges();
                    }

                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Пример #3
0
        public void AddParticipantShouldAddParticipantToPoll()
        {
            var options   = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("Database_For_Tests").Options;
            var dbContext = new ApplicationDbContext(options);
            var service   = new PollsService(dbContext);

            dbContext.Polls.Add(new Poll());
            var user = new LessplasticUser
            {
                UserName = "******"
            };

            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            var poll = dbContext.Polls.First();

            service.AddParticipant(poll, "test");

            Assert.Equal(1, dbContext.PollsUsers.Count());
        }
Пример #4
0
        public bool AddParticipant(Event myEvent, string username)
        {
            LessplasticUser user = this.context.Users.FirstOrDefault(x => x.UserName == username);

            if (user == null)
            {
                return(false);
            }

            var userEvent = new UserEvents
            {
                EventId           = myEvent.Id,
                Event             = myEvent,
                LessplasticUserId = user.Id,
                LessplasticUser   = user
            };

            user.Events.Add(userEvent);
            myEvent.Participants.Add(userEvent);

            this.context.SaveChanges();
            return(true);
        }
Пример #5
0
        public bool AddParticipant(Poll poll, string username)
        {
            LessplasticUser user = this.context.Users.FirstOrDefault(x => x.UserName == username);

            if (user == null)
            {
                return(false);
            }

            var pollsUsers = new PollsUsers
            {
                LessplasticUser   = user,
                LessplasticUserId = user.Id,
                Poll   = poll,
                PollId = poll.Id
            };

            user.Pools.Add(pollsUsers);
            poll.Users.Add(pollsUsers);
            this.context.SaveChanges();

            return(true);
        }
Пример #6
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                bool hasUser = true;

                var user = new LessplasticUser {
                    UserName = Input.UserName, Email = Input.Email
                };

                if (!_signInManager.UserManager.Users.Any())
                {
                    hasUser = false;
                }
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    var townExists = this.context.Towns.FirstOrDefault(t => t.TownName == Input.TownName);

                    if (townExists == null)
                    {
                        var town = new Town
                        {
                            TownName = Input.TownName
                        };
                        town.Users.Add(user);
                        this.context.Towns.Add(town);
                        this.context.SaveChanges();
                    }
                    else
                    {
                        townExists.Users.Add(user);
                        this.context.SaveChanges();
                    }

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (!hasUser)
                    {
                        var roleResult = _signInManager.UserManager.AddToRoleAsync(user, "Admin").Result;
                        if (roleResult.Errors.Any())
                        {
                            return(Page());
                        }
                    }
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }