public async Task <IActionResult> Read(string id) { User user = await _userManager.FindByIdAsync(id); UserUpdateVM userVM = new UserUpdateVM(_roleManager, _userManager, user); return(View(userVM)); }
public ActionResult UpdateUser(UserUpdateVM form) { ActionResult response; if (ModelState.IsValid) { //FirstName and LastName are optional, however cannot be null. This sets them to an empty string if they are null if (form.User.FirstName == null) { form.User.FirstName = ""; } if (form.User.LastName == null) { form.User.LastName = ""; } //try to connect to the server, and update users information try { UserDO user = Mapper.Mapper.UserPOtoDO(form.User); _UserDAO.UpdateUser(user); } //catch any sqlExceptions encountered during db call and log them catch (SqlException sqlEx) { if (!((bool)sqlEx.Data["Logged"] == true) || !sqlEx.Data.Contains("Logged")) { Logger.LogSqlException(sqlEx); } } catch (Exception ex) { if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false) { Logger.LogException(ex); } } //redirects based on if the user is updating their own account, or someone elses. //redirects mods and admins to the View all users page if they are updating someone elses account information if (Session["UserName"].ToString() != form.User.UserName) { response = RedirectToAction("Index", "Account"); } else { response = RedirectToAction("AccountView", "Account"); } } else { ModelState.AddModelError("", "Missing information, please enter all fields."); response = View(form); } return(response); }
public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,PhoneNumber,BirthDate,Email,PolyclinicId")] UserUpdateVM userr) { if (id != userr.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var docc = await _context.Doctor.FirstOrDefaultAsync(x => x.Id == userr.Id); if (docc == null) { ModelState.AddModelError(string.Empty, "Doctor Can Not Find"); return(View(userr)); } docc.PolyclinicId = userr.PolyclinicId; _context.Doctor.Update(docc); _context.SaveChanges(); var user = await _userManager.FindByIdAsync(docc.UserId.ToString()); if (user == null) { ModelState.AddModelError(string.Empty, "User Can Not Find"); return(View(userr)); } user.FirstName = userr.FirstName; user.LastName = userr.LastName; user.FullName = userr.FirstName + " " + userr.LastName; user.UserName = userr.Email; user.NormalizedUserName = userr.Email.ToUpper(); user.Email = userr.Email; user.NormalizedEmail = userr.Email.ToUpper(); user.BirthDate = userr.BirthDate; user.PhoneNumber = userr.PhoneNumber; await _userManager.UpdateAsync(user); } catch (DbUpdateConcurrencyException) { if (!ApplicationUserExists(userr.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(userr)); }
public async Task <JsonResult> Update([FromRoute] Guid id, [FromBody] UserUpdateVM model) { if (id.IsEmptyGuid()) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01003), StatusCodes.Status400BadRequest)); } else if (!ModelState.IsValid) { return(new JsonAPIResult(_apiResult.CreateVMWithModelState(modelStateDictionary: ModelState), StatusCodes.Status400BadRequest)); } var currentUser = await _userManager.FindByNameAsync(User.Identity.Name); if (currentUser == null) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01004), StatusCodes.Status404NotFound)); } var user = await _userManager.FindByIdAsync(id.ToString()); if (user == null) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01004), StatusCodes.Status404NotFound)); } if (currentUser.Id != user.Id) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01007), StatusCodes.Status403Forbidden)); } user.About = model.About; user.DisplayName = model.DisplayName; IdentityResult result; result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01010), StatusCodes.Status422UnprocessableEntity)); } return(new JsonAPIResult(_apiResult.CreateVM(user.Id, true), StatusCodes.Status200OK)); }
public ActionResult Profil(UserUpdateVM user) { AppUser x = new AppUser(); x = db.UserFromName(HttpContext.User.Identity.Name); if (x.isAllPropsFilled == false) { user.Nick = x.Nick; user.Password = x.Password; user.Email = x.Email; return(View(user)); } else { return(RedirectToAction("FullProfile", "Account")); } }
public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,PhoneNumber,BirthDate,Email")] UserUpdateVM userr) { if (id != userr.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var patientt = await _context.Patient.FirstOrDefaultAsync(x => x.Id == userr.Id); if (patientt == null) { ModelState.AddModelError(string.Empty, "Patient Can Not Find"); return(View(userr)); } var user = await _userManager.FindByIdAsync(patientt.UserId.ToString()); if (user == null) { ModelState.AddModelError(string.Empty, "User Can Not Find"); return(View(userr)); } user.FirstName = userr.FirstName; user.LastName = userr.LastName; user.FullName = userr.FirstName + " " + userr.LastName; user.UserName = userr.Email; user.NormalizedUserName = userr.Email.ToUpper(); user.Email = userr.Email; user.NormalizedEmail = userr.Email.ToUpper(); user.BirthDate = userr.BirthDate; user.PhoneNumber = userr.PhoneNumber; await _userManager.UpdateAsync(user); } catch (Exception ex) { throw; } return(RedirectToAction(nameof(Index))); } return(View(userr)); }
public ActionResult UpdateUser(int id) { ActionResult response; //creating a user view model including a UserPO, and a list of Roles UserUpdateVM user = new UserUpdateVM(); //try to connect to the db and aquire the user information filtered by the id provided, and a List of all Roles try { UserDO temp = _UserDAO.ViewSingleUser(id); user.User = Mapper.Mapper.UserDOtoPO(temp); user.Roles = Mapper.Mapper.RoleDOListToPOList(_UserDAO.GetRoleList()); } //catch any SqlExceptions we encounter during our db calls catch (SqlException sqlEx) { if (!((bool)sqlEx.Data["Logged"] == true) || !sqlEx.Data.Contains("Logged")) { Logger.LogSqlException(sqlEx); } } catch (Exception ex) { if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false) { Logger.LogException(ex); } } //Prevents users from altering someone elses information, unless they are a higher role than the user //being updated if (user.User.UserName == Session["UserName"].ToString() || (int)Session["Role"] > user.User.Role) { response = View(user); } else { response = RedirectToAction("Index", "Home"); } return(response); }
public async Task <JsonResult> Update(UserUpdateVM model) { try { var user = await _userManager.FindByIdAsync(model.Id.ToString()); if (user != null) { user.About = model.About; user.DisplayName = model.DisplayName; user.IsAdmin = model.IsAdmin; user.UserName = model.UserName; IdentityResult result; if (model.OldPassword != model.PasswordHash) { result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.PasswordHash); if (!result.Succeeded) { return(new JsonResult(APIResult.CreateVM(false, null, AppStatusCode.WRG01001))); } } result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { return(new JsonResult(APIResult.CreateVM(false, null, AppStatusCode.WRG01001))); } } return(new JsonResult(APIResult.CreateVM(true, user.Id))); } catch (Exception ex) { return(new JsonResult(APIResult.CreateVMWithError(ex, APIResult.CreateVM(false, null, AppStatusCode.ERR01001)))); } }
public IActionResult Update([FromBody] UserUpdateVM user) { if (!ModelState.IsValid) { var errors = CustomValidator.GetErrorsByModel(ModelState); return(BadRequest(errors)); } var str_name = user.Name; var name_regex = @"^[A-Za-z-а-яА-Я]+$"; var str_email = user.Email; var email_regex = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"; var match_name = Regex.Match(str_name, name_regex); var match_email = Regex.Match(str_email, email_regex); if (!match_name.Success) { return(BadRequest(new { Name = "Неправильний формат поля." })); } if (!match_email.Success) { return(BadRequest(new { Email = "Неправильний формат поля." })); } var emp = _context.Users.SingleOrDefault(p => p.Id == user.Id); if (emp != null) { emp.UserName = user.Name; emp.Email = user.Email; _context.SaveChanges(); return(Ok("Дані оновлено")); } return(BadRequest(new { Email = "Помилка оновлення" })); }
public string Update(UserUpdateVM user) { var http = (HttpWebRequest)WebRequest.Create(new Uri(_url)); http.Accept = "application/json"; http.ContentType = "application/json"; http.Method = "PUT"; string parsedContent = JsonConvert.SerializeObject(user); UTF8Encoding encoding = new UTF8Encoding(); Byte[] bytes = encoding.GetBytes(parsedContent); Stream newStream = http.GetRequestStream(); newStream.Write(bytes, 0, bytes.Length); newStream.Close(); var response = http.GetResponse(); var stream = response.GetResponseStream(); var sr = new StreamReader(stream); var content = sr.ReadToEnd(); return(content.ToString()); }
public ActionResult Profil(UserUpdateVM data, HttpPostedFileBase file) { string path; if (ModelState.IsValid) { if (file != null) { string displayName = Path.GetFileName(file.FileName); var uploadDirection = Path.Combine(Server.MapPath("~/Content/pp"), displayName); file.SaveAs(uploadDirection); path = displayName; } else { path = "defaultpp.png"; } string selectedRole = Request.Form["roleselect"].ToString(); if (selectedRole == "Ressam") { AppUser user = new AppUser() { Nick = data.Nick, Password = data.Password, Role = Role.Member, isAllPropsFilled = true, Email = data.Email, ID = db.GetByName(HttpContext.User.Identity.Name).ID, NameSurname = data.NameSurname, Phone = data.Phone, ProfileImage = path, AboutMe = data.Hakkimda, isArtist = true, Seen = true }; db.Update(user); } else if (selectedRole == "Kullanıcı") { AppUser user = new AppUser() { Nick = data.Nick, Password = data.Password, Role = Role.Member, isAllPropsFilled = true, Email = data.Email, ID = db.GetByName(HttpContext.User.Identity.Name).ID, NameSurname = data.NameSurname, Phone = data.Phone, ProfileImage = path, AboutMe = data.Hakkimda, isArtist = false, Seen = true }; db.Update(user); } return(RedirectToAction("FullProfile", "Account")); } return(View()); }
public Task <string> UpdateAsync(UserUpdateVM user) { return(Task.Run(() => Update(user))); }