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.5KB

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