Skip to content

Commit

Permalink
add canInit method and CAN_DEV_GRAPHQL permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Oct 26, 2023
1 parent 4c66b57 commit addf529
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "BSD-3-Clause",
"require": {
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/framework": "^5.2",
"silverstripe/vendor-plugin": "^2",
"webonyx/graphql-php": "^15.0.1",
"silverstripe/event-dispatcher": "^1",
Expand Down
42 changes: 29 additions & 13 deletions src/Dev/DevelopmentAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\DebugView;
use SilverStripe\Dev\DevelopmentAdmin as RootDevelopmentAdmin;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\Security\Security;
use Exception;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\Schema\Logger;

class DevelopmentAdmin extends Controller
class DevelopmentAdmin extends Controller implements PermissionProvider
{
private static $allowed_actions = [
'runRegisteredController'
Expand All @@ -30,21 +32,12 @@ protected function init()
{
parent::init();

if (DevelopmentAdmin::config()->get('deny_non_cli') && !Director::is_cli()) {
if (RootDevelopmentAdmin::config()->get('deny_non_cli') && !Director::is_cli()) {
return $this->httpError(404);
}
// We allow access to this controller regardless of live-status or ADMIN permission only
// if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
$allowAllCLI = DevelopmentAdmin::config()->get('allow_all_cli');
$canAccess = (
Director::isDev()
|| (Director::is_cli() && $allowAllCLI)
// Its important that we don't run this check if dev/build was requested
|| Permission::check("ADMIN")
);
if (!$canAccess) {

if (!$this->canInit()) {
Security::permissionFailure($this);
return;
}

// Define custom logger
Expand Down Expand Up @@ -104,6 +97,29 @@ public function runRegisteredController(HTTPRequest $request)
}
}

public function canInit(): bool
{
return (
Director::isDev()
// We need to ensure that DevelopmentAdminTest can simulate permission failures when running
// "dev/tasks" from CLI.
|| (Director::is_cli() && RootDevelopmentAdmin::config()->get('allow_all_cli'))
|| Permission::check(['ADMIN', 'ALL_DEV_ADMIN', 'CAN_DEV_GRAPHQL'])
);
}

public function providePermissions(): array
{
return [
'CAN_DEV_GRAPHQL' => [
'name' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_DESCRIPTION', 'Can view and execute /dev/graphql'),
'help' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_HELP', 'Can view and execute GraphQL development tools (/dev/graphql).'),
'category' => RootDevelopmentAdmin::permissionsCategory(),
'sort' => 80
],
];
}

/**
* @return array of url => description
*/
Expand Down

0 comments on commit addf529

Please sign in to comment.