public IHttpActionResult EditUserProfile(EditProfileBindingModel model) { if (!ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } // Validate the current user exists in the database var currentUserId = User.Identity.GetUserId(); int result = this.repository.EditUserProfile(currentUserId, model.UserName, model.Email); switch (result) { case 3: return(BadRequest("This email is already taken!")); case 2: return(BadRequest("This name is already taken!")); case 0: return(BadRequest("Invalid user token! Please login again!")); default: return(Ok(new { message = "User profile edited successfully." })); } ; }
public async Task <ActionResult> Edit(EditProfileBindingModel model) { if (this.ModelState.IsValid) { var user = await this.UserManager.FindByIdAsync(this.User.Identity.GetUserId()); user.Fullname = model.Fullname; user.Email = model.Email; var result = await this.UserManager.UpdateAsync(user); if (result.Succeeded) { await this.SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); this.TempData["Success"] = new[] { "Edit successfull" }; } else { this.AddErrors(result); } } return(View(model)); }
public async Task UploadUserProfilePicture( string username, bool hasOwnProfilePicture, EditProfileBindingModel epbm) { var memoryStream = new MemoryStream(); epbm.PostedFileBase.InputStream.CopyTo(memoryStream); var imageBytes = InternalService.ImageService .Load(memoryStream.ToArray()) .GetRectangledImage() .GetManualSizedRectangle(250) .To72DPI() .ToJpeg() .ToByteArray(); var userPath = this.ConvertUsernameToApiDirectory(username); if (!InternalService.DropboxService.IsDirectoryExists(userPath)) { InternalService.DropboxService.CreateDirectory(userPath); } var filePath = $"{userPath}/profile.jpg"; await InternalService.DropboxService.UploadFile(filePath, imageBytes, hasOwnProfilePicture); }
public int AdminUpdate(EditProfileBindingModel model, Admin user) { if (user != null) { user.Admin_firstname = model.Admin_firstname; user.Admin_lastname = model.Admin_lastname; user.Admin_gender = model.Admin_gender; user.Admin_dob = model.Admin_dob; user.Admin_aadhar_id = model.Admin_aadhar_id; user.Admin_pan_card = model.Admin_pan_card; user.Admin_gst_id = model.Admin_gst_id; } this.binding.GetAdminRepository.Attach(user); int result = this.binding.Save(); if (result > 0) { result = 1; return(result); } else { result = 0; return(result); } }
public IHttpActionResult EditProfile(EditProfileBindingModel model) { int result; //var id=User.Identity.GetUserId(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Admin admin = _adminLog.GetByUsername(model.username); // ApplicationUser user;//= await UserManager.FindByNameAsync(model.username); //user = applicationDbContext.Users.AsNoTracking().Where(a => a.UserName == model.username).FirstOrDefault(); if (admin == null) { return(BadRequest("No such user. Try Again")); } result = _adminLog.AdminUpdate(model, admin); if (result == 1) { return(Ok("Changes Saved Successfully!")); } return(InternalServerError()); }
public ActionResult Edit(EditProfileBindingModel model) { var loggedUserId = User.Identity.GetUserId(); var profile = Data.ApplicationUsers.GetById(loggedUserId); if (model.FullName != null) { profile.FullName = model.FullName; } if (model.AvatarUrl != null) { profile.AvatarUrl = model.AvatarUrl; } if (model.Biography != null) { profile.Biography = model.Biography; } if (model.BirthDay != null) { profile.BirthDay = model.BirthDay; } if (model.Website != null) { profile.Website = model.Website; } this.Data.SaveChanges(); this.TempData["SuccessMessage"] = "Profile successfully edited!"; return(RedirectToAction("Index", "Profile")); }
public async Task <IHttpActionResult> EditProfile(EditProfileBindingModel model) { if (!ModelState.IsValid || model == null) { return(BadRequest(ModelState)); } var userStore = new UserStore <ApplicationUser>(new ApplicationDbContext()); var appManager = new UserManager <ApplicationUser>(userStore); var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); user.Address = model.Address; user.Name = model.Name; user.PhoneNumber = model.PhoneNumber; var result = await UserManager.UpdateAsync(user); // IdentityResult result = await UserManager.FindByIdAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } var ctx = userStore.Context.SaveChanges(); return(Ok()); }
public void EditProfile(EditProfileBindingModel model, string userId) { User currentUser = this.GetCurrentUser(userId); currentUser.FirstName = model.FirstName; currentUser.LastName = model.LastName; this.Context.SaveChanges(); }
public IHttpActionResult EditProfileInfo(EditProfileBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var currentUserId = this.User.Identity.GetUserId(); var currentUser = this.Data.ApplicationUsers.All().FirstOrDefault(u => u.Id == currentUserId); if (currentUser == null) { return this.BadRequest("Invalid user token."); } var userEmail = this.Data.ApplicationUsers.All().FirstOrDefault(u => u.Email == model.Email); if (userEmail != null && userEmail.Id != currentUserId) { return this.BadRequest("Email is already taken."); } currentUser.Name = model.Name; currentUser.Email = model.Email; currentUser.Gender = model.Gender; if (model.ProfileImageData != null && model.ProfileImageData.IndexOf(',') == -1) { model.ProfileImageData = string.Format("{0}{1}", "data:image/jpg;base64,", model.ProfileImageData); } currentUser.ProfileImageData = model.ProfileImageData; if (model.CoverImageData != null && model.CoverImageData.IndexOf(',') == -1) { model.CoverImageData = string.Format("{0}{1}", "data:image/jpg;base64,", model.CoverImageData); } currentUser.CoverImageData = model.CoverImageData; this.Data.SaveChanges(); return this.Ok(new { message = "User profile updated successfully." }); }
public async Task <IActionResult> Profile() { //Finds user by his id string id = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; User user = await this.userService.FindUserById(id); EditProfileBindingModel model = new EditProfileBindingModel() { Email = user.Email, Username = user.UserName, FirstName = user.FirstName, LastName = user.LastName, PhoneNumber = user.PhoneNumber, PictureUrl = user.PictureUrl }; return(View(model)); }
public async Task <IActionResult> Edit([FromBody] EditProfileBindingModel collection) { if (string.IsNullOrWhiteSpace(collection?.Token)) { return(BadRequest(GeneralMessage.TokenNotFound)); } if (string.IsNullOrWhiteSpace(collection?.DeviceId)) { return(BadRequest(GeneralMessage.DeviceIdNotFound)); } if (!string.IsNullOrWhiteSpace(collection.Avatar) && collection.Avatar.ToLower() != "remove") { return(BadRequest("درخواست غیر مجاز!")); } if (string.IsNullOrWhiteSpace(collection.NickName) && string.IsNullOrWhiteSpace(collection.Avatar)) { return(BadRequest("نام مستعار خود را وارد نمایید")); } if (collection.Avatar == string.Empty) { collection.Avatar = null; } try { var model = _mapper.Map <UserEditSchema>(collection); await _userService.EditAsync(model); switch (model.StatusCode) { case 1: return(Ok()); case -1: return(BadRequest(GeneralMessage.UserNotFound)); case -2: return(BadRequest(GeneralMessage.UserIsNotActive)); } } catch (Exception ex) { await _exceptionService.InsertAsync(ex, URL, IP); } return(InternalServerError()); }
public ActionResult Edit([Bind(Include = "FirstName,LastName")] EditProfileBindingModel model) { string userId = User.Identity.GetUserId(); var indexOfAt = User.Identity.Name.IndexOf("@"); var currentUsername = User.Identity.Name.Substring(0, indexOfAt); if (this.ModelState.IsValid) { this.service.EditProfile(model, userId); return(this.RedirectToAction("ProfilePage", new { username = currentUsername })); } EditProfileViewModel viewModel = this.service.GetEditProfileViewModel(currentUsername); if (viewModel == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } return(this.View(viewModel)); }
public override ActionResult Profile(EditProfileBindingModel model) { if (!this.ModelState.IsValid) { return this.View("GetUserByUsername", model); } var user = this.Data.Users.All() .FirstOrDefault(u => u.Email == model.Email); if (user == null) { return this.RedirectToAction("Users", "Admin"); } if (user.Id == this.User.Identity.GetUserId()) { this.TempData["message"] = "Admin profile can not be edited!"; return this.RedirectToAction("Users", "Admin"); } user.Name = model.Name; if (model.BirthDate != null) { user.BirthDate = model.BirthDate; } if (model.ProfilePicture != null) { user.ProfilePicture = model.ProfilePicture; } user.Gender = model.Gender; this.Data.SaveChanges(); this.TempData["message"] = "User profile of [" + model.Name + "] edited successfully"; return this.RedirectToAction("Users", "Admin"); }
public void CallingEditProfileActionWithInCorrectModelShouldNotMakeChangesToDatabase() { this.LoginMock(true); var editProfileBindingModel = new EditProfileBindingModel { Name = null, BirthDate = DateTime.Now }; this.meController.ModelState.AddModelError("Name", "Current value: " + editProfileBindingModel.Name); var result = this.meController.Profile(editProfileBindingModel); Assert.IsInstanceOfType(result, typeof(ViewResult)); var viewResult = result as ViewResult; Assert.IsInstanceOfType(viewResult.Model, typeof(EditProfileBindingModel)); var fakeUser = this.fakeUsers.FirstOrDefault(); Assert.AreNotEqual(editProfileBindingModel.Name, fakeUser.Name); Assert.AreNotEqual(editProfileBindingModel.BirthDate, fakeUser.BirthDate); }
public void CallingEditProfileActionWithCorrectModelAndProfilePictureShouldSuccesfullyEditUserProfileAndRedirectsToHomeIndex() { this.LoginMock(true); var editProfileBindingModel = new EditProfileBindingModel { Name = "Edited Name", BirthDate = DateTime.Now, ProfilePicture = "someBase64string" }; var result = this.meController.Profile(editProfileBindingModel); Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); var routeResult = result as RedirectToRouteResult; Assert.AreEqual(routeResult.RouteValues["controller"], "Home"); Assert.AreEqual(routeResult.RouteValues["action"], "Index"); var fakeUser = this.fakeUsers.FirstOrDefault(); Assert.AreEqual(editProfileBindingModel.Name, fakeUser.Name); Assert.AreEqual(editProfileBindingModel.BirthDate, fakeUser.BirthDate); Assert.AreEqual(editProfileBindingModel.ProfilePicture, fakeUser.ProfilePicture); }
public virtual ActionResult Profile(EditProfileBindingModel model) { if (this.User.IsInRole("Administrator")) { this.TempData["message"] = Messages.AdminCannotBeEdited; return this.RedirectToAction("Index", "Home"); } if (!this.ModelState.IsValid) { return this.View(model); } var userId = User.Identity.GetUserId(); var user = this.Data.Users.Find(userId); if (user == null) { this.RedirectToAction("Index", "Home"); } user.Name = model.Name; if (model.BirthDate != null) { user.BirthDate = model.BirthDate; } if (model.ProfilePicture != null) { try { var image = PictureUtills.CreateImageFromBase64(model.ProfilePicture); var thumbnail = PictureUtills.CreateThumbnailFromImage(image, 200); var thumbnailProfilePicture = PictureUtills.ConvertImageToBase64(thumbnail); user.ProfilePicture = thumbnailProfilePicture; } catch (Exception ex) { user.ProfilePicture = model.ProfilePicture; } } user.Gender = model.Gender; this.Data.SaveChanges(); return this.RedirectToAction("Index", "Home"); }
public async Task EditProfile( EditProfileBindingModel epbm, IPrincipal user, IAuthenticationManager authenticationManager, UserManager userManager) { var isImageUpdate = epbm.PostedFileBase != null; using (var context = this.GetDbContext) { var currentUser = context.Users.Find(user.Identity.GetUserId()); var isUserInfoIsUpdated = epbm.Email != currentUser.Email || epbm.FirstName != currentUser.FirstName || epbm.LastName != currentUser.LastName; if (isImageUpdate) { var accountUserProfile = new AccountProfileService(); await accountUserProfile.UploadUserProfilePicture( currentUser.UserName, currentUser.HasOwnProfilePicture, epbm); if (!currentUser.HasOwnProfilePicture) { currentUser.HasOwnProfilePicture = true; context.Users.AddOrUpdate(currentUser); await context.SaveChangesAsync(); } } if (isUserInfoIsUpdated) { currentUser.FirstName = epbm.FirstName; currentUser.LastName = epbm.LastName; currentUser.Email = epbm.Email; var newUsername = $"{epbm.FirstName} {epbm.LastName}"; if (currentUser.HasOwnProfilePicture) { InternalService.DropboxService .RenameFolder("/Users/", currentUser.UserName.Replace(" ", "-").ToLower(), newUsername.Replace(" ", "-").ToLower()); } currentUser.UserName = newUsername; context.Users.AddOrUpdate(currentUser); await context.SaveChangesAsync(); authenticationManager.SignOut( DefaultAuthenticationTypes.ExternalCookie); var identity = userManager.CreateIdentity( currentUser, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn( new AuthenticationProperties { IsPersistent = true }, identity); } } }
public ActionResult EditProfile(EditProfileBindingModel model) { if (!ModelState.IsValid) { return RedirectToAction("Index"); } var userId = User.Identity.GetUserId(); var user = this.Data.Users.Find(userId); if (user == null) { RedirectToAction("Index", "Home"); } user.Name = model.Name; if (model.BirthDate != null) { user.BirthDate = model.BirthDate; } if (model.ProfilePicture != null) { user.ProfilePicture = model.ProfilePicture; } user.Gender = model.Gender; this.Data.SaveChanges(); return RedirectToAction("Index", "Home"); }