From 09a33714ab6b5c8ea7420bbf6567a9e7ed1150af Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Tue, 23 Jun 2020 00:18:58 -0700 Subject: [PATCH] Added paste and shortened URL search to admin page --- .../Admin/Controllers/AdminController.cs | 58 +++++++++++++++++ .../Admin/ViewModels/PasteResultViewModel.cs | 18 ++++++ .../Admin/ViewModels/PasteSearchViewModel.cs | 12 ++++ .../ViewModels/ShortenedUrlResultViewModel.cs | 17 +++++ .../ViewModels/ShortenedUrlSearchViewModel.cs | 12 ++++ .../Admin/Views/Admin/PasteResult.cshtml | 33 ++++++++++ .../Admin/Views/Admin/PasteSearch.cshtml | 27 ++++++++ .../Views/Admin/ShortenedUrlResult.cshtml | 33 ++++++++++ .../Views/Admin/ShortenedUrlSearch.cshtml | 27 ++++++++ .../Paste/Controllers/PasteController.cs | 5 +- .../Controllers/ShortenerController.cs | 5 +- Teknik/Scripts/Admin/PasteSearch.js | 63 +++++++++++++++++++ Teknik/Scripts/Admin/ShortenedUrlSearch.js | 63 +++++++++++++++++++ Teknik/bundleconfig.json | 18 ++++-- 14 files changed, 381 insertions(+), 10 deletions(-) create mode 100644 Teknik/Areas/Admin/ViewModels/PasteResultViewModel.cs create mode 100644 Teknik/Areas/Admin/ViewModels/PasteSearchViewModel.cs create mode 100644 Teknik/Areas/Admin/ViewModels/ShortenedUrlResultViewModel.cs create mode 100644 Teknik/Areas/Admin/ViewModels/ShortenedUrlSearchViewModel.cs create mode 100644 Teknik/Areas/Admin/Views/Admin/PasteResult.cshtml create mode 100644 Teknik/Areas/Admin/Views/Admin/PasteSearch.cshtml create mode 100644 Teknik/Areas/Admin/Views/Admin/ShortenedUrlResult.cshtml create mode 100644 Teknik/Areas/Admin/Views/Admin/ShortenedUrlSearch.cshtml create mode 100644 Teknik/Scripts/Admin/PasteSearch.js create mode 100644 Teknik/Scripts/Admin/ShortenedUrlSearch.js diff --git a/Teknik/Areas/Admin/Controllers/AdminController.cs b/Teknik/Areas/Admin/Controllers/AdminController.cs index a9e64ff..9318f38 100644 --- a/Teknik/Areas/Admin/Controllers/AdminController.cs +++ b/Teknik/Areas/Admin/Controllers/AdminController.cs @@ -73,6 +73,22 @@ namespace Teknik.Areas.Admin.Controllers return View(model); } + [HttpGet] + [TrackPageView] + public IActionResult PasteSearch() + { + PasteSearchViewModel model = new PasteSearchViewModel(); + return View(model); + } + + [HttpGet] + [TrackPageView] + public IActionResult ShoretenedUrlSearch() + { + UploadSearchViewModel model = new UploadSearchViewModel(); + return View(model); + } + [HttpPost] public async Task GetUserSearchResults(string query, [FromServices] ICompositeViewEngine viewEngine) { @@ -133,6 +149,48 @@ namespace Teknik.Areas.Admin.Controllers return Json(new { error = new { message = "Upload does not exist" } }); } + [HttpPost] + public async Task GetPasteSearchResults(string url, [FromServices] ICompositeViewEngine viewEngine) + { + Paste.Models.Paste foundPaste = _dbContext.Pastes.Where(u => u.Url == url).FirstOrDefault(); + if (foundPaste != null) + { + PasteResultViewModel model = new PasteResultViewModel(); + + model.Url = foundPaste.Url; + model.DatePosted = foundPaste.DatePosted; + model.Views = foundPaste.Views; + model.DeleteKey = foundPaste.DeleteKey; + model.Username = foundPaste.User?.Username; + + string renderedView = await RenderPartialViewToString(viewEngine, "~/Areas/Admin/Views/Admin/PasteResult.cshtml", model); + + return Json(new { result = new { html = renderedView } }); + } + return Json(new { error = new { message = "Paste does not exist" } }); + } + + [HttpPost] + public async Task GetShortenedUrlSearchResults(string url, [FromServices] ICompositeViewEngine viewEngine) + { + Shortener.Models.ShortenedUrl foundUrl = _dbContext.ShortenedUrls.Where(u => u.ShortUrl == url).FirstOrDefault(); + if (foundUrl != null) + { + ShortenedUrlResultViewModel model = new ShortenedUrlResultViewModel(); + + model.OriginalUrl = foundUrl.OriginalUrl; + model.ShortenedUrl = foundUrl.ShortUrl; + model.CreationDate = foundUrl.DateAdded; + model.Views = foundUrl.Views; + model.Username = foundUrl.User?.Username; + + string renderedView = await RenderPartialViewToString(viewEngine, "~/Areas/Admin/Views/Admin/ShortenedUrlResult.cshtml", model); + + return Json(new { result = new { html = renderedView } }); + } + return Json(new { error = new { message = "Shortened Url does not exist" } }); + } + [HttpPost] [ValidateAntiForgeryToken] public async Task EditUserAccountType(string username, AccountType accountType) diff --git a/Teknik/Areas/Admin/ViewModels/PasteResultViewModel.cs b/Teknik/Areas/Admin/ViewModels/PasteResultViewModel.cs new file mode 100644 index 0000000..c0a9ea5 --- /dev/null +++ b/Teknik/Areas/Admin/ViewModels/PasteResultViewModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Teknik.ViewModels; + +namespace Teknik.Areas.Admin.ViewModels +{ + public class PasteResultViewModel : ViewModelBase + { + public string Url { get; set; } + public string Title { get; set; } + public DateTime DatePosted { get; set; } + public int Views { get; set; } + public string DeleteKey { get; set; } + public string Username { get; set; } + } +} diff --git a/Teknik/Areas/Admin/ViewModels/PasteSearchViewModel.cs b/Teknik/Areas/Admin/ViewModels/PasteSearchViewModel.cs new file mode 100644 index 0000000..6b82557 --- /dev/null +++ b/Teknik/Areas/Admin/ViewModels/PasteSearchViewModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Teknik.ViewModels; + +namespace Teknik.Areas.Admin.ViewModels +{ + public class PasteSearchViewModel : ViewModelBase + { + } +} \ No newline at end of file diff --git a/Teknik/Areas/Admin/ViewModels/ShortenedUrlResultViewModel.cs b/Teknik/Areas/Admin/ViewModels/ShortenedUrlResultViewModel.cs new file mode 100644 index 0000000..6ef5f15 --- /dev/null +++ b/Teknik/Areas/Admin/ViewModels/ShortenedUrlResultViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Teknik.ViewModels; + +namespace Teknik.Areas.Admin.ViewModels +{ + public class ShortenedUrlResultViewModel : ViewModelBase + { + public string OriginalUrl { get; set; } + public string ShortenedUrl { get; set; } + public DateTime CreationDate { get; set; } + public int Views { get; set; } + public string Username { get; set; } + } +} diff --git a/Teknik/Areas/Admin/ViewModels/ShortenedUrlSearchViewModel.cs b/Teknik/Areas/Admin/ViewModels/ShortenedUrlSearchViewModel.cs new file mode 100644 index 0000000..1ed57b9 --- /dev/null +++ b/Teknik/Areas/Admin/ViewModels/ShortenedUrlSearchViewModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Teknik.ViewModels; + +namespace Teknik.Areas.Admin.ViewModels +{ + public class ShortenedUrlSearchViewModel : ViewModelBase + { + } +} \ No newline at end of file diff --git a/Teknik/Areas/Admin/Views/Admin/PasteResult.cshtml b/Teknik/Areas/Admin/Views/Admin/PasteResult.cshtml new file mode 100644 index 0000000..bb9a065 --- /dev/null +++ b/Teknik/Areas/Admin/Views/Admin/PasteResult.cshtml @@ -0,0 +1,33 @@ +@model Teknik.Areas.Admin.ViewModels.PasteResultViewModel + +
+ +
+
+ +

