Skip to content

Commit

Permalink
Flip canView logic and combine into taskEnabled check
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Nov 9, 2023
1 parent b9b891d commit c77a77d
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/Dev/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public function runTask($request)
$inst = Injector::inst()->create($task['class']);
$title(sprintf('Running Task %s', $inst->getTitle()));

if (!$inst->isEnabled()) {
$message('The task is disabled');
if (!$this->taskEnabled($task['class'])) {
$message('The task is disabled or you do not have sufficient permission to run it');
return;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ protected function getTaskList(): array
{
$taskClasses = ClassInfo::subclassesFor(BuildTask::class, false);
foreach ($taskClasses as $index => $task) {
if (!$this->taskEnabled($task) || !$this->canViewTask($task)) {
if (!$this->taskEnabled($task)) {
unset($taskClasses[$index]);
}
}
Expand All @@ -179,30 +179,18 @@ protected function taskEnabled($class)
$reflectionClass = new ReflectionClass($class);
if ($reflectionClass->isAbstract()) {
return false;
} elseif (!singleton($class)->isEnabled()) {
return false;
}

return true;
}

protected function canViewTask(string $class): bool
{
if ($this->canViewAllTasks()) {
return true;
}

$reflectionClass = new ReflectionClass($class);
if ($reflectionClass->isAbstract()) {
$task = Injector::inst()->get($class);
if (!$task->isEnabled()) {
return false;
}

$task = Injector::inst()->get($class);
if (!$task->hasMethod('canView') || !$task->canView()) {
if ($task->hasMethod('canView') && !$task->canView()) {
return false;
}

return true;
return $this->canViewAllTasks();
}

protected function canViewAllTasks(): bool
Expand Down Expand Up @@ -254,7 +242,7 @@ public function providePermissions(): array
return [
'BUILDTASK_CAN_RUN' => [
'name' => _t(__CLASS__ . '.BUILDTASK_CAN_RUN_DESCRIPTION', 'Can view and execute all /dev/tasks'),
'help' => _t(__CLASS__ . '.BUILDTASK_CAN_RUN_HELP', 'Can view and execute all Build Tasks (/dev/tasks). This supersedes individual task permissions'),
'help' => _t(__CLASS__ . '.BUILDTASK_CAN_RUN_HELP', 'Can view and execute all Build Tasks (/dev/tasks). This may still be overriden by individual task view permissions'),
'category' => DevelopmentAdmin::permissionsCategory(),
'sort' => 70
],
Expand Down

0 comments on commit c77a77d

Please sign in to comment.