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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. public ActionResult Dashboard()
  29. {
  30. DashboardViewModel model = new DashboardViewModel();
  31. return View(model);
  32. }
  33. [HttpGet]
  34. public ActionResult UserSearch()
  35. {
  36. UserSearchViewModel model = new UserSearchViewModel();
  37. return View(model);
  38. }
  39. [HttpGet]
  40. public ActionResult UserInfo(string username)
  41. {
  42. if (UserHelper.UserExists(_dbContext, username))
  43. {
  44. User user = UserHelper.GetUser(_dbContext, username);
  45. UserInfoViewModel model = new UserInfoViewModel();
  46. model.Username = user.Username;
  47. model.AccountType = user.AccountType;
  48. model.AccountStatus = user.AccountStatus;
  49. return View(model);
  50. }
  51. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  52. }
  53. [HttpGet]
  54. public ActionResult UploadSearch()
  55. {
  56. UploadSearchViewModel model = new UploadSearchViewModel();
  57. return View(model);
  58. }
  59. [HttpPost]
  60. public async Task<ActionResult> GetUserSearchResults(string query, [FromServices] ICompositeViewEngine viewEngine)
  61. {
  62. List<UserResultViewModel> models = new List<UserResultViewModel>();
  63. var results = _dbContext.Users.Where(u => u.Username.Contains(query)).ToList();
  64. if (results != null)
  65. {
  66. foreach (User user in results)
  67. {
  68. try
  69. {
  70. UserResultViewModel model = new UserResultViewModel();
  71. model.Username = user.Username;
  72. if (_config.EmailConfig.Enabled)
  73. {
  74. model.Email = string.Format("{0}@{1}", user.Username, _config.EmailConfig.Domain);
  75. }
  76. model.JoinDate = user.JoinDate;
  77. model.LastSeen = UserHelper.GetLastAccountActivity(_dbContext, _config, user);
  78. models.Add(model);
  79. }
  80. catch (Exception)
  81. {
  82. // Skip this result
  83. }
  84. }
  85. }
  86. string renderedView = await RenderPartialViewToString(viewEngine, "~/Areas/Admin/Views/Admin/UserResults.cshtml", models);
  87. return Json(new { result = new { html = renderedView } });
  88. }
  89. [HttpPost]
  90. public async Task<ActionResult> GetUploadSearchResults(string url, [FromServices] ICompositeViewEngine viewEngine)
  91. {
  92. Upload.Models.Upload foundUpload = _dbContext.Uploads.Where(u => u.Url == url).FirstOrDefault();
  93. if (foundUpload != null)
  94. {
  95. UploadResultViewModel model = new UploadResultViewModel();
  96. model.Url = foundUpload.Url;
  97. model.ContentType = foundUpload.ContentType;
  98. model.ContentLength = foundUpload.ContentLength;
  99. model.DateUploaded = foundUpload.DateUploaded;
  100. model.Downloads = foundUpload.Downloads;
  101. model.DeleteKey = foundUpload.DeleteKey;
  102. string renderedView = await RenderPartialViewToString(viewEngine, "~/Areas/Admin/Views/Admin/UploadResult.cshtml", model);
  103. return Json(new { result = new { html = renderedView } });
  104. }
  105. return Json(new { error = new { message = "Upload does not exist" } });
  106. }
  107. [HttpPost]
  108. [ValidateAntiForgeryToken]
  109. public ActionResult EditUserAccountType(string username, AccountType accountType)
  110. {
  111. if (UserHelper.UserExists(_dbContext, username))
  112. {
  113. // Edit the user's account type
  114. UserHelper.EditAccountType(_dbContext, _config, username, accountType);
  115. return Json(new { result = new { success = true } });
  116. }
  117. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  118. }
  119. [HttpPost]
  120. [ValidateAntiForgeryToken]
  121. public ActionResult EditUserAccountStatus(string username, AccountStatus accountStatus)
  122. {
  123. if (UserHelper.UserExists(_dbContext, username))
  124. {
  125. // Edit the user's account type
  126. UserHelper.EditAccountStatus(_dbContext, _config, username, accountStatus);
  127. return Json(new { result = new { success = true } });
  128. }
  129. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  130. }
  131. [HttpPost]
  132. [ValidateAntiForgeryToken]
  133. public ActionResult CreateInviteCode(string username)
  134. {
  135. if (UserHelper.UserExists(_dbContext, username))
  136. {
  137. User user = UserHelper.GetUser(_dbContext, username);
  138. InviteCode inviteCode = new InviteCode();
  139. inviteCode.Active = true;
  140. inviteCode.Code = Guid.NewGuid().ToString();
  141. inviteCode.Owner = user;
  142. _dbContext.InviteCodes.Add(inviteCode);
  143. _dbContext.SaveChanges();
  144. return Json(new { result = new { code = inviteCode.Code } });
  145. }
  146. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  147. }
  148. [HttpPost]
  149. [ValidateAntiForgeryToken]
  150. public ActionResult DeleteAccount(string username)
  151. {
  152. try
  153. {
  154. User user = UserHelper.GetUser(_dbContext, username);
  155. if (user != null)
  156. {
  157. UserHelper.DeleteAccount(_dbContext, _config, user);
  158. return Json(new { result = true });
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. return Json(new { error = ex.GetFullMessage(true) });
  164. }
  165. return Json(new { error = "Unable to delete user" });
  166. }
  167. }
  168. }