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

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