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.

AdminController.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Teknik.Areas.Admin.ViewModels;
  7. using Teknik.Areas.Users.Models;
  8. using Teknik.Areas.Users.Utility;
  9. using Teknik.Attributes;
  10. using Teknik.Controllers;
  11. using Teknik.Filters;
  12. using Teknik.Models;
  13. using Teknik.Utilities;
  14. using Teknik.ViewModels;
  15. namespace Teknik.Areas.Admin.Controllers
  16. {
  17. [TeknikAuthorize(Roles = "Admin")]
  18. public class AdminController : DefaultController
  19. {
  20. private TeknikEntities db = new TeknikEntities();
  21. [HttpGet]
  22. [TrackPageView]
  23. public ActionResult Dashboard()
  24. {
  25. DashboardViewModel model = new DashboardViewModel();
  26. return View(model);
  27. }
  28. [HttpGet]
  29. [TrackPageView]
  30. public ActionResult UserSearch()
  31. {
  32. UserSearchViewModel model = new UserSearchViewModel();
  33. return View(model);
  34. }
  35. [HttpGet]
  36. [TrackPageView]
  37. public ActionResult UserInfo(string username)
  38. {
  39. if (UserHelper.UserExists(db, username))
  40. {
  41. User user = UserHelper.GetUser(db, username);
  42. UserInfoViewModel model = new UserInfoViewModel();
  43. model.Username = user.Username;
  44. model.AccountType = user.AccountType;
  45. model.AccountStatus = user.AccountStatus;
  46. return View(model);
  47. }
  48. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  49. }
  50. [HttpGet]
  51. public ActionResult UploadSearch()
  52. {
  53. UploadSearchViewModel model = new UploadSearchViewModel();
  54. return View(model);
  55. }
  56. [HttpPost]
  57. public ActionResult GetUserSearchResults(string query)
  58. {
  59. List<UserResultViewModel> models = new List<UserResultViewModel>();
  60. var results = db.Users.Where(u => u.Username.Contains(query)).ToList();
  61. if (results != null)
  62. {
  63. foreach (User user in results)
  64. {
  65. try
  66. {
  67. UserResultViewModel model = new UserResultViewModel();
  68. model.Username = user.Username;
  69. if (Config.EmailConfig.Enabled)
  70. {
  71. model.Email = string.Format("{0}@{1}", user.Username, Config.EmailConfig.Domain);
  72. }
  73. model.JoinDate = user.JoinDate;
  74. model.LastSeen = UserHelper.GetLastAccountActivity(db, Config, user);
  75. models.Add(model);
  76. }
  77. catch (Exception ex)
  78. {
  79. // Skip this result
  80. }
  81. }
  82. }
  83. return Json(new { result = new { html = PartialView("~/Areas/Admin/Views/Admin/UserResults.cshtml", models).RenderToString() } });
  84. }
  85. [HttpPost]
  86. public ActionResult GetUploadSearchResults(string url)
  87. {
  88. Upload.Models.Upload foundUpload = db.Uploads.Where(u => u.Url == url).FirstOrDefault();
  89. if (foundUpload != null)
  90. {
  91. UploadResultViewModel model = new UploadResultViewModel();
  92. model.Url = foundUpload.Url;
  93. model.ContentType = foundUpload.ContentType;
  94. model.ContentLength = foundUpload.ContentLength;
  95. model.DateUploaded = foundUpload.DateUploaded;
  96. model.Downloads = foundUpload.Downloads;
  97. model.DeleteKey = foundUpload.DeleteKey;
  98. return Json(new { result = new { html = PartialView("~/Areas/Admin/Views/Admin/UploadResult.cshtml", model).RenderToString() } });
  99. }
  100. return Json(new { error = new { message = "Upload does not exist" } });
  101. }
  102. [HttpPost]
  103. [ValidateAntiForgeryToken]
  104. public ActionResult EditUserAccountType(string username, AccountType accountType)
  105. {
  106. if (UserHelper.UserExists(db, username))
  107. {
  108. // Edit the user's account type
  109. UserHelper.EditAccountType(db, Config, username, accountType);
  110. return Json(new { result = new { success = true } });
  111. }
  112. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  113. }
  114. [HttpPost]
  115. [ValidateAntiForgeryToken]
  116. public ActionResult EditUserAccountStatus(string username, AccountStatus accountStatus)
  117. {
  118. if (UserHelper.UserExists(db, username))
  119. {
  120. // Edit the user's account type
  121. UserHelper.EditAccountStatus(db, Config, username, accountStatus);
  122. return Json(new { result = new { success = true } });
  123. }
  124. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  125. }
  126. [HttpPost]
  127. [ValidateAntiForgeryToken]
  128. public ActionResult CreateInviteCode(string username)
  129. {
  130. if (UserHelper.UserExists(db, username))
  131. {
  132. User user = UserHelper.GetUser(db, username);
  133. InviteCode inviteCode = db.InviteCodes.Create();
  134. inviteCode.Active = true;
  135. inviteCode.Code = Guid.NewGuid().ToString();
  136. inviteCode.Owner = user;
  137. db.InviteCodes.Add(inviteCode);
  138. db.SaveChanges();
  139. return Json(new { result = new { code = inviteCode.Code } });
  140. }
  141. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  142. }
  143. [HttpPost]
  144. [ValidateAntiForgeryToken]
  145. public ActionResult DeleteAccount(string username)
  146. {
  147. try
  148. {
  149. using (TeknikEntities db = new TeknikEntities())
  150. {
  151. User user = UserHelper.GetUser(db, username);
  152. if (user != null)
  153. {
  154. UserHelper.DeleteAccount(db, Config, user);
  155. return Json(new { result = true });
  156. }
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. return Json(new { error = ex.GetFullMessage(true) });
  162. }
  163. return Json(new { error = "Unable to delete user" });
  164. }
  165. }
  166. }