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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. [ValidateAntiForgeryToken]
  67. public ActionResult Login(LoginViewModel model)
  68. {
  69. if (ModelState.IsValid)
  70. {
  71. string username = model.Username;
  72. string password = SHA384.Hash(model.Username, model.Password);
  73. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  74. if (userValid)
  75. {
  76. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  77. if (string.IsNullOrEmpty(model.ReturnUrl))
  78. {
  79. return Json(new { result = "true" });
  80. }
  81. else
  82. {
  83. return Redirect(model.ReturnUrl);
  84. }
  85. }
  86. }
  87. return Json(new { error = "Invalid User name or Password." });
  88. }
  89. public ActionResult Logout()
  90. {
  91. FormsAuthentication.SignOut();
  92. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  93. }
  94. [HttpGet]
  95. [AllowAnonymous]
  96. // GET: Profile
  97. public ActionResult Register(string ReturnUrl)
  98. {
  99. RegisterViewModel model = new RegisterViewModel();
  100. model.ReturnUrl = ReturnUrl;
  101. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  102. }
  103. [HttpPost]
  104. [AllowAnonymous]
  105. [ValidateAntiForgeryToken]
  106. public ActionResult Register(RegisterViewModel model)
  107. {
  108. if (ModelState.IsValid)
  109. {
  110. var foundUser = db.Users.Where(b => b.Username == model.Username);
  111. if (foundUser.Any())
  112. {
  113. return Json(new { error = "That username already exists." });
  114. }
  115. if (model.Password != model.ConfirmPassword)
  116. {
  117. return Json(new { error = "Passwords must match." });
  118. }
  119. try
  120. {
  121. // Add User
  122. User newUser = db.Users.Create();
  123. newUser.JoinDate = DateTime.Now;
  124. newUser.Username = model.Username;
  125. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  126. db.Users.Add(newUser);
  127. db.SaveChanges();
  128. // Generate blog for the user
  129. var newBlog = db.Blogs.Create();
  130. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  131. db.SaveChanges();
  132. }
  133. catch (Exception ex)
  134. {
  135. return Json(new { error = "Unable to create the user." });
  136. }
  137. return Login(new LoginViewModel { Username = model.Username, Password = model.Password, RememberMe = false, ReturnUrl = model.ReturnUrl });
  138. }
  139. return Json(new { error = "You must include all fields." });
  140. }
  141. [HttpPost]
  142. [ValidateAntiForgeryToken]
  143. public ActionResult Edit(string curPass, string newPass, string newPassConfirm, string website, string quote, string about, string blogTitle, string blogDesc)
  144. {
  145. if (ModelState.IsValid)
  146. {
  147. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  148. if (user != null)
  149. {
  150. Blog.Models.Blog blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID).First();
  151. if (blog != null)
  152. {
  153. // Changing Password?
  154. if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
  155. {
  156. // Old Password Valid?
  157. if (SHA384.Hash(User.Identity.Name, curPass) != user.HashedPassword)
  158. {
  159. return Json(new { error = "Invalid Original Password." });
  160. }
  161. // The New Password Match?
  162. if (newPass != newPassConfirm)
  163. {
  164. return Json(new { error = "New Password Must Match." });
  165. }
  166. user.HashedPassword = SHA384.Hash(User.Identity.Name, newPass);
  167. }
  168. user.Website = website;
  169. user.Quote = quote;
  170. user.About = about;
  171. blog.Title = blogTitle;
  172. blog.Description = blogDesc;
  173. db.Entry(blog).State = EntityState.Modified;
  174. db.Entry(user).State = EntityState.Modified;
  175. db.SaveChanges();
  176. return Json(new { result = true });
  177. }
  178. }
  179. }
  180. return Json(new { error = "Unable to save profile." });
  181. }
  182. [HttpPost]
  183. [ValidateAntiForgeryToken]
  184. public ActionResult Delete()
  185. {
  186. if (ModelState.IsValid)
  187. {
  188. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  189. if (user != null)
  190. {
  191. db.Users.Remove(user);
  192. db.SaveChanges();
  193. return Logout();
  194. }
  195. }
  196. return Json(new { error = "Unable to delete user." });
  197. }
  198. }
  199. }