private void AddUserData(BlabberDB context) { logger.Info("Preparing the Statement for adding users"); foreach (var user in _veraUsers) { logger.Info("Adding user " + user.UserName); context.Users.Add(user); } context.SaveChanges(); }
public ActionResult PostRegisterFinish(CompleteRegisrationViewModel user, string cpassword) { if (user.Password != cpassword) { logger.Info("Password and Confirm Password do not match"); return(View(new RegisterViewModel { Error = "The Password and Confirm Password values do not match. Please try again.", UserName = user.UserName, RealName = user.RealName, BlabName = user.BlabName, })); } var dbUser = new User { Password = Sha256Hash(user.Password), CreatedAt = DateTime.Now, UserName = user.UserName, BlabName = user.BlabName, RealName = user.RealName, PasswordHint = user.PasswordHint, }; using (var dbContext = new BlabberDB()) { dbContext.Users.Add(dbUser); dbContext.SaveChanges(); } var imageDir = HostingEnvironment.MapPath("~/Images/"); try { System.IO.File.Copy(Path.Combine(imageDir, "default_profile.png"), Path.Combine(imageDir, user.UserName) + ".png"); } catch (Exception ex) { } //EmailUser(userName); return(RedirectToAction("Login", "Account", new LoginView { UserName = user.UserName })); }
public ActionResult PostRegisterFinish([Bind(Include = "UserName,RealName,BlabName")] User user, string cpassword) { if (user.Password != cpassword) { logger.Info("Password and Confirm Password do not match"); return(View(new RegisterViewModel { Error = "The Password and Confirm Password values do not match. Please try again.", UserName = user.UserName, RealName = user.RealName, BlabName = user.BlabName, })); } // Use the user class to get the hashed password. user.Password = Crypto.HashPassword(user.Password); user.CreatedAt = DateTime.Now; using (var dbContext = new BlabberDB()) { dbContext.Users.Add(user); dbContext.SaveChanges(); } var imageDir = HostingEnvironment.MapPath("~/Images/"); try { System.IO.File.Copy(Path.Combine(imageDir, "default_profile.png"), Path.Combine(imageDir, user.UserName) + ".png"); } catch (Exception ex) { } //EmailUser(userName); return(RedirectToAction("Login", "Account", new LoginView { UserName = user.UserName })); }
public ActionResult PostProfile(string realName, string blabName, string userName, HttpPostedFileBase file) { logger.Info("Entering PostProfile"); if (IsUserLoggedIn() == false) { return(RedirectToLogin(HttpContext.Request.RawUrl)); } var oldUsername = GetLoggedInUsername(); var imageDir = HostingEnvironment.MapPath("~/Images/"); string oldImage = null; using (var dbContext = new BlabberDB()) { var user = dbContext.Users.FirstOrDefault(t => t.UserName == oldUsername); if (user == null) { Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(Json(new { message = "User cannot be found." })); } oldImage = Path.Combine(imageDir, user.PictureName); var connection = dbContext.Database.Connection; connection.Open(); var update = connection.CreateCommand(); update.CommandText = "UPDATE users SET real_name=@realname, blab_name=@blabname WHERE username=@username;"; update.Parameters.Add(new SqlParameter { ParameterName = "@realname", Value = realName }); update.Parameters.Add(new SqlParameter { ParameterName = "@blabname", Value = blabName }); update.Parameters.Add(new SqlParameter { ParameterName = "@username", Value = oldUsername }); var result = update.ExecuteNonQuery(); if (result == 0) { Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(Json(new { message = "An error occurred, please try again" })); } } if (userName != oldUsername) { if (UsernameExists(userName)) { Response.StatusCode = (int)HttpStatusCode.Conflict; return(Json(new { message = "That username already exists. Please try another." })); } if (!UpdateUsername(oldUsername, userName)) { Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(Json(new { message = "An error occurred, please try again" })); } Session["username"] = userName; } string newFilename = oldImage; // Update user profile image if (file != null && file.ContentLength > 0) { // Get old image name, if any, to delete if (System.IO.File.Exists(oldImage)) { System.IO.File.Delete(oldImage); } var extension = Path.GetExtension(file.FileName).ToLower(); newFilename = Path.Combine(imageDir, Guid.NewGuid().ToString("N")); newFilename += extension; logger.Info("Saving new profile image: " + newFilename); file.SaveAs(newFilename); using (var dbContext = new BlabberDB()) { var user = dbContext.Users.First(t => t.UserName == userName); user.PictureName = Path.GetFileName(newFilename); dbContext.SaveChanges(); } } Response.StatusCode = (int)HttpStatusCode.OK; var msg = "Successfully changed values!"; var newObject = new { values = new { picturename = Path.GetFileName(newFilename), username = userName.ToLower(), realName = realName, blabName = blabName }, message = msg }; return(Json(newObject)); }