Exemple #1
0
        public virtual IActionResult CreateNewUser()
        {
            var model = new AdminCreateNewUserViewModel();

            model.RoleType = typeof(Role);

            return(this.View(model));
        }
Exemple #2
0
        public virtual async Task <IActionResult> CreateNewUser(AdminCreateNewUserViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var selectedRole = await this.EntityManager.GetEntityAsync <Role, Role>(model.Role);

                var user = await this.authenticationService.CreateUserAsync <User>(
                    model.Email,
                    StringFunctions.GenerateRandomPassword(),
                    model.FirstName,
                    model.LastName,
                    new string[] { selectedRole.Name });

                if (user != null)
                {
                    var resetToken = await this.identityService.GeneratePasswordResetTokenAsync(user);

                    var resetPasswordUrl = this.Url.Action("ResetPassword", "AdminAccount", new { userId = user.Id, resetToken = resetToken }, this.HttpContext.Request.Scheme);

                    var mailModel = new UserResetPasswordEmailModel
                    {
                        Email            = user.Email,
                        Subject          = "Password Reset",
                        GivenName        = user.FirstName,
                        Surname          = user.LastName,
                        resetPasswordUrl = resetPasswordUrl,
                    };
                    var result = await this.emailService.SendEmailAsync("UserResetPasswordEmailView", mailModel);

                    if (result.Success)
                    {
                        this.TempData["SuccessStatusMessage"] =
                            $"User has been created successfully.";
                    }
                    else
                    {
                        this.TempData["ErrorStatusMessage"] = $"An error occured while creating user.";
                    }

                    return(this.RedirectToAction(nameof(this.GetAll)));
                }
            }

            return(this.View(model));
        }