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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Security;
  9. using Teknik.Areas.Blog.Models;
  10. using Teknik.Areas.Error.Controllers;
  11. using Teknik.Areas.Error.ViewModels;
  12. using Teknik.Areas.Profile.Models;
  13. using Teknik.Areas.Profile.ViewModels;
  14. using Teknik.Controllers;
  15. using Teknik.Helpers;
  16. using Teknik.Models;
  17. using Teknik.ViewModels;
  18. using System.Windows;
  19. namespace Teknik.Areas.Profile.Controllers
  20. {
  21. public class ProfileController : DefaultController
  22. {
  23. private TeknikEntities db = new TeknikEntities();
  24. // GET: Profile/Profile
  25. [AllowAnonymous]
  26. public ActionResult Index(string username)
  27. {
  28. if (string.IsNullOrEmpty(username))
  29. {
  30. username = User.Identity.Name;
  31. }
  32. ProfileViewModel model = new ProfileViewModel();
  33. ViewBag.Title = "User Does Not Exist - " + Config.Title;
  34. ViewBag.Message = "The User does not exist";
  35. User user = db.Users.Where(u => u.Username == username).FirstOrDefault();
  36. if (user != null)
  37. {
  38. ViewBag.Title = username + "'s Profile - " + Config.Title;
  39. ViewBag.Message = "Viewing " + username + "'s Profile";
  40. model.UserID = user.UserId;
  41. model.Username = user.Username;
  42. model.Email = string.Format("{0}@{1}", user.Username, Config.Host);
  43. model.JoinDate = user.JoinDate;
  44. model.LastSeen = user.LastSeen;
  45. model.UserSettings = user.UserSettings;
  46. model.BlogSettings = user.BlogSettings;
  47. model.UploadSettings = user.UploadSettings;
  48. model.Uploads = db.Uploads.Where(u => u.UserId == user.UserId).OrderByDescending(u => u.DateUploaded).ToList();
  49. model.Pastes = db.Pastes.Where(u => u.UserId == user.UserId).OrderByDescending(u => u.DatePosted).ToList();
  50. return View(model);
  51. }
  52. model.Error = true;
  53. return View(model);
  54. }
  55. // GET: Profile/Profile
  56. [AllowAnonymous]
  57. public ActionResult Settings()
  58. {
  59. if (User.Identity.IsAuthenticated)
  60. {
  61. string username = User.Identity.Name;
  62. SettingsViewModel model = new SettingsViewModel();
  63. ViewBag.Title = "User Does Not Exist - " + Config.Title;
  64. ViewBag.Message = "The User does not exist";
  65. User user = db.Users.Where(u => u.Username == username).FirstOrDefault();
  66. if (user != null)
  67. {
  68. ViewBag.Title = "Settings - " + Config.Title;
  69. ViewBag.Message = "Your " + Config.Title + " Settings";
  70. model.UserID = user.UserId;
  71. model.Username = user.Username;
  72. model.UserSettings = user.UserSettings;
  73. model.BlogSettings = user.BlogSettings;
  74. model.UploadSettings = user.UploadSettings;
  75. return View(model);
  76. }
  77. model.Error = true;
  78. return View(model);
  79. }
  80. return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  81. }
  82. [HttpGet]
  83. [AllowAnonymous]
  84. // GET: Profile
  85. public ActionResult Login(string ReturnUrl)
  86. {
  87. LoginViewModel model = new LoginViewModel();
  88. model.ReturnUrl = ReturnUrl;
  89. return View("/Areas/Profile/Views/Profile/ViewLogin.cshtml", model);
  90. }
  91. [HttpPost]
  92. [AllowAnonymous]
  93. [ValidateAntiForgeryToken]
  94. public ActionResult Login(LoginViewModel model)
  95. {
  96. if (ModelState.IsValid)
  97. {
  98. string username = model.Username;
  99. string password = SHA384.Hash(model.Username, model.Password);
  100. bool userValid = db.Users.Any(b => b.Username == username && b.HashedPassword == password);
  101. if (userValid)
  102. {
  103. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  104. if (string.IsNullOrEmpty(model.ReturnUrl))
  105. {
  106. return Json(new { result = "true" });
  107. }
  108. else
  109. {
  110. return Redirect(model.ReturnUrl);
  111. }
  112. }
  113. }
  114. return Json(new { error = "Invalid User name or Password." });
  115. }
  116. public ActionResult Logout()
  117. {
  118. FormsAuthentication.SignOut();
  119. return Redirect(Url.SubRouteUrl("www", "Home.Index"));
  120. }
  121. [HttpGet]
  122. [AllowAnonymous]
  123. // GET: Profile
  124. public ActionResult Register(string ReturnUrl)
  125. {
  126. RegisterViewModel model = new RegisterViewModel();
  127. model.ReturnUrl = ReturnUrl;
  128. return View("/Areas/Profile/Views/Profile/ViewRegistration.cshtml", model);
  129. }
  130. [HttpPost]
  131. [AllowAnonymous]
  132. [ValidateAntiForgeryToken]
  133. public ActionResult Register(RegisterViewModel model)
  134. {
  135. if (ModelState.IsValid)
  136. {
  137. var foundUser = db.Users.Where(b => b.Username == model.Username).FirstOrDefault();
  138. if (foundUser != null)
  139. {
  140. return Json(new { error = "That username already exists." });
  141. }
  142. if (model.Password != model.ConfirmPassword)
  143. {
  144. return Json(new { error = "Passwords must match." });
  145. }
  146. try
  147. {
  148. // Connect to hmailserver COM
  149. if (!Config.DevEnvironment)
  150. {
  151. string email = string.Format("{0}@{1}", model.Username, Config.EmailConfig.Domain);
  152. var app = new hMailServer.Application();
  153. app.Connect();
  154. app.Authenticate(Config.EmailConfig.Username, Config.EmailConfig.Password);
  155. var domain = app.Domains.ItemByName[Config.EmailConfig.Domain];
  156. try
  157. {
  158. var account = domain.Accounts.ItemByAddress[email];
  159. return Json(new { error = "That email already exists." });
  160. }
  161. catch { }
  162. // If we got an exception, then the email doesnt exist and we continue on!
  163. var newAccount = domain.Accounts.Add();
  164. newAccount.Address = email;
  165. newAccount.Password = model.Password;
  166. newAccount.Active = true;
  167. newAccount.MaxSize = Config.EmailConfig.MaxSize;
  168. newAccount.Save();
  169. }
  170. // Add User
  171. User newUser = db.Users.Create();
  172. newUser.JoinDate = DateTime.Now;
  173. newUser.Username = model.Username;
  174. newUser.HashedPassword = SHA384.Hash(model.Username, model.Password);
  175. newUser.UserSettings = new UserSettings();
  176. newUser.BlogSettings = new BlogSettings();
  177. newUser.UploadSettings = new UploadSettings();
  178. db.Users.Add(newUser);
  179. db.SaveChanges();
  180. // Generate blog for the user
  181. var newBlog = db.Blogs.Create();
  182. newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
  183. db.Blogs.Add(newBlog);
  184. db.SaveChanges();
  185. }
  186. catch (Exception ex)
  187. {
  188. return Json(new { error = "Unable to create the user." });
  189. }
  190. return Login(new LoginViewModel { Username = model.Username, Password = model.Password, RememberMe = false, ReturnUrl = model.ReturnUrl });
  191. }
  192. return Json(new { error = "You must include all fields." });
  193. }
  194. [HttpPost]
  195. [ValidateAntiForgeryToken]
  196. public ActionResult Edit(string curPass, string newPass, string newPassConfirm, string website, string quote, string about, string blogTitle, string blogDesc, bool saveKey, bool serverSideEncrypt)
  197. {
  198. if (ModelState.IsValid)
  199. {
  200. User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
  201. if (user != null)
  202. {
  203. // Changing Password?
  204. if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
  205. {
  206. // Old Password Valid?
  207. if (SHA384.Hash(User.Identity.Name, curPass) != user.HashedPassword)
  208. {
  209. return Json(new { error = "Invalid Original Password." });
  210. }
  211. // The New Password Match?
  212. if (newPass != newPassConfirm)
  213. {
  214. return Json(new { error = "New Password Must Match." });
  215. }
  216. user.HashedPassword = SHA384.Hash(User.Identity.Name, newPass);
  217. }
  218. // Update Email Pass
  219. var app = new hMailServer.Application();
  220. app.Connect();
  221. app.Authenticate(Config.EmailConfig.Username, Config.EmailConfig.Password);
  222. var domain = app.Domains.ItemByName[Config.EmailConfig.Domain];
  223. var account = domain.Accounts.ItemByAddress[string.Format("{0}@{1}",User.Identity.Name, Config.EmailConfig.Domain)];
  224. account.Password = newPass;
  225. account.Save();
  226. user.UserSettings.Website = website;
  227. user.UserSettings.Quote = quote;
  228. user.UserSettings.About = about;
  229. user.BlogSettings.Title = blogTitle;
  230. user.BlogSettings.Description = blogDesc;
  231. user.UploadSettings.SaveKey = saveKey;
  232. user.UploadSettings.ServerSideEncrypt = serverSideEncrypt;
  233. db.Entry(user).State = EntityState.Modified;
  234. db.SaveChanges();
  235. return Json(new { result = true });
  236. }
  237. return Json(new { error = "User does not exist" });
  238. }
  239. return Json(new { error = "Invalid Parameters" });
  240. }
  241. [HttpPost]
  242. [ValidateAntiForgeryToken]
  243. public ActionResult Delete()
  244. {
  245. if (ModelState.IsValid)
  246. {
  247. // Delete Email
  248. var app = new hMailServer.Application();
  249. app.Connect();
  250. app.Authenticate(Config.EmailConfig.Username, Config.EmailConfig.Password);
  251. var domain = app.Domains.ItemByName[Config.EmailConfig.Domain];
  252. var account = domain.Accounts.ItemByAddress[string.Format("{0}@{1}", User.Identity.Name, Config.EmailConfig.Domain)];
  253. account.Delete();
  254. // Update uploads
  255. List<Upload.Models.Upload> uploads = db.Uploads.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  256. if (uploads != null)
  257. {
  258. foreach (Upload.Models.Upload upload in uploads)
  259. {
  260. upload.UserId = null;
  261. db.Entry(upload).State = EntityState.Modified;
  262. }
  263. }
  264. // Update pastes
  265. List<Paste.Models.Paste> pastes = db.Pastes.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  266. if (pastes != null)
  267. {
  268. foreach (Paste.Models.Paste paste in pastes)
  269. {
  270. paste.UserId = null;
  271. db.Entry(paste).State = EntityState.Modified;
  272. }
  273. }
  274. // Delete Blogs
  275. Blog.Models.Blog blog = db.Blogs.Include("BlogPosts").Include("BlogPosts.Comments").Include("User").Where(u => u.User.Username == User.Identity.Name).FirstOrDefault();
  276. if (blog != null)
  277. {
  278. db.Blogs.Remove(blog);
  279. }
  280. // Delete post comments
  281. List<BlogPostComment> postComments = db.BlogComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  282. if (postComments != null)
  283. {
  284. foreach (BlogPostComment postComment in postComments)
  285. {
  286. db.BlogComments.Remove(postComment);
  287. }
  288. }
  289. // Delete post comments
  290. List<Podcast.Models.PodcastComment> podComments = db.PodcastComments.Include("User").Where(u => u.User.Username == User.Identity.Name).ToList();
  291. if (podComments != null)
  292. {
  293. foreach (Podcast.Models.PodcastComment podComment in podComments)
  294. {
  295. db.PodcastComments.Remove(podComment);
  296. }
  297. }
  298. // Delete User
  299. User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  300. if (user != null)
  301. {
  302. user.UserSettings = db.UserSettings.Find(user.UserId);
  303. user.BlogSettings = db.BlogSettings.Find(user.UserId);
  304. user.UploadSettings = db.UploadSettings.Find(user.UserId);
  305. db.Users.Remove(user);
  306. db.SaveChanges();
  307. FormsAuthentication.SignOut();
  308. return Json(new { result = true });
  309. }
  310. }
  311. return Json(new { error = "Unable to delete user" });
  312. }
  313. }
  314. }