示例#1
0
        /// <summary>
        /// populate user with information,
        /// assigns him a role
        /// </summary>
        private async Task FillUserAsync()
        {
            var user = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User
                {
                    FirstName      = "Joana",
                    LastName       = "Roque",
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    PhoneNumber    = "965214744",
                    Address        = "Rua da Programação",
                    EmailConfirmed = true
                };

                await _userHelper.AddUserAsync(user, "gfdGF545++");

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            var isInRole = await _userHelper.IsUserInRoleAsync(user, "Admin");

            if (!isInRole)
            {
                await _userHelper.AddUSerToRoleAsync(user, "Admin");
            }

            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Register(RegisterUserViewModel 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 password = UtilityHelper.Generate();

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

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

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

                        var myToken = await _userHelper.GeneratePasswordResetTokenAsync(user);

                        var link = Url.Action(
                            "ResetPassword",
                            "Account",
                            new
                        {
                            token  = myToken,
                            userId = user.Id
                        },
                            protocol: HttpContext.Request.Scheme);

                        _mailHelper.SendToNewUser(user.Email, link, user.Name);

                        return(RedirectToAction(nameof(ListUsers)));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "This username is already registered.");
                }
            }
            model.Countries = _countryRepository.GetComboCountries();
            model.Roles     = _userHelper.GetComboRoles();
            model.Genders   = _clientRepository.GetComboGenders();
            return(View(model));
        }
示例#3
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));
        }
        public async Task <IActionResult> EditUser(EditUserViewModel editUser)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByIdAsync(editUser.Id);

                if (user == null)
                {
                    return(new NotFoundViewResult("UserNotFound"));
                }

                user.FirstName   = editUser.FirstName;
                user.LastName    = editUser.LastName;
                user.Address     = editUser.Address;
                user.PhoneNumber = editUser.PhoneNumber;

                var selectedRole = await _roleManager.FindByIdAsync(editUser.SelectedRole);

                foreach (var currentRole in _roleManager.Roles.ToList())
                {
                    var isSelectedRole = selectedRole.Name.Equals(currentRole.Name);
                    if (!isSelectedRole && await _userHelper.IsUserInRoleAsync(user, currentRole.Name))
                    {
                        await _userManager.RemoveFromRoleAsync(user, currentRole.Name);
                    }
                }

                await _userHelper.AddUSerToRoleAsync(user, selectedRole.Name);

                var result = await _userHelper.UpdateUserAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListUsers"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(editUser));
        }
示例#5
0
        public async Task <IActionResult> ConfirmEmail(string userId, string token)
        {
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(token))
            {
                return(NotFound());
            }

            var user = await _userHelper.GetUserByIdAsync(userId);

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

            var result = await _userHelper.ConfirmEmailAsync(user, token);

            if (!result.Succeeded)
            {
                return(NotFound());
            }

            var isInRole = await _userHelper.IsUserInRoleAsync(user, "Customer");

            if (!isInRole)
            {
                await _userHelper.AddUSerToRoleAsync(user, "Customer");
            }

            var owner = new Owner
            {
                Appointments = new List <Appointment>(),
                Pets         = new List <Pet>(),
                User         = user,
                CreatedBy    = user,
                ModifiedBy   = user,
                CreateDate   = DateTime.Now,
                UpdateDate   = DateTime.Now
            };

            await _ownerRepository.CreateAsync(owner);

            return(View());
        }
示例#6
0
        public async Task <IActionResult> Create(RegisterNewViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    Address     = model.Address,
                    Email       = model.UserName,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    UserName    = model.UserName
                };

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

                if (response.Succeeded)
                {
                    var userInDB = await _userHelper.GetUserByEmailAsync(model.UserName);

                    await _userHelper.AddUSerToRoleAsync(userInDB, "Customer");

                    var creator = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                    var owner = new Owner
                    {
                        Appointments = new List <Appointment>(),
                        Pets         = new List <Pet>(),
                        User         = userInDB,
                        CreatedBy    = creator,
                        ModifiedBy   = creator,
                        CreateDate   = DateTime.Now,
                        UpdateDate   = DateTime.Now
                    };

                    await _ownerRepository.CreateAsync(owner);

                    try
                    {
                        var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(userInDB);

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

                        _mailHelper.SendMail(model.UserName, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                             $"To allow the user, " +
                                             $"please click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");

                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.ToString());
                        return(View(model));
                    }
                }

                ModelState.AddModelError(string.Empty, response.Errors.FirstOrDefault().Description);
            }

            return(View(model));
        }