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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. User user = db.Users.Where(u => u.Username == username).FirstOrDefault();
  34. if (user != null)
  35. {
  36. ViewBag.Title = username + "'s Profile - " + Config.Title;
  37. ViewBag.Message = "Viewing " + username + "'s Profile";
  38. model.UserID = user.UserId;
  39. model.Username = user.Username;
  40. model.Email = string.Format("{0}@{1}", user.Username, Config.Host);
  41. model.JoinDate = user.JoinDate;
  42. model.LastSeen = user.LastSeen;
  43. model.UserSettings = user.UserSettings;
  44. model.BlogSettings = user.BlogSettings;
  45. model.UploadSettings = user.UploadSettings;
  46. return View(model);
  47. }
  48. model.Error = true;
  49. return View(model);
  50. }
  51. [HttpGet]
  52. [AllowAnonymous]
  53. // GET: Profile
  54. public ActionResult Login(string ReturnUrl)
  55. {
  56. LoginViewModel model = new LoginViewModel();
  57. model.ReturnUrl = ReturnUrl;
  58. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  59. }
  60. [HttpPost]
  61. [AllowAnonymous]
  62. [ValidateAntiForgeryToken]
  63. public ActionResult Login(LoginViewModel model)
  64. {
  65. if (ModelState.IsValid)
  66. {
  67. string username = model.Username;
  68. string password = SHA384.Hash(model.Username, model.Password);
  69. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  70. if (userValid)
  71. {
  72. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  73. if (string.IsNullOrEmpty(model.ReturnUrl))
  74. {
  75. return Json(new { result = "true" });
  76. }
  77. else
  78. {
  79. return Redirect(model.ReturnUrl);
  80. }
  81. }
  82. }
  83. return Json(new { error = "Invalid User name or Password." });
  84. }
  85. public ActionResult Logout()
  86. {
  87. FormsAuthentication.SignOut();
  88. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  89. }
  90. [HttpGet]
  91. [AllowAnonymous]
  92. // GET: Profile
  93. public ActionResult Register(string ReturnUrl)
  94. {
  95. RegisterViewModel model = new RegisterViewModel();
  96. model.ReturnUrl = ReturnUrl;
  97. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  98. }
  99. [HttpPost]
  100. [AllowAnonymous]
  101. [ValidateAntiForgeryToken]
  102. public ActionResult Register(RegisterViewModel model)
  103. {
  104. if (ModelState.IsValid)
  105. {
  106. var foundUser = db.Users.Where(b => b.Username == model.Username).FirstOrDefault();
  107. if (foundUser != null)
  108. {
  109. return Json(new { error = "That username already exists." });
  110. }
  111. if (model.Password != model.ConfirmPassword)
  112. {
  113. return Json(new { error = "Passwords must match." });
  114. }
  115. try
  116. {
  117. // Add User
  118. User newUser = db.Users.Create();
  119. newUser.JoinDate = DateTime.Now;
  120. newUser.Username = model.Username;
  121. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  122. newUser.UserSettings = new UserSettings();
  123. newUser.BlogSettings = new BlogSettings();
  124. newUser.UploadSettings = new UploadSettings();
  125. db.Users.Add(newUser);
  126. db.SaveChanges();
  127. // Generate blog for the user
  128. var newBlog = db.Blogs.Create();
  129. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  130. db.Blogs.Add(newBlog);
  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, bool saveKey, bool serverSideEncrypt)
  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. // Changing Password?
  151. if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
  152. {
  153. // Old Password Valid?
  154. if (SHA384.Hash(User.Identity.Name, curPass) != user.HashedPassword)
  155. {
  156. return Json(new { error = "Invalid Original Password." });
  157. }
  158. // The New Password Match?
  159. if (newPass != newPassConfirm)
  160. {
  161. return Json(new { error = "New Password Must Match." });
  162. }
  163. user.HashedPassword = SHA384.Hash(User.Identity.Name, newPass);
  164. }
  165. user.UserSettings.Website = website;
  166. user.UserSettings.Quote = quote;
  167. user.UserSettings.About = about;
  168. user.BlogSettings.Title = blogTitle;
  169. user.BlogSettings.Description = blogDesc;
  170. user.UploadSettings.SaveKey = saveKey;
  171. user.UploadSettings.ServerSideEncrypt = serverSideEncrypt;
  172. db.Entry(user).State = EntityState.Modified;
  173. db.SaveChanges();
  174. return Json(new { result = true });
  175. }
  176. return Json(new { error = "User does not exist" });
  177. }
  178. return Json(new { error = "Invalid Parameters" });
  179. }
  180. [HttpPost]
  181. [ValidateAntiForgeryToken]
  182. public ActionResult Delete()
  183. {
  184. if (ModelState.IsValid)
  185. {
  186. // Delete Blogs
  187. Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault();
  188. if (blog != null)
  189. {
  190. db.Blogs.Remove(blog);
  191. db.SaveChanges();
  192. }
  193. User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  194. if (user != null)
  195. {
  196. user.UserSettings = db.UserSettings.Find(user.UserId);
  197. user.BlogSettings = db.BlogSettings.Find(user.UserId);
  198. user.UploadSettings = db.UploadSettings.Find(user.UserId);
  199. db.Users.Remove(user);
  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. }