Skip to content

Commit

Permalink
API Make use of the new AdminController (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Oct 20, 2024
1 parent a40106d commit 20112b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
7 changes: 0 additions & 7 deletions _config/routes.yml

This file was deleted.

2 changes: 1 addition & 1 deletion client/dist/js/BrokenExternalLinksReport.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/src/js/BrokenExternalLinksReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 16 additions & 24 deletions src/Controllers/CMSExternalLinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,54 @@

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'
];

/**
* 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
Expand All @@ -71,5 +62,6 @@ public function start()
$task = CheckExternalLinksTask::create();
$task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_HTML));
}
return $this->jsonSuccess(200, []);
}
}

0 comments on commit 20112b9

Please sign in to comment.