|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Repository\DoctrineMongoODM\Event; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\PermissionsAwareInterface; |
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
|
15
|
|
|
use Doctrine\Common\EventSubscriber; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
17
|
|
|
use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs; |
|
18
|
|
|
use Doctrine\ODM\MongoDB\Events; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
|
20
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Boilerplate for Files permissions update subscriber. |
|
24
|
|
|
* |
|
25
|
|
|
* updates the permissions of embedded files if the permissions on the parent document |
|
26
|
|
|
* had changed. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
29
|
|
|
* @since 0.26 |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractUpdateFilesPermissionsSubscriber implements EventSubscriber |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* List of properties where embedded files can be retreived. |
|
35
|
|
|
* |
|
36
|
|
|
* Note: entries will be prepended with "get" |
|
37
|
|
|
* |
|
38
|
|
|
* @var string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $filesProperties = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The document class that should be watched. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $targetDocument; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function getSubscribedEvents() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ Events::onFlush ]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Updates permissions on embedded files. |
|
57
|
|
|
* |
|
58
|
|
|
* @param OnFlushEventArgs $args |
|
59
|
|
|
*/ |
|
60
|
|
|
public function onFlush(OnFlushEventArgs $args) |
|
61
|
|
|
{ |
|
62
|
|
|
$dm = $args->getDocumentManager(); |
|
63
|
|
|
$uow = $dm->getUnitOfWork(); |
|
64
|
|
|
|
|
65
|
|
|
$filter = function($element) { |
|
66
|
|
|
return $element instanceOf $this->targetDocument |
|
67
|
|
|
&& $element instanceOf PermissionsAwareInterface |
|
68
|
|
|
&& $element->getPermissions()->hasChanged(); |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
|
|
$inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter); |
|
72
|
|
|
$updates = array_filter($uow->getScheduledDocumentUpdates(), $filter); |
|
73
|
|
|
|
|
74
|
|
|
$this->process($inserts); |
|
75
|
|
|
$this->process($updates, $dm, $uow); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* |
|
80
|
|
|
* |
|
81
|
|
|
* @param PermissionsAwareInterface[] $documents |
|
82
|
|
|
* @param null|DocumentManager $dm |
|
83
|
|
|
* @param null|UnitOfWork $uow |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function process($documents, $dm = null, $uow = null) |
|
86
|
|
|
{ |
|
87
|
|
|
foreach ($documents as $document) { |
|
88
|
|
|
$perms = $document->getPermissions(); |
|
89
|
|
|
$files = $this->getFiles($document); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
foreach ($files as $file) { |
|
|
|
|
|
|
92
|
|
|
$file |
|
93
|
|
|
->getPermissions() |
|
94
|
|
|
->clear() |
|
95
|
|
|
->inherit($perms); |
|
96
|
|
|
|
|
97
|
|
|
if ($dm) { |
|
98
|
|
|
$uow->computeChangeSet( |
|
|
|
|
|
|
99
|
|
|
$dm->getClassMetadata(get_class($file)), |
|
100
|
|
|
$file |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* |
|
109
|
|
|
* |
|
110
|
|
|
* @param \Core\Entity\EntityInterface $document |
|
111
|
|
|
* |
|
112
|
|
|
* @return PermissionsAwareInterface[] |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getFiles($document) |
|
115
|
|
|
{ |
|
116
|
|
|
$files = []; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($this->filesProperties as $prop) { |
|
119
|
|
|
$getter = "get$prop"; |
|
120
|
|
|
$file = $document->$getter(); |
|
121
|
|
|
if ($file instanceof Collection) { |
|
122
|
|
|
$files = array_merge($files, ArrayUtils::iteratorToArray($file)); |
|
123
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
$files[] = $file; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return array_filter($files, function($i) { return $i instanceOf PermissionsAwareInterface; }); |
|
130
|
|
|
} |
|
131
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: