/
ObjectTaskHandler.php
106 lines (91 loc) · 3.57 KB
/
ObjectTaskHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Concrete\Core\Permission\Category;
use Concrete\Core\Controller\Controller;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Permission\Access\Access;
use Concrete\Core\Permission\Access\Entity\Entity;
use Concrete\Core\Permission\Duration;
use Concrete\Core\Permission\Key\Key;
use Concrete\Core\Permission\ObjectInterface;
use Symfony\Component\HttpFoundation\Response;
use Concrete\Core\Workflow\Workflow;
defined('C5_EXECUTE') or die('Access Denied.');
/**
* Abstrct class for task handles that use a permission object.
*/
abstract class ObjectTaskHandler extends Controller implements TaskHandlerInterface
{
/**
* Should we save workflows when saving permissions?
*
* @var bool
*/
protected $hasWorkflows = false;
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Permission\Category\TaskHandlerInterface::handle()
*/
public function handle(string $task, array $options): ?Response
{
$permissionObject = $this->getPermissionObject($options);
$method = lcfirst(camelcase($task));
if (!method_exists($this, $method)) {
throw new UserMessageException(t('Unknown permission task: %s', $task));
}
return $this->{$method}($permissionObject, $options);
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
abstract protected function getPermissionObject(array $options): ObjectInterface;
protected function addAccessEntity(ObjectInterface $permissionObject, array $options): ?Response
{
$pk = Key::getByID($options['pkID']);
$pk->setPermissionObject($permissionObject);
$pa = Access::getByID($options['paID'], $pk);
$pe = Entity::getByID($options['peID']);
$pd = empty($options['pdID']) ? null : Duration::getByID($options['pdID']);
$pa->addListItem($pe, $pd, $options['accessType']);
return $this->app->make(ResponseFactoryInterface::class)->json(true);
}
protected function removeAccessEntity(ObjectInterface $permissionObject, array $options): ?Response
{
$pk = Key::getByID($options['pkID']);
$pk->setPermissionObject($permissionObject);
$pa = Access::getByID($options['paID'], $pk);
$pe = Entity::getByID($options['peID']);
$pa->removeListItem($pe);
return $this->app->make(ResponseFactoryInterface::class)->json(true);
}
protected function savePermission(ObjectInterface $permissionObject, array $options): ?Response
{
$pk = Key::getByID($options['pkID']);
$pk->setPermissionObject($permissionObject);
$pa = Access::getByID($options['paID'], $pk);
$pa->save($options);
if ($this->hasWorkflows) {
$pa->clearWorkflows();
$wfIDs = $options['wfID'] ?? null;
if (is_array($wfIDs)) {
foreach ($wfIDs as $wfID) {
$wf = Workflow::getByID($wfID);
if ($wf !== null) {
$pa->attachWorkflow($wf);
}
}
}
}
return $this->app->make(ResponseFactoryInterface::class)->json(true);
}
protected function displayAccessCell(ObjectInterface $permissionObject, array $options): ?Response
{
$pk = Key::getByID($options['pkID']);
$pk->setPermissionObject($permissionObject);
$this->set('pk', $pk);
$this->set('pa', Access::getByID($options['paID'], $pk));
$this->setViewPath('/backend/permissions/labels');
return null;
}
}