+ @if (!string.IsNullOrEmpty(Model.Username)) + { + @:@Model.Username + } +

+
+
+ +

@Model.Title

+
+
+ +

+
+
+ +

@Model.Views

+
+
+

+
+
+
diff --git a/Teknik/Areas/Admin/Views/Admin/PasteSearch.cshtml b/Teknik/Areas/Admin/Views/Admin/PasteSearch.cshtml new file mode 100644 index 0000000..acf83fb --- /dev/null +++ b/Teknik/Areas/Admin/Views/Admin/PasteSearch.cshtml @@ -0,0 +1,27 @@ +@model Teknik.Areas.Admin.ViewModels.PasteSearchViewModel + + + +
+
+
+ +
+ +
+ +
+
+
+
+
+
+
+
+ + diff --git a/Teknik/Areas/Admin/Views/Admin/ShortenedUrlResult.cshtml b/Teknik/Areas/Admin/Views/Admin/ShortenedUrlResult.cshtml new file mode 100644 index 0000000..78191bf --- /dev/null +++ b/Teknik/Areas/Admin/Views/Admin/ShortenedUrlResult.cshtml @@ -0,0 +1,33 @@ +@model Teknik.Areas.Admin.ViewModels.ShortenedUrlResultViewModel + +
+ +
+
+ +

+ @if (!string.IsNullOrEmpty(Model.Username)) + { + @:@Model.Username + } +

+
+
+ +

@Model.OriginalUrl

+
+
+ +

+
+
+ +

@Model.Views

+
+
+

+
+
+
diff --git a/Teknik/Areas/Admin/Views/Admin/ShortenedUrlSearch.cshtml b/Teknik/Areas/Admin/Views/Admin/ShortenedUrlSearch.cshtml new file mode 100644 index 0000000..47b7855 --- /dev/null +++ b/Teknik/Areas/Admin/Views/Admin/ShortenedUrlSearch.cshtml @@ -0,0 +1,27 @@ +@model Teknik.Areas.Admin.ViewModels.ShortenedUrlSearchViewModel + + + +
+
+
+ +
+ +
+ +
+
+
+
+
+
+
+
+ + diff --git a/Teknik/Areas/Paste/Controllers/PasteController.cs b/Teknik/Areas/Paste/Controllers/PasteController.cs index e0c1429..5079a02 100644 --- a/Teknik/Areas/Paste/Controllers/PasteController.cs +++ b/Teknik/Areas/Paste/Controllers/PasteController.cs @@ -381,13 +381,14 @@ namespace Teknik.Areas.Paste.Controllers Models.Paste foundPaste = _dbContext.Pastes.Where(p => p.Url == id).FirstOrDefault(); if (foundPaste != null) { - if (foundPaste.User.Username == User.Identity.Name) + if (foundPaste.User.Username == User.Identity.Name || + User.IsInRole("Admin")) { DeleteFile(foundPaste); return Json(new { result = true, redirect = Url.SubRouteUrl("p", "Paste.Index") }); } - return Json(new { error = new { message = "You do not have permission to edit this Paste" } }); + return Json(new { error = new { message = "You do not have permission to delete this Paste" } }); } return Json(new { error = new { message = "This Paste does not exist" } }); } diff --git a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs index 5795c50..3029799 100644 --- a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs +++ b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs @@ -85,14 +85,15 @@ namespace Teknik.Areas.Shortener.Controllers ShortenedUrl shortenedUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == id).FirstOrDefault(); if (shortenedUrl != null) { - if (shortenedUrl.User.Username == User.Identity.Name) + if (shortenedUrl.User.Username == User.Identity.Name || + User.IsInRole("Admin")) { _dbContext.ShortenedUrls.Remove(shortenedUrl); _dbContext.SaveChanges(); return Json(new { result = true }); } - return Json(new { error = new { message = "You do not have permission to edit this Shortened URL" } }); + return Json(new { error = new { message = "You do not have permission to delete this Shortened URL" } }); } return Json(new { error = new { message = "This Shortened URL does not exist" } }); } diff --git a/Teknik/Scripts/Admin/PasteSearch.js b/Teknik/Scripts/Admin/PasteSearch.js new file mode 100644 index 0000000..3dd2a2a --- /dev/null +++ b/Teknik/Scripts/Admin/PasteSearch.js @@ -0,0 +1,63 @@ +/* globals searchResultsURL, deletePasteURL, homeUrl */ +$(document).ready(function () { + $('#Query').on('input', function () { + var query = $(this).val(); + + // Try to strip out the ID from the url + var pattern = '(?:(?:.+)\\/)?([^\\?]+)(?:\\?(?:.*))?'; + var reg = new RegExp(pattern); + var match = reg.exec(query); + query = match[1]; + + $.ajax({ + type: "POST", + url: searchResultsURL, + data: { url: query }, + success: function (response) { + if (response.result) { + $("#top_msg").css('display', 'none'); + $("#top_msg").html(''); + $("#results").html(response.result.html); + LinkPasteDelete($('.delete-paste-button')); + } + else { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response) + '
'); + } + } + }); + }); +}); + +function LinkPasteDelete(selector) { + $(selector).click(function () { + var id = $(this).data('paste-id'); + + deleteConfirm("Are you sure you want to delete this paste?", function (result) { + if (result) { + $.ajax({ + type: "POST", + url: deletePasteURL, + data: { id: id }, + headers: { 'X-Requested-With': 'XMLHttpRequest' }, + xhrFields: { + withCredentials: true + }, + success: function (response) { + if (response.result) { + window.location.replace(homeUrl); + } + else { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response) + '
'); + } + }, + error: function (response) { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response.responseText) + '
'); + } + }); + } + }); + }); +} diff --git a/Teknik/Scripts/Admin/ShortenedUrlSearch.js b/Teknik/Scripts/Admin/ShortenedUrlSearch.js new file mode 100644 index 0000000..1f8273b --- /dev/null +++ b/Teknik/Scripts/Admin/ShortenedUrlSearch.js @@ -0,0 +1,63 @@ +/* globals searchResultsURL, deleteShortenedURL, homeUrl */ +$(document).ready(function () { + $('#Query').on('input', function () { + var query = $(this).val(); + + // Try to strip out the ID from the url + var pattern = '(?:(?:.+)\\/)?([^\\?]+)(?:\\?(?:.*))?'; + var reg = new RegExp(pattern); + var match = reg.exec(query); + query = match[1]; + + $.ajax({ + type: "POST", + url: searchResultsURL, + data: { url: query }, + success: function (response) { + if (response.result) { + $("#top_msg").css('display', 'none'); + $("#top_msg").html(''); + $("#results").html(response.result.html); + LinkShortUrlDelete($('.delete-short-url-button')); + } + else { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response) + '
'); + } + } + }); + }); +}); + +function LinkShortUrlDelete(selector) { + $(selector).click(function () { + var id = $(this).data('short-id'); + + deleteConfirm("Are you sure you want to delete this shortened url?", function (result) { + if (result) { + $.ajax({ + type: "POST", + url: deleteShortenedURL, + data: { id: id }, + headers: { 'X-Requested-With': 'XMLHttpRequest' }, + xhrFields: { + withCredentials: true + }, + success: function (response) { + if (response.result) { + window.location.replace(homeUrl); + } + else { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response) + '
'); + } + }, + error: function (response) { + $("#top_msg").css('display', 'inline', 'important'); + $("#top_msg").html('
' + parseErrorMessage(response.responseText) + '
'); + } + }); + } + }); + }); +} diff --git a/Teknik/bundleconfig.json b/Teknik/bundleconfig.json index 209913f..a27abbc 100644 --- a/Teknik/bundleconfig.json +++ b/Teknik/bundleconfig.json @@ -11,6 +11,18 @@ "./wwwroot/js/App/Admin/UploadSearch.js" ] }, + { + "outputFileName": "./wwwroot/js/pasteSearch.min.js", + "inputFiles": [ + "./wwwroot/js/App/Admin/PasteSearch.js" + ] + }, + { + "outputFileName": "./wwwroot/js/shortenedUrlSearch.min.js", + "inputFiles": [ + "./wwwroot/js/App/Admin/ShortenedUrlSearch.js" + ] + }, { "outputFileName": "./wwwroot/js/userInfo.min.js", "inputFiles": [ @@ -221,12 +233,6 @@ "./wwwroot/js/app/User/ResetPass.js" ] }, - { - "outputFileName": "./wwwroot/js/user.settings.min.js", - "inputFiles": [ - "./wwwroot/js/app/User/Settings.js" - ] - }, { "outputFileName": "./wwwroot/js/user.settings.blog.min.js", "inputFiles": [