// GET: Clients/Create
        public IActionResult Create()
        {
            var model = new RegisterNewClientViewModel()
            {
                //ProgramTiers = _combosHelper.GetProgramTiers()
            };

            return(View(model));
        }
        public IActionResult Register()
        {
            var model = new RegisterNewClientViewModel
            {
                //Countries = _countryRepository.GetComboCountries(),
                //Cities = _countryRepository.GetComboCities(0)
            };

            View(model);

            return(View());
        }
        public async Task <IActionResult> Create(RegisterNewClientViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(model.Username);

                if (user == null)
                {
                    user = new User
                    {
                        FirstName             = model.FirstName,
                        LastName              = model.LastName,
                        Email                 = model.Username,
                        UserName              = model.Username,
                        PhoneNumber           = model.PhoneNumber,
                        RequirePasswordChange = true,
                        MainRole              = "Client"
                    };

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

                    if (result != IdentityResult.Success)
                    {
                        ModelState.AddModelError(string.Empty, "The user could not be created.");
                        //model.ProgramTiers = _combosHelper.GetProgramTiers();
                        return(View(model));
                    }

                    var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                    // TODO: Confirm with human interaction
                    await _userHelper.ConfirmEmailAsync(user, token);

                    user = await _userHelper.GetUserByEmailAsync(user.Email);

                    await _clientRepository.CreateClientWithUserAsync(user, model.BirthDate.Value);

                    return(RedirectToAction(nameof(Index)));
                }

                ModelState.AddModelError(string.Empty, "The username is already taken.");
            }
            //model.ProgramTiers = _combosHelper.GetProgramTiers();
            return(View(model));
        }
        public async Task <IActionResult> Register(RegisterNewClientViewModel model)
        {
            if (ModelState.IsValid)
            {
                var today = DateTime.UtcNow;

                var age = today.Year - model.BirthDate.Value.Year;

                if (model.BirthDate.Value.Date > today.AddYears(-age))
                {
                    age--;
                }

                if (age >= 2)
                {
                    var user = await _userHelper.GetUserByEmailAsync(model.Username);

                    if (user == null)
                    {
                        //var city = await _countryRepository.GetCityAsync(model.CityId);

                        user = new User
                        {
                            FirstName   = model.FirstName,
                            LastName    = model.LastName,
                            Email       = model.Username,
                            UserName    = model.Username,
                            PhoneNumber = model.PhoneNumber,
                        };

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

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

                        //TODO: Error message
                        await _clientRepository.CreateClientWithUserAsync(user, model.BirthDate.Value);

                        var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

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

                        var client = await _clientRepository.GetClientByUserAsync(user);

                        //TODO Prettify Email
                        _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                             "Welcome to CinelAirMiles!<br/><br/>" +
                                             $"To confirm that this is your email, <a href = \"{tokenLink}\">click here</a> and we will activate your account.<br/>" +
                                             $"After that is done, you may login using your personal, newly issued, Program Number: <b>{client.MilesProgramNumber}</b>");
                        ViewBag.Message = "The instructions to allow your user has been sent to email.";

                        return(RedirectToAction("SuccessfulRegistration"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "This username already exists");
                    }
                }
                else
                {
                    ModelState.AddModelError("BirthDate", $"The client must be older than 2 years old.");
                }
            }
            return(View(model));
        }
Esempio n. 5
0
        public async Task <IActionResult> Register(RegisterNewClientViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Gets the User
                var user = await _userRepository.GetUserByEmailAsync(model.Username);

                if (user == null)
                {
                    // Creates a new Client
                    var client = new Client
                    {
                        RevisionMonth = DateTime.Now.Month,
                        IsAproved     = true,
                        IsDeleted     = false,
                        User          = new User
                        {
                            BirthDate = new DateTime(1990, 2, 15),
                            FirstName = model.FirstName,
                            LastName  = model.LastName,
                            UserName  = model.Username,
                            Email     = model.Username,
                            PhotoUrl  = "~/images/Users/Default_User_Image.png"
                        }
                    };

                    // Adds the User to the DataBase
                    var result = await _userRepository.AddUserAsync(client.User, model.Password);

                    if (result != IdentityResult.Success)
                    {
                        ModelState.AddModelError(string.Empty, "The User could not be created");
                        return(View(model));
                    }

                    // Add user to Role
                    await _userRepository.AddUsertoRoleAsync(client.User, "Client");

                    await _userRepository.AddUsertoRoleAsync(client.User, "Basic");

                    // Adds the Client to the DataBase
                    await _clientRepository.CreateAsync(client);

                    // Retrieves the Client ID
                    var clientID = await _clientRepository.GetByEmailAsync(model.Username);

                    // Creates a Token in order to confirm the email

                    var myToken = await _userRepository.GenerateEmailConfirmationTokenAsync(client.User);

                    // Defines the Link with its properties to be sent in the email
                    var tokenLink = this.Url.Action("ConfirmAccount", "Account", new
                    {
                        userid = client.User.Id,
                        token  = myToken,
                    }, protocol: HttpContext.Request.Scheme);

                    //Sends an Email to the User with the TokenLink
                    try
                    {
                        _mailHelper.SendMail(model.Username, "Account Confirmation", $"<h1>Account Confirmation</h1>" +
                                             $"To finish your account registration, " +
                                             $"please click this link: <a href = \"{tokenLink}\">Confirm Account</a>"
                                             + $"<br/><br/>Your Account ID is: {clientID.Id:D9}");
                        this.ViewBag.Message = "The instructions to confirm your account have been sent to the email.";
                    }
                    catch (Exception)
                    {
                        this.ModelState.AddModelError(string.Empty, "Error sending the email, please try again in a few minutes");
                    }
                    return(this.View(model));
                }

                ModelState.AddModelError(string.Empty, "A User with this email is already registered");
            }

            return(View(model));
        }