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

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