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

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