예제 #1
0
        public async Task <IActionResult> RegisterClient(RegisterNewUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(model.EmailAddress);

                if (user == null)
                {
                    try
                    {
                        var country = await _countryRepository.GetByIdAsync(model.CountryId);

                        user = _converterHelper.ToUser(model, country);

                        var result = await _userHelper.AddUserAsync(user, model.Password);

                        if (result != IdentityResult.Success)
                        {
                            this.ModelState.AddModelError(string.Empty, "The user couldn't be created.");
                            return(this.View(model));
                        }

                        await _userHelper.AddUSerToRoleAsync(user, user.SelectedRole);

                        var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                        var tokenLink = this.Url.Action("ConfirmEmailClient", "Account", new
                        {
                            userid = user.Id,
                            token  = myToken
                        }, protocol: HttpContext.Request.Scheme);

                        _mailHelper.SendToNewClient(user.Email, tokenLink, user.Name);

                        await _notificationHelper.CreateNotificationAsync(user.GuidId, UserType.Admin, "New Client Registration", NotificationType.NewClient);

                        return(RedirectToAction(nameof(ConfirmRegistration)));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }

                this.ModelState.AddModelError(string.Empty, "There's already an account with this email address.");
            }
            model.Countries = _countryRepository.GetComboCountries();
            model.Genders   = _clientRepository.GetComboGenders();

            return(View(model));
        }