public ActionResult Create(AddNewUserViewModel model)
        {
            ViewBag.Msg      = "";
            ViewBag.Branches = branchRepo.GetAll().AsEnumerable().Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.ID.ToString()
            });

            ViewBag.Roles = roleRepo.GetAll().AsEnumerable().Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.ID.ToString()
            });

            if (ModelState.IsValid)
            {
                //unique username and email
                if (!userLogic.IsUniqueUsername(model.Username))
                {
                    ViewBag.Msg = "Username must be unique";
                    return(View());
                }
                if (!userLogic.IsUniqueEmail(model.Email))
                {
                    ViewBag.Msg = "Email must be unique";
                    return(View());
                }

                string autoGenPassword = utilLogic.GetRandomPassword();
                string hashedPassword  = UserLogic.HashPassword(autoGenPassword);
                User   user            = new Core.Models.User {
                    FirstName = model.FirstName, LastName = model.LastName, Username = model.Username, PasswordHash = hashedPassword, Email = model.Email, PhoneNumber = model.PhoneNumber, Role = roleRepo.GetById(model.RoleId), Branch = branchRepo.GetById(model.BranchId)
                };

                userRepo.Insert(user);

                userLogic.SendPasswordToUser(model.LastName + " " + model.FirstName, model.Email, model.Username, autoGenPassword);

                return(RedirectToAction("Create", new { message = "User added" }));
            }
            ViewBag.Msg = "Please enter a valid name";
            return(View());
        }
Example #2
0
        public ActionResult Create(AddNewUserViewModel model)
        {
            ViewBag.Msg      = "";
            ViewBag.Branches = branchRepo.GetAll().AsEnumerable().Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.ID.ToString()
            });

            ViewBag.Roles = roleRepo.GetAll().AsEnumerable().Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.ID.ToString()
            });

            if (ModelState.IsValid)
            {
                //unique username and email that has been been confirmed by any user.
                if (!userLogic.IsUniqueUsername(model.Username))
                {
                    ViewBag.Msg = "Username must be unique";
                    return(View());
                }
                if (!userLogic.IsUniqueEmail(model.Email))
                {
                    // edit, email musn't be unique
                    // only if email has been confirmed by another user.
                    // if email has been confirmed by any user.
                    // check if anyone with email has confirmed it.
                    if (userLogic.IsEmailConfirmed(model.Email))
                    {
                        ViewBag.Msg = "Email must be unique";
                        return(View());
                    }
                }

                string autoGenPassword  = utilLogic.GetRandomPassword();
                string hashedPassword   = UserLogic.HashPassword(autoGenPassword);
                string verificationCode = Guid.NewGuid().ToString();

                User user = new Core.Models.User {
                    TokenExpiryDate = DateTime.Now.AddMinutes(tokenExpiryMinutes), VerificationCode = verificationCode, FirstName = model.FirstName, LastName = model.LastName, Username = model.Username, PasswordHash = hashedPassword, Email = model.Email, PhoneNumber = model.PhoneNumber, EmailConfirmed = false, Role = roleRepo.GetById(model.RoleId), Branch = branchRepo.GetById(model.BranchId)
                };

                userRepo.Insert(user);

                // send email confirmation
                var callbackUrl = Url.Action("ConfirmEmail", "UserManager", new { userId = user.ID, code = verificationCode }, protocol: Request.Url.Scheme);

                try
                {
                    userLogic.SendEmailConfirmationTokenToUser(callbackUrl, model.Email);
                    userLogic.SendPasswordToUser(model.LastName + " " + model.FirstName, model.Email, model.Username, autoGenPassword);
                }
                catch (Exception)
                {
                    return(RedirectToAction("Create", new { message = "[User added : " + autoGenPassword + "][ CallbackUrl : " + callbackUrl + " ] .Send Mail Failed." }));
                }

                // tell them confirmation link has been sent to user mail
                // you dont need to show the user pass and call back since mail send was successful.
                return(RedirectToAction("Create", new { message = "[User added : " + model.Username + "][Confirmation link and password has been sent to user mail]" }));
            }
            ViewBag.Msg = "Please enter a valid name";
            return(View());
        }