Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceUpdateHandler
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 catchResourceUpdated
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
5
 getRolePrivilegeRetriever
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissionService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2021 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDacSimple\model\eventHandler;
24
25use Laminas\ServiceManager\ServiceLocatorAwareTrait;
26use oat\generis\model\OntologyAwareTrait;
27use oat\oatbox\service\ConfigurableService;
28use oat\tao\model\event\ResourceMovedEvent;
29use oat\taoDacSimple\model\ChangePermissionsService;
30use oat\taoDacSimple\model\Command\ChangePermissionsCommand;
31use oat\taoDacSimple\model\RolePrivilegeRetriever;
32
33class ResourceUpdateHandler extends ConfigurableService
34{
35    use ServiceLocatorAwareTrait;
36    use OntologyAwareTrait;
37
38    public function catchResourceUpdated(ResourceMovedEvent $event): void
39    {
40        $permissionService = $this->getPermissionService();
41        $rolePrivilegeRetriever = $this->getRolePrivilegeRetriever();
42        $movedResource = $event->getMovedResource();
43
44        $rolePrivilegeList = $rolePrivilegeRetriever->retrieveByResourceIds([
45            $event->getDestinationClass()->getUri(),
46            $movedResource->getUri()
47        ]);
48
49        if ($movedResource->isClass()) {
50            foreach ($movedResource->getInstances(true) as $item) {
51                $itemUri = $item->getUri();
52                $itemPrivilegesMap[$itemUri] =  $rolePrivilegeRetriever->retrieveByResourceIds([$itemUri]);
53            }
54        }
55
56        $permissionService->change(new ChangePermissionsCommand($movedResource, $rolePrivilegeList, true));
57
58        if (isset($itemPrivilegesMap)) {
59            foreach ($itemPrivilegesMap as $uri => $itemPrivilege) {
60                $permissionService->change(
61                    new ChangePermissionsCommand(
62                        $this->getResource($uri),
63                        $itemPrivilege,
64                        true
65                    )
66                );
67            }
68        }
69    }
70
71    private function getRolePrivilegeRetriever(): RolePrivilegeRetriever
72    {
73        return $this->getServiceLocator()->get(RolePrivilegeRetriever::class);
74    }
75
76    private function getPermissionService(): ChangePermissionsService
77    {
78        return $this->getServiceManager()->getContainer()->get(ChangePermissionsService::class);
79    }
80}