Exemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                ApplicationUser user;
                if (model.CreateTestAccount == true)
                {
                    user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email, EmailConfirmed = true
                    };
                }
                else
                {
                    user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email
                    };
                }
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (model.CreateTestAccount == true)
                    {
                        _logger.LogInformation("Create random generated user for tests.");
                        await _userManager.AddToRoleAsync(user, "Member");

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

                        string            testUserName = "******" + (_playerRepository.Players.Count() + 1);
                        SummonerInfoModel testSummoner = new SummonerInfoModel
                        {
                            profileIconId   = 1,
                            name            = testUserName,
                            summonerLevel   = 100,
                            accountId       = 1000 + (_playerRepository.Players.Count() + 1),
                            id              = 1000 + (_playerRepository.Players.Count() + 1),
                            revisionDate    = 0,
                            IsVerified      = true,
                            ConectedAccount = model.Email,
                            AddTime         = DateTime.Now,
                            ConnectedTime   = DateTime.Now,
                            LockedToAssign  = true
                        };
                        _summonerRepository.SaveSummonerInfo(testSummoner);

                        Player testPlayer = new Player
                        {
                            ConectedSummoners      = _summonerRepository.SummonerInfos.Where(s => s.name == testUserName).FirstOrDefault(),
                            FirstName              = testUserName,
                            ConnectedSummonerEmail = user.Email,
                            Created = DateTime.Now
                        };
                        _playerRepository.CreateBasicPlayer(testPlayer);
                    }
                    else
                    {
                        _logger.LogInformation("User created a new account with password.");

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                        await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                        //await _signInManager.SignInAsync(user, isPersistent: false); //nie zaloguje się po rejestracji bez potwierdzenia maila

                        await _userManager.AddToRoleAsync(user, "Member");

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

                        //nowo zarejestrowany użytkownik zostanie przypisany modelowi Player z przypisanym emailem
                        Player player = new Player
                        {
                            ConectedSummoners      = null,
                            FirstName              = null,
                            ConnectedSummonerEmail = user.Email,
                            Created = DateTime.Now
                        };
                        _playerRepository.CreateBasicPlayer(player);
                    }
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }