From 20112b9aec451e2446af44ac373266c97e9d6a85 Mon Sep 17 00:00:00 2001
From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com>
Date: Mon, 21 Oct 2024 11:54:20 +1300
Subject: [PATCH] API Make use of the new AdminController (#151)
---
_config/routes.yml | 7 ----
client/dist/js/BrokenExternalLinksReport.js | 2 +-
client/src/js/BrokenExternalLinksReport.js | 2 +-
.../CMSExternalLinksController.php | 40 ++++++++-----------
4 files changed, 18 insertions(+), 33 deletions(-)
delete mode 100644 _config/routes.yml
diff --git a/_config/routes.yml b/_config/routes.yml
deleted file mode 100644
index 2ac22a9..0000000
--- a/_config/routes.yml
+++ /dev/null
@@ -1,7 +0,0 @@
----
-Name: externallinkroutes
-Before: '#adminroutes'
----
-SilverStripe\Control\Director:
- rules:
- 'admin/externallinks//$Action': SilverStripe\ExternalLinks\Controllers\CMSExternalLinksController
diff --git a/client/dist/js/BrokenExternalLinksReport.js b/client/dist/js/BrokenExternalLinksReport.js
index 8629c9b..8fc25d5 100644
--- a/client/dist/js/BrokenExternalLinksReport.js
+++ b/client/dist/js/BrokenExternalLinksReport.js
@@ -1 +1 @@
-!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var n=function n(o){var r=e[o];if(void 0!==r)return r.exports;var s=e[o]={exports:{}};return t[o](s,s.exports,n),s.exports}(669);n.entwine("ss",(t=>{t(".external-links-report__create-report").entwine({PollTimeout:null,ButtonIsLoading:!1,ReloadContent:!1,onclick(t){t.preventDefault(),this.buttonLoading(),this.start()},onmatch(){this.poll()},start(){const e=this;t(".external-links-report__report-progress").empty().text("Running report 0%"),t.ajax({url:"admin/externallinks/start",async:!0,timeout:3e3,success(){e.setReloadContent(!0),e.poll()},error(){e.buttonReset()}})},getButton(){return t(".external-links-report__create-report")},buttonLoading(){if(this.getButtonIsLoading())return;this.setButtonIsLoading(!0);const e=this.getButton();e.addClass("btn--loading loading"),e.attr("disabled",!0),e.is("button")&&(e.append(t('
')),e.css(`${e.outerWidth()}px`))},buttonReset(){this.setButtonIsLoading(!1);const t=this.getButton();t.removeClass("btn--loading loading"),t.attr("disabled",!1),t.find(".btn__loading-icon").remove(),t.css("width","auto")},poll(){const e=this;this.buttonLoading(),t.ajax({url:"admin/externallinks/getJobStatus",async:!0,success(n){if(!n)return void e.buttonReset();const o=n.Completed?n.Completed:0,r=n.Total?n.Total:0;if("Completed"===n.Status)return e.getReloadContent()&&(t(".cms-container").loadPanel(document.location.href,null,{},!0,!1),e.setReloadContent(!1)),t(".external-links-report__report-progress").text(`Report finished ${o}/${r}`),void e.buttonReset();if(o{t(".external-links-report__create-report").poll()}),1e3))},error(){e.buttonReset()}})}})}))}();
\ No newline at end of file
+!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var n=function n(o){var r=e[o];if(void 0!==r)return r.exports;var s=e[o]={exports:{}};return t[o](s,s.exports,n),s.exports}(669);n.entwine("ss",(t=>{t(".external-links-report__create-report").entwine({PollTimeout:null,ButtonIsLoading:!1,ReloadContent:!1,onclick(t){t.preventDefault(),this.buttonLoading(),this.start()},onmatch(){this.poll()},start(){const e=this;t(".external-links-report__report-progress").empty().text("Running report 0%"),t.ajax({url:"admin/externallinks/start",async:!0,timeout:3e3,success(){e.setReloadContent(!0),e.poll()},error(){e.buttonReset()}})},getButton(){return t(".external-links-report__create-report")},buttonLoading(){if(this.getButtonIsLoading())return;this.setButtonIsLoading(!0);const e=this.getButton();e.addClass("btn--loading loading"),e.attr("disabled",!0),e.is("button")&&(e.append(t('
')),e.css(`${e.outerWidth()}px`))},buttonReset(){this.setButtonIsLoading(!1);const t=this.getButton();t.removeClass("btn--loading loading"),t.attr("disabled",!1),t.find(".btn__loading-icon").remove(),t.css("width","auto")},poll(){const e=this;this.buttonLoading(),t.ajax({url:"admin/externallinks/getJobStatus",async:!0,success(n){if(!n||"object"==typeof n&&n.length<1)return void e.buttonReset();const o=n.Completed?n.Completed:0,r=n.Total?n.Total:0;if("Completed"===n.Status)return e.getReloadContent()&&(t(".cms-container").loadPanel(document.location.href,null,{},!0,!1),e.setReloadContent(!1)),t(".external-links-report__report-progress").text(`Report finished ${o}/${r}`),void e.buttonReset();if(o{t(".external-links-report__create-report").poll()}),1e3))},error(){e.buttonReset()}})}})}))}();
\ No newline at end of file
diff --git a/client/src/js/BrokenExternalLinksReport.js b/client/src/js/BrokenExternalLinksReport.js
index a3f3225..0f25afc 100644
--- a/client/src/js/BrokenExternalLinksReport.js
+++ b/client/src/js/BrokenExternalLinksReport.js
@@ -99,7 +99,7 @@
async: true,
success(data) {
// No report, so let user create one
- if (!data) {
+ if (!data || (typeof data === 'object' && data.length < 1)) {
self.buttonReset();
return;
}
diff --git a/src/Controllers/CMSExternalLinksController.php b/src/Controllers/CMSExternalLinksController.php
index 9977614..8739016 100644
--- a/src/Controllers/CMSExternalLinksController.php
+++ b/src/Controllers/CMSExternalLinksController.php
@@ -2,17 +2,22 @@
namespace SilverStripe\ExternalLinks\Controllers;
+use SilverStripe\Admin\AdminController;
+use SilverStripe\Control\HTTPResponse;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
-use SilverStripe\Control\Controller;
use Symbiote\QueuedJobs\Services\QueuedJobService;
-use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\PolyExecution\PolyOutput;
-use SilverStripe\Security\Permission;
-class CMSExternalLinksController extends Controller
+class CMSExternalLinksController extends AdminController
{
+ private static ?string $url_segment = 'externallinks';
+
+ private static string|array $required_permission_codes = [
+ 'CMS_ACCESS_CMSMain',
+ ];
+
private static $allowed_actions = [
'getJobStatus',
'start'
@@ -20,45 +25,31 @@ class CMSExternalLinksController extends Controller
/**
* Respond to Ajax requests for info on a running job
- *
- * @return string JSON string detailing status of the job
*/
- public function getJobStatus()
+ public function getJobStatus(): HTTPResponse
{
- if (!Permission::check('CMS_ACCESS_CMSMain')) {
- return $this->httpError(403, 'You do not have permission to access this resource');
- }
- // Set headers
- HTTPCacheControlMiddleware::singleton()->setMaxAge(0);
- $this->response
- ->addHeader('Content-Type', 'application/json')
- ->addHeader('Content-Encoding', 'UTF-8')
- ->addHeader('X-Content-Type-Options', 'nosniff');
-
- // Format status
+ $this->getResponse()->addHeader('X-Content-Type-Options', 'nosniff');
$track = BrokenExternalPageTrackStatus::get_latest();
if ($track) {
- return json_encode([
+ return $this->jsonSuccess(200, [
'TrackID' => $track->ID,
'Status' => $track->Status,
'Completed' => $track->getCompletedPages(),
'Total' => $track->getTotalPages()
]);
}
+ return $this->jsonSuccess(200, []);
}
/**
* Starts a broken external link check
*/
- public function start()
+ public function start(): HTTPResponse
{
- if (!Permission::check('CMS_ACCESS_CMSMain')) {
- return $this->httpError(403, 'You do not have permission to access this resource');
- }
// return if the a job is already running
$status = BrokenExternalPageTrackStatus::get_latest();
if ($status && $status->Status == 'Running') {
- return;
+ return $this->jsonSuccess(200, []);
}
// Create a new job
@@ -71,5 +62,6 @@ public function start()
$task = CheckExternalLinksTask::create();
$task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_HTML));
}
+ return $this->jsonSuccess(200, []);
}
}