// GET: UserMng/Form/:id public ActionResult Form(string id) { Models.UserDetailVM model = null; if (id != null) { var user = this.aspNetUserService.Get(id); if (user != null) { model = new Models.UserDetailVM { UserName = user.UserName, FullName = user.FullName, isActive = user.isActive, PhoneNumber = user.PhoneNumber, Email = user.Email, BrandId = user.BrandID, Id = user.Id, }; var userRoles = UserManager.GetRoles(user.Id).ToArray(); ViewBag.userRoles = userRoles; if (userRoles.Length > 0) { model.Role = userRoles[0]; } } } ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View(model)); }
public async System.Threading.Tasks.Task <ActionResult> Update(Models.BrandUserUpdateVM model) { if (ModelState.IsValid) { /*Update custom fields*/ var user = aspNetUserService .Get(a => a.Id == model.Id) .FirstOrDefault(); var currUser = Helper.GetCurrentUser(); if (user != null) { user.BrandID = currUser.BrandID; user.FullName = model.FullName; user.isActive = model.isActive; await this.aspNetUserService.UpdateAsync(user); /*Update user role*/ //Remove from other Roles var userRoles = UserManager.GetRoles(user.Id).ToArray(); if (userRoles.Length > 0) { UserManager.RemoveFromRole(user.Id, userRoles[0]); } //Add to new Role if (model.Role.CompareTo("System Admin") == 0) { model.Role = "Active User"; } UserManager.AddToRoles(user.Id, new string[] { model.Role }); Session.Clear(); Session["UPDATE_RESULT"] = true; return(new ContentResult { Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "BrandUserMng")), ContentType = "text/html" }); } ; } // If we got this far, something failed, redisplay form ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View("Form", model)); }
public async System.Threading.Tasks.Task <ActionResult> Add(Models.BrandUserDetailVM model) { if (ModelState.IsValid) { var currUser = Helper.GetCurrentUser(); var user = new Wisky.Models.ApplicationUser { UserName = model.UserName, Email = model.Email, FullName = model.FullName, PhoneNumber = model.PhoneNumber, BrandId = currUser.BrandID, isActive = model.isActive, }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { Session.Clear(); Session["ADD_RESULT"] = true; if (model.Role.CompareTo("System Admin") == 0) { model.Role = "Active User"; } UserManager.AddToRoles(user.Id, new string[] { model.Role }); return(new ContentResult { Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "BrandUserMng")), ContentType = "text/html" }); } } // If we got this far, something failed, redisplay form ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View("Form", model)); }