Flip canView logic and combine into taskEnabled check

This commit is contained in:
Andrew Paxley 2023-11-09 20:22:59 +13:00
parent b9b891d050
commit c77a77d584

View File

@ -118,8 +118,8 @@ class TaskRunner extends Controller implements PermissionProvider
$inst = Injector::inst()->create($task['class']); $inst = Injector::inst()->create($task['class']);
$title(sprintf('Running Task %s', $inst->getTitle())); $title(sprintf('Running Task %s', $inst->getTitle()));
if (!$inst->isEnabled()) { if (!$this->taskEnabled($task['class'])) {
$message('The task is disabled'); $message('The task is disabled or you do not have sufficient permission to run it');
return; return;
} }
@ -162,7 +162,7 @@ class TaskRunner extends Controller implements PermissionProvider
{ {
$taskClasses = ClassInfo::subclassesFor(BuildTask::class, false); $taskClasses = ClassInfo::subclassesFor(BuildTask::class, false);
foreach ($taskClasses as $index => $task) { foreach ($taskClasses as $index => $task) {
if (!$this->taskEnabled($task) || !$this->canViewTask($task)) { if (!$this->taskEnabled($task)) {
unset($taskClasses[$index]); unset($taskClasses[$index]);
} }
} }
@ -176,33 +176,21 @@ class TaskRunner extends Controller implements PermissionProvider
*/ */
protected function taskEnabled($class) 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); $reflectionClass = new ReflectionClass($class);
if ($reflectionClass->isAbstract()) { if ($reflectionClass->isAbstract()) {
return false; return false;
} }
$task = Injector::inst()->get($class); $task = Injector::inst()->get($class);
if (!$task->hasMethod('canView') || !$task->canView()) { if (!$task->isEnabled()) {
return false; return false;
} }
return true; if ($task->hasMethod('canView') && !$task->canView()) {
return false;
}
return $this->canViewAllTasks();
} }
protected function canViewAllTasks(): bool protected function canViewAllTasks(): bool
@ -254,7 +242,7 @@ class TaskRunner extends Controller implements PermissionProvider
return [ return [
'BUILDTASK_CAN_RUN' => [ 'BUILDTASK_CAN_RUN' => [
'name' => _t(__CLASS__ . '.BUILDTASK_CAN_RUN_DESCRIPTION', 'Can view and execute all /dev/tasks'), '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(), 'category' => DevelopmentAdmin::permissionsCategory(),
'sort' => 70 'sort' => 70
], ],