Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Register(DataModels.RegisterViewModel model, HttpPostedFileBase files)
- {
- if (!ModelState.IsValid) return View(model);
- string path = null;
- if (files != null && files.ContentLength > 0)
- {
- string filename = model.Login + ".jpg";
- path = Path.Combine(Server.MapPath("/App_Data/temp/Users"), filename);
- Random rnd = new Random(113);
- if (System.IO.File.Exists(path)) path = path + rnd.Next(99999);
- files.SaveAs(path);
- model.Photo = System.IO.File.ReadAllBytes(path);
- }
- try { if (path != null) System.IO.File.Delete(path); }
- catch (IOException){ /* ignored*/ }
- var user = CacheManager.CreateUser(model);
- if (user == null)
- {
- ModelState.AddModelError("", Resources.UserAlreadyExist);
- return View(model);
- }
- if (!SendVerificationEmail(user)) ModelState.AddModelError("", Resources.EmailSendingErr);
- return RedirectToAction("VerifyEmail", new {user.UserID});
- }
- public ActionResult VerifyEmail(int userid)
- {
- var user = CacheManager.GetUser(userid);
- ViewData.Model = user;
- return View();
- }
- [HttpPost]
- public ActionResult VerifyEmail(BlogUser user)
- {
- if (!ModelState.IsValid) View(user);
- if (user != null)
- {
- if (!SendVerificationEmail(user)) ModelState.AddModelError("", Resources.EmailSendingErr);
- else return RedirectToAction("VerifyEmail", new {user.UserID});
- }
- else ModelState.AddModelError("", Resources.UserNotExist);
- return View();
- }
- [AllowAnonymous]
- public ActionResult ConfirmEmail(int token, string email)
- {
- var user = CacheManager.GetUser(Convert.ToInt32(token));
- if (user == null) return RedirectToAction("Register");
- if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
- user.Verified = 1;
- CacheManager.SaveUser(user, false);
- return View("Login");
- }
- public static BlogUser SaveUser(BlogUser user, bool hashPass)
- {
- return Rep.SaveUser(new BlogUser
- {
- Name = user.Name,
- Surname = user.Surname,
- Patronymic = user.Patronymic,
- UserLogin = user.UserLogin,
- Password = hashPass ? Hash(user.Password) : user.Password,
- Email = user.Email,
- UserPhoto = user.UserPhoto,
- UserID = user.UserID,
- RegistrationDate = user.RegistrationDate,
- UserRight = user.UserRight,
- Blocked = user.Blocked,
- Warnings = user.Warnings,
- Verified = user.Verified
- });
- }
- public BlogUser SaveUser(BlogUser user)
- {
- if (user.UserID == 0) _db.BlogUser.Add(user);
- else
- {
- _db.BlogUser.Attach(_db.BlogUser.Single(u => u.UserID == user.UserID));
- ((IObjectContextAdapter) _db).ObjectContext.ApplyCurrentValues("BlogUser", user);
- }
- _db.SaveChanges();
- CacheManager.PurgeCacheItems("users");
- return GetUser(user.UserID);
- }
- [AllowAnonymous]
- public ActionResult ConfirmEmail(int token, string email)
- {
- var user = CacheManager.GetUser(Convert.ToInt32(token));
- if (user == null) return RedirectToAction("Register");
- if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
- CacheManager.SaveUser(new BlogUser
- {
- Blocked = 0,
- Email = user.Email,
- Password = user.Password,
- Verified = 1,
- UserID = user.UserID,
- Name = user.Name,
- Surname = user.Surname,
- Patronymic = user.Patronymic,
- UserRight = user.UserRight,
- Warnings = 0,
- UserLogin = user.UserLogin,
- UserPhoto = user.UserPhoto,
- RegistrationDate = user.RegistrationDate
- }, false);
- return View("Login");
- }
Add Comment
Please, Sign In to add comment