예제 #1
0
        public async Task <IActionResult> Create(
            [Bind("UserEntity, Roles")] UsersCreateUpdateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.RoleDropDown = RoleDefaults();
                return(View(model));
            }
            if (model.UserEntity.Password != model.UserEntity.ConfirmPassword)
            {
                model.RoleDropDown   = RoleDefaults();
                ViewBag.errorMessage = "Password and Confirm Password fields must match!";
                return(View(model));
            }

            string       _userName     = HttpContext.User.Identity.Name;
            CreateObject _actionObject = _accountActions.CreatePost(_userName, model);

            if (!_actionObject.UserIsAdmin)
            {
                return(Error("You need to be logged in as admin to do this."));
            }
            else if (!_actionObject.UserCreated)
            {
                model.RoleDropDown   = RoleDefaults();
                ViewBag.errorMessage = _actionObject.Message;
                return(View(model));
            }
            if (_actionObject.UserCreated)
            {
                TempData["success"] = "User created.";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["error"] = "Oops, something went wrong, try again.";
                return(RedirectToAction("Index"));
            }
        }