public ActionResult UploadAvatar(UserProfileViewModel model) { if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0 && model.ImageUpload.ContentLength < 1024*1024) { var profile = db.UserProfiles.First(p => p.UserName == User.Identity.Name); var customExisting = db.Avatars.FirstOrDefault(p => p.CustomForUserId == profile.UserId); if (customExisting != null) { if(!String.IsNullOrEmpty(customExisting.Key)) S3.DeleteFile(customExisting.Key); S3File f = S3.UploadUserAvatar( profile.UserId.ToString(), model.ImageUpload.FileName, model.ImageUpload.InputStream); customExisting.Url = f.Url; customExisting.Key = f.Key; profile.Avatar = customExisting; } else { var f = S3.UploadUserAvatar( profile.UserId.ToString(), model.ImageUpload.FileName, model.ImageUpload.InputStream); Avatar custom = new Avatar() { CustomForUserId = profile.UserId, Url = f.Url, Key = f.Url }; db.Avatars.Add(custom); profile.Avatar = custom; } db.SaveChanges(); ((UserProfile)Session["UserInfo"]).Avatar = profile.Avatar; } return RedirectToAction("Index"); }
// GET: Profile /// <summary> /// /// </summary> /// <returns></returns> public ActionResult Index() { if (!User.Identity.IsAuthenticated) { return Redirect("/"); } else { var profile = db.UserProfiles .Include(p => p.Avatar) .Include(p => p.City) .Include(p=>p.Country) .First(p => p.UserName == User.Identity.Name); UserProfileViewModel profilevm = new UserProfileViewModel(); SDC.Library.Tools.CopySimpleProperties.Copy(profile, profilevm); SDC.Library.Tools.CopySimpleProperties.Copy(profile.Avatar, profilevm.Avatar); if (profile.Country != null) SDC.Library.Tools.CopySimpleProperties.Copy(profile.Country, profilevm.Country); if(profile.City != null) SDC.Library.Tools.CopySimpleProperties.Copy(profile.City, profilevm.City); profilevm.ComputedProperty = "some value"; var customAvatar = (from av in db.Avatars where av.CustomForUserId == profile.UserId select av) .FirstOrDefault(); if (customAvatar != null) profilevm.CustomAvatar = customAvatar; profilevm.DefaultAvatars = Avatar.GetDefaultAvatars(db); profilevm.AllCountries = Country.GetAll(db); if(profilevm.Country != null) profilevm.AllCities = City.GetAll(db, profilevm.Country.Code); return View(profilevm); } }
public ActionResult SavePrivacyChanges(UserProfileViewModel upvm) { if (!User.Identity.IsAuthenticated) return Redirect("/"); var profile = db.UserProfiles.Find(upvm.Id); profile.ShowEmail = upvm.ShowEmail; db.SaveChanges(); //redirect to /profile/index#privacy return Redirect(Url.RouteUrl(new { controller="Profile", action="Index" })+"#Privacy"); }
public ActionResult DeleteAccount(UserProfileViewModel model) { try { if (Membership.ValidateUser(User.Identity.Name, model.Password)) { //delete profile and log out. using (var db = new SDCContext()) using (var t = db.Database.BeginTransaction()) { var profile = db.UserProfiles.First(p => p.UserName == User.Identity.Name); //delete login traces for this account var login_traces = db.LogInTraces.Where(p => p.User.UserId == profile.UserId).ToList(); db.LogInTraces.RemoveRange(login_traces); //delete custom avatar var custom_avatar = db.Avatars.FirstOrDefault(p => p.CustomForUserId == profile.UserId); if (custom_avatar != null) { var relative_avatar_path = VirtualPathUtility.ToAppRelative(custom_avatar.Url); var path = Server.MapPath(relative_avatar_path); System.IO.File.Delete(path); db.Avatars.Remove(custom_avatar); } db.SaveChanges(); t.Commit(); } //delete user profile // I wonder if the transaction has anything to do with it... Membership.DeleteUser(User.Identity.Name, true); WebSecurity.Logout(); } else { model.Message = "Enter your password to delete your account."; //redirect to /profile/index#privacy return Redirect(Url.RouteUrl(new { controller = "Profile", action = "Index" }) + "#DeleteProfile"); } } catch (Exception ex) { //todo: log this shit. } return Redirect("/"); }