Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IntersectionUnionSupported
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 add
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 getInner
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getPermissions
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 onResourceCreated
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getSupportedRights
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\generis\model\data\permission\implementation;
23
24use oat\generis\model\data\permission\PermissionInterface;
25use oat\oatbox\service\ConfigurableService;
26use oat\oatbox\user\User;
27
28/**
29 * Implementation to get permissions only from implementations that support the rights
30 *
31 * @access public
32 * @author Antoine Robin, <antoine@taotesting.com>
33 */
34class IntersectionUnionSupported extends ConfigurableService implements PermissionInterface
35{
36    /**
37     * @param PermissionInterface $service
38     * @return $this
39     */
40    public function add(PermissionInterface $service)
41    {
42        $registered = false;
43        $options = $this->getOption('inner');
44        foreach ($options as $impl) {
45            if ($impl == $service) {
46                $registered = true;
47                break;
48            }
49        }
50
51        if (!$registered) {
52            $options[] = $service;
53            $this->setOption('inner', $options);
54        }
55
56        return $this;
57    }
58
59    /**
60     * @return PermissionInterface[]
61     */
62    protected function getInner()
63    {
64        $results = [];
65        foreach ($this->getOption('inner') as $impl) {
66            $impl->setServiceLocator($this->getServiceLocator());
67            $results[] = $impl;
68        }
69        return $results;
70    }
71
72
73    /**
74     * (non-PHPdoc)
75     * @see PermissionInterface::getPermissions()
76     */
77    public function getPermissions(User $user, array $resourceIds)
78    {
79
80        $results = [];
81        $allRights = $this->getSupportedRights();
82
83        foreach ($this->getInner() as $impl) {
84            //Get rights not supported by implementation
85            $notSupported = array_diff($allRights, $impl->getSupportedRights());
86            $resourceRights = $impl->getPermissions($user, $resourceIds);
87            $resourcesRights = [];
88            foreach ($resourceRights as $uri => $resourceRight) {
89                $resourcesRights[$uri] = array_merge($notSupported, $resourceRight);
90            }
91            $results[] = $resourcesRights;
92        }
93
94        $rights = [];
95        foreach ($resourceIds as $id) {
96            $intersect = null;
97            foreach ($results as $modelResult) {
98                $intersect = is_null($intersect)
99                    ? $modelResult[$id]
100                    : array_intersect($intersect, $modelResult[$id]);
101            }
102            $rights[$id] = array_values($intersect);
103        }
104
105        return $rights;
106    }
107
108    /**
109     * (non-PHPdoc)
110     * @see PermissionInterface::onResourceCreated()
111     */
112    public function onResourceCreated(\core_kernel_classes_Resource $resource)
113    {
114        foreach ($this->getInner() as $impl) {
115            $impl->onResourceCreated($resource);
116        }
117    }
118
119    /**
120     * (non-PHPdoc)
121     * @see PermissionInterface::getSupportedPermissions()
122     */
123    public function getSupportedRights()
124    {
125        $models = $this->getInner();
126        $first = array_pop($models);
127        $supported = $first->getSupportedRights();
128        while (!empty($models)) {
129            $model = array_pop($models);
130            $supported = array_merge($supported, $model->getSupportedRights());
131        }
132
133        return array_values(array_unique($supported));
134    }
135}