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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. model.Uploads = db.Uploads.Where(u => u.UserId == user.UserId).OrderByDescending(u => u.DateUploaded).ToList();
  47. model.Pastes = db.Pastes.Where(u => u.UserId == user.UserId).OrderByDescending(u => u.DatePosted).ToList();
  48. return View(model);
  49. }
  50. model.Error = true;
  51. return View(model);
  52. }
  53. // GET: Profile/Profile
  54. [AllowAnonymous]
  55. public ActionResult Settings()
  56. {
  57. if (User.Identity.IsAuthenticated)
  58. {
  59. string username = User.Identity.Name;
  60. SettingsViewModel model = new SettingsViewModel();
  61. ViewBag.Title = "User Does Not Exist - " + Config.Title;
  62. ViewBag.Message = "The User does not exist";
  63. User user = db.Users.Where(u => u.Username == username).FirstOrDefault();
  64. if (user != null)
  65. {
  66. ViewBag.Title = "Settings - " + Config.Title;
  67. ViewBag.Message = "Your " + Config.Title + " Settings";
  68. model.UserID = user.UserId;
  69. model.Username = user.Username;
  70. model.UserSettings = user.UserSettings;
  71. model.BlogSettings = user.BlogSettings;
  72. model.UploadSettings = user.UploadSettings;
  73. return View(model);
  74. }
  75. model.Error = true;
  76. return View(model);
  77. }
  78. return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  79. }
  80. [HttpGet]
  81. [AllowAnonymous]
  82. // GET: Profile
  83. public ActionResult Login(string ReturnUrl)
  84. {
  85. LoginViewModel model = new LoginViewModel();
  86. model.ReturnUrl = ReturnUrl;
  87. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  88. }
  89. [HttpPost]
  90. [AllowAnonymous]
  91. [ValidateAntiForgeryToken]
  92. public ActionResult Login(LoginViewModel model)
  93. {
  94. if (ModelState.IsValid)
  95. {
  96. string username = model.Username;
  97. string password = SHA384.Hash(model.Username, model.Password);
  98. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  99. if (userValid)
  100. {
  101. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  102. if (string.IsNullOrEmpty(model.ReturnUrl))
  103. {
  104. return Json(new { result = "true" });
  105. }
  106. else
  107. {
  108. return Redirect(model.ReturnUrl);
  109. }
  110. }
  111. }
  112. return Json(new { error = "Invalid User name or Password." });
  113. }
  114. public ActionResult Logout()
  115. {
  116. FormsAuthentication.SignOut();
  117. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  118. }
  119. [HttpGet]
  120. [AllowAnonymous]
  121. // GET: Profile
  122. public ActionResult Register(string ReturnUrl)
  123. {
  124. RegisterViewModel model = new RegisterViewModel();
  125. model.ReturnUrl = ReturnUrl;
  126. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  127. }
  128. [HttpPost]
  129. [AllowAnonymous]
  130. [ValidateAntiForgeryToken]
  131. public ActionResult Register(RegisterViewModel model)
  132. {
  133. if (ModelState.IsValid)
  134. {
  135. var foundUser = db.Users.Where(b => b.Username == model.Username).FirstOrDefault();
  136. if (foundUser != null)
  137. {
  138. return Json(new { error = "That username already exists." });
  139. }
  140. if (model.Password != model.ConfirmPassword)
  141. {
  142. return Json(new { error = "Passwords must match." });
  143. }
  144. try
  145. {
  146. // Add User
  147. User newUser = db.Users.Create();
  148. newUser.JoinDate = DateTime.Now;
  149. newUser.Username = model.Username;
  150. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  151. newUser.UserSettings = new UserSettings();
  152. newUser.BlogSettings = new BlogSettings();
  153. newUser.UploadSettings = new UploadSettings();
  154. db.Users.Add(newUser);
  155. db.SaveChanges();
  156. // Generate blog for the user
  157. var newBlog = db.Blogs.Create();
  158. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  159. db.Blogs.Add(newBlog);
  160. db.SaveChanges();
  161. }
  162. catch (Exception ex)
  163. {
  164. return Json(new { error = "Unable to create the user." });
  165. }
  166. return Login(new LoginViewModel { Username = model.Username, Password = model.Password, RememberMe = false, ReturnUrl = model.ReturnUrl });
  167. }
  168. return Json(new { error = "You must include all fields." });
  169. }
  170. [HttpPost]
  171. [ValidateAntiForgeryToken]
  172. public ActionResult Edit(string curPass, string newPass, string newPassConfirm, string website, string quote, string about, string blogTitle, string blogDesc, bool saveKey, bool serverSideEncrypt)
  173. {
  174. if (ModelState.IsValid)
  175. {
  176. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  177. if (user != null)
  178. {
  179. // Changing Password?
  180. if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
  181. {
  182. // Old Password Valid?
  183. if (SHA384.Hash(User.Identity.Name, curPass) != user.HashedPassword)
  184. {
  185. return Json(new { error = "Invalid Original Password." });
  186. }
  187. // The New Password Match?
  188. if (newPass != newPassConfirm)
  189. {
  190. return Json(new { error = "New Password Must Match." });
  191. }
  192. user.HashedPassword = SHA384.Hash(User.Identity.Name, newPass);
  193. }
  194. user.UserSettings.Website = website;
  195. user.UserSettings.Quote = quote;
  196. user.UserSettings.About = about;
  197. user.BlogSettings.Title = blogTitle;
  198. user.BlogSettings.Description = blogDesc;
  199. user.UploadSettings.SaveKey = saveKey;
  200. user.UploadSettings.ServerSideEncrypt = serverSideEncrypt;
  201. db.Entry(user).State = EntityState.Modified;
  202. db.SaveChanges();
  203. return Json(new { result = true });
  204. }
  205. return Json(new { error = "User does not exist" });
  206. }
  207. return Json(new { error = "Invalid Parameters" });
  208. }
  209. [HttpPost]
  210. [ValidateAntiForgeryToken]
  211. public ActionResult Delete()
  212. {
  213. if (ModelState.IsValid)
  214. {
  215. // Update uploads
  216. List<Upload.Models.Upload> uploads = db.Uploads.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  217. if (uploads != null)
  218. {
  219. foreach (Upload.Models.Upload upload in uploads)
  220. {
  221. upload.UserId = null;
  222. db.Entry(upload).State = EntityState.Modified;
  223. }
  224. }
  225. // Update pastes
  226. List<Paste.Models.Paste> pastes = db.Pastes.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  227. if (pastes != null)
  228. {
  229. foreach (Paste.Models.Paste paste in pastes)
  230. {
  231. paste.UserId = null;
  232. db.Entry(paste).State = EntityState.Modified;
  233. }
  234. }
  235. // Delete Blogs
  236. Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("BlogPosts.Comments").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault();
  237. if (blog != null)
  238. {
  239. db.Blogs.Remove(blog);
  240. }
  241. // Delete post comments
  242. List<BlogPostComment> postComments = db.BlogComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  243. if (postComments != null)
  244. {
  245. foreach (BlogPostComment postComment in postComments)
  246. {
  247. db.BlogComments.Remove(postComment);
  248. }
  249. }
  250. // Delete post comments
  251. List<Podcast.Models.PodcastComment> podComments = db.PodcastComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  252. if (podComments != null)
  253. {
  254. foreach (Podcast.Models.PodcastComment podComment in podComments)
  255. {
  256. db.PodcastComments.Remove(podComment);
  257. }
  258. }
  259. // Delete User
  260. User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  261. if (user != null)
  262. {
  263. user.UserSettings = db.UserSettings.Find(user.UserId);
  264. user.BlogSettings = db.BlogSettings.Find(user.UserId);
  265. user.UploadSettings = db.UploadSettings.Find(user.UserId);
  266. db.Users.Remove(user);
  267. db.SaveChanges();
  268. FormsAuthentication.SignOut();
  269. return Json(new { result = true });
  270. }
  271. }
  272. return Json(new { error = "Unable to delete user" });
  273. }
  274. }
  275. }