The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfileController.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Security;
  8. using Teknik.Areas.Blog.Models;
  9. using Teknik.Areas.Profile.Models;
  10. using Teknik.Areas.Profile.ViewModels;
  11. using Teknik.Controllers;
  12. using Teknik.Helpers;
  13. using Teknik.Models;
  14. using Teknik.ViewModels;
  15. namespace Teknik.Areas.Profile.Controllers
  16. {
  17. public class ProfileController : DefaultController
  18. {
  19. private TeknikEntities db = new TeknikEntities();
  20. // GET: Profile/Profile
  21. [AllowAnonymous]
  22. public ActionResult Index(string username)
  23. {
  24. if (string.IsNullOrEmpty(username))
  25. {
  26. username = User.Identity.Name;
  27. }
  28. ProfileViewModel model = null;
  29. ViewBag.Title = "User Does Not Exist - " + Config.Title;
  30. ViewBag.Message = "The User does not exist";
  31. User user = db.Users.Where(u => u.Username == username).First();
  32. if (user != null)
  33. {
  34. ViewBag.Title = username + "'s Profile - " + Config.Title;
  35. ViewBag.Message = "Viewing " + username + "'s Profile";
  36. model = new ProfileViewModel();
  37. model.UserID = user.UserId;
  38. model.Username = user.Username;
  39. model.Email = string.Format("{0}@{1}", user.Username, Config.Host);
  40. model.JoinDate = user.JoinDate;
  41. model.LastSeen = user.LastSeen;
  42. model.About = user.About;
  43. model.Website = user.Website;
  44. model.Quote = user.Quote;
  45. // fill in Blog details
  46. Blog.Models.Blog blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID).First();
  47. if (blog != null)
  48. {
  49. model.BlogTitle = blog.Title;
  50. model.BlogDescription = blog.Description;
  51. }
  52. }
  53. return View(model);
  54. }
  55. [HttpGet]
  56. [AllowAnonymous]
  57. // GET: Profile
  58. public ActionResult Login(string ReturnUrl)
  59. {
  60. LoginViewModel model = new LoginViewModel();
  61. model.ReturnUrl = ReturnUrl;
  62. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  63. }
  64. [HttpPost]
  65. [AllowAnonymous]
  66. public ActionResult Login(LoginViewModel model)
  67. {
  68. if (ModelState.IsValid)
  69. {
  70. string username = model.Username;
  71. string password = SHA384.Hash(model.Username, model.Password);
  72. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  73. if (userValid)
  74. {
  75. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  76. if (string.IsNullOrEmpty(model.ReturnUrl))
  77. {
  78. return Json(new { result = "true" });
  79. }
  80. else
  81. {
  82. return Redirect(model.ReturnUrl);
  83. }
  84. }
  85. }
  86. return Json(new { error = "Invalid User name or Password." });
  87. }
  88. public ActionResult Logout()
  89. {
  90. FormsAuthentication.SignOut();
  91. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  92. }
  93. [HttpGet]
  94. [AllowAnonymous]
  95. // GET: Profile
  96. public ActionResult Register(string ReturnUrl)
  97. {
  98. RegisterViewModel model = new RegisterViewModel();
  99. model.ReturnUrl = ReturnUrl;
  100. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  101. }
  102. [HttpPost]
  103. [AllowAnonymous]
  104. public ActionResult Register(RegisterViewModel model)
  105. {
  106. if (ModelState.IsValid)
  107. {
  108. var foundUser = db.Users.Where(b => b.Username == model.Username);
  109. if (foundUser.Any())
  110. {
  111. return Json(new { error = "That username already exists." });
  112. }
  113. if (model.Password != model.ConfirmPassword)
  114. {
  115. return Json(new { error = "Passwords must match." });
  116. }
  117. try
  118. {
  119. // Add User
  120. User newUser = db.Users.Create();
  121. newUser.JoinDate = DateTime.Now;
  122. newUser.Username = model.Username;
  123. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  124. db.Users.Add(newUser);
  125. db.SaveChanges();
  126. // Generate blog for the user
  127. var newBlog = db.Blogs.Create();
  128. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  129. db.SaveChanges();
  130. }
  131. catch (Exception ex)
  132. {
  133. return Json(new { error = "Unable to create the user." });
  134. }
  135. return Login(new LoginViewModel { Username = model.Username, Password = model.Password, RememberMe = false, ReturnUrl = model.ReturnUrl });
  136. }
  137. return Json(new { error = "You must include all fields." });
  138. }
  139. [HttpPost]
  140. [ValidateAntiForgeryToken]
  141. public ActionResult Edit(string curPass, string newPass, string newPassConfirm, string website, string quote, string about, string blogTitle, string blogDesc)
  142. {
  143. if (ModelState.IsValid)
  144. {
  145. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  146. if (user != null)
  147. {
  148. Blog.Models.Blog blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID).First();
  149. if (blog != null)
  150. {
  151. // Changing Password?
  152. if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
  153. {
  154. // Old Password Valid?
  155. if (SHA384.Hash(User.Identity.Name, curPass) != user.HashedPassword)
  156. {
  157. return Json(new { error = "Invalid Original Password." });
  158. }
  159. // The New Password Match?
  160. if (newPass != newPassConfirm)
  161. {
  162. return Json(new { error = "New Password Must Match." });
  163. }
  164. user.HashedPassword = SHA384.Hash(User.Identity.Name, newPass);
  165. }
  166. user.Website = website;
  167. user.Quote = quote;
  168. user.About = about;
  169. blog.Title = blogTitle;
  170. blog.Description = blogDesc;
  171. db.Entry(blog).State = EntityState.Modified;
  172. db.Entry(user).State = EntityState.Modified;
  173. db.SaveChanges();
  174. return Json(new { result = true });
  175. }
  176. }
  177. }
  178. return Json(new { error = "Unable to save profile." });
  179. }
  180. [HttpPost]
  181. [ValidateAntiForgeryToken]
  182. public ActionResult Delete(int postID)
  183. {
  184. if (ModelState.IsValid)
  185. {
  186. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  187. if (user != null)
  188. {
  189. db.Users.Remove(user);
  190. db.SaveChanges();
  191. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  192. }
  193. }
  194. return Json(new { error = "Unable to delete user." });
  195. }
  196. }
  197. }