Guest User

Untitled

a guest
Jun 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Register(DataModels.RegisterViewModel model, HttpPostedFileBase files)
  4. {
  5. if (!ModelState.IsValid) return View(model);
  6.  
  7. string path = null;
  8. if (files != null && files.ContentLength > 0)
  9. {
  10. string filename = model.Login + ".jpg";
  11. path = Path.Combine(Server.MapPath("/App_Data/temp/Users"), filename);
  12. Random rnd = new Random(113);
  13. if (System.IO.File.Exists(path)) path = path + rnd.Next(99999);
  14. files.SaveAs(path);
  15. model.Photo = System.IO.File.ReadAllBytes(path);
  16. }
  17.  
  18. try { if (path != null) System.IO.File.Delete(path); }
  19. catch (IOException){ /* ignored*/ }
  20.  
  21. var user = CacheManager.CreateUser(model);
  22. if (user == null)
  23. {
  24. ModelState.AddModelError("", Resources.UserAlreadyExist);
  25. return View(model);
  26. }
  27.  
  28. if (!SendVerificationEmail(user)) ModelState.AddModelError("", Resources.EmailSendingErr);
  29. return RedirectToAction("VerifyEmail", new {user.UserID});
  30. }
  31. public ActionResult VerifyEmail(int userid)
  32. {
  33. var user = CacheManager.GetUser(userid);
  34. ViewData.Model = user;
  35. return View();
  36. }
  37. [HttpPost]
  38. public ActionResult VerifyEmail(BlogUser user)
  39. {
  40. if (!ModelState.IsValid) View(user);
  41. if (user != null)
  42. {
  43. if (!SendVerificationEmail(user)) ModelState.AddModelError("", Resources.EmailSendingErr);
  44. else return RedirectToAction("VerifyEmail", new {user.UserID});
  45. }
  46. else ModelState.AddModelError("", Resources.UserNotExist);
  47. return View();
  48. }
  49.  
  50. [AllowAnonymous]
  51. public ActionResult ConfirmEmail(int token, string email)
  52. {
  53. var user = CacheManager.GetUser(Convert.ToInt32(token));
  54. if (user == null) return RedirectToAction("Register");
  55. if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
  56.  
  57. user.Verified = 1;
  58. CacheManager.SaveUser(user, false);
  59. return View("Login");
  60. }
  61.  
  62. public static BlogUser SaveUser(BlogUser user, bool hashPass)
  63. {
  64. return Rep.SaveUser(new BlogUser
  65. {
  66. Name = user.Name,
  67. Surname = user.Surname,
  68. Patronymic = user.Patronymic,
  69. UserLogin = user.UserLogin,
  70. Password = hashPass ? Hash(user.Password) : user.Password,
  71. Email = user.Email,
  72. UserPhoto = user.UserPhoto,
  73. UserID = user.UserID,
  74. RegistrationDate = user.RegistrationDate,
  75. UserRight = user.UserRight,
  76. Blocked = user.Blocked,
  77. Warnings = user.Warnings,
  78. Verified = user.Verified
  79. });
  80. }
  81.  
  82. public BlogUser SaveUser(BlogUser user)
  83. {
  84. if (user.UserID == 0) _db.BlogUser.Add(user);
  85. else
  86. {
  87. _db.BlogUser.Attach(_db.BlogUser.Single(u => u.UserID == user.UserID));
  88. ((IObjectContextAdapter) _db).ObjectContext.ApplyCurrentValues("BlogUser", user);
  89. }
  90. _db.SaveChanges();
  91. CacheManager.PurgeCacheItems("users");
  92. return GetUser(user.UserID);
  93. }
  94.  
  95. [AllowAnonymous]
  96. public ActionResult ConfirmEmail(int token, string email)
  97. {
  98. var user = CacheManager.GetUser(Convert.ToInt32(token));
  99. if (user == null) return RedirectToAction("Register");
  100. if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
  101.  
  102. CacheManager.SaveUser(new BlogUser
  103. {
  104. Blocked = 0,
  105. Email = user.Email,
  106. Password = user.Password,
  107. Verified = 1,
  108. UserID = user.UserID,
  109. Name = user.Name,
  110. Surname = user.Surname,
  111. Patronymic = user.Patronymic,
  112. UserRight = user.UserRight,
  113. Warnings = 0,
  114. UserLogin = user.UserLogin,
  115. UserPhoto = user.UserPhoto,
  116. RegistrationDate = user.RegistrationDate
  117. }, false);
  118. return View("Login");
  119. }
Add Comment
Please, Sign In to add comment