|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Acl\Assertion; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\AbstractModel; |
|
8
|
|
|
use Application\Model\Card; |
|
9
|
|
|
use Application\Model\Change; |
|
10
|
|
|
use Application\Model\Collection; |
|
11
|
|
|
use Application\Model\User; |
|
12
|
|
|
use Ecodev\Felix\Acl\Assertion\NamedAssertion; |
|
13
|
|
|
use Ecodev\Felix\Acl\ModelResource; |
|
14
|
|
|
use Laminas\Permissions\Acl\Acl; |
|
15
|
|
|
use Laminas\Permissions\Acl\Resource\ResourceInterface; |
|
16
|
|
|
use Laminas\Permissions\Acl\Role\RoleInterface; |
|
17
|
|
|
|
|
18
|
|
|
class IsOwnerOrResponsible implements NamedAssertion |
|
19
|
|
|
{ |
|
20
|
|
|
public function getName(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return "l'objet m'appartient ou j'en suis responsable"; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Assert that the object belongs to the current user, or belong to a collection that the user is responsible of. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Application\Acl\Acl $acl |
|
29
|
|
|
* @param ModelResource $resource |
|
30
|
|
|
* @param string $privilege |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var AbstractModel $object */ |
|
37
|
8 |
|
$object = $resource->getInstance(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
// Without user no chance to be the owner |
|
40
|
8 |
|
if (!User::getCurrent()) { |
|
41
|
|
|
return $acl->reject('it is not himself'); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
if (User::getCurrent() === $object->getOwner()) { |
|
45
|
6 |
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// If not direct owner, look for indirect collection responsible |
|
49
|
|
|
/** @var Collection[] $collections */ |
|
50
|
3 |
|
$collections = []; |
|
51
|
3 |
|
if ($object instanceof Collection) { |
|
52
|
3 |
|
$collections = [$object]; |
|
53
|
1 |
|
} elseif ($object instanceof Card) { |
|
54
|
1 |
|
$collections = $object->getCollections(); |
|
55
|
|
|
} elseif ($object instanceof Change) { |
|
56
|
|
|
$original = $object->getOriginal(); |
|
57
|
|
|
if ($original) { |
|
58
|
|
|
$collections = $original->getCollections()->toArray(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$suggestion = $object->getSuggestion(); |
|
62
|
|
|
if ($suggestion) { |
|
63
|
|
|
$collections = array_merge($collections, $suggestion->getCollections()->toArray()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
foreach ($collections as $collection) { |
|
68
|
3 |
|
if ($collection->getUsers()->contains(User::getCurrent())) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
return $acl->reject('it is not the owner, nor one of the responsible'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|