Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Intersection
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
12
100.00% covered (success)
100.00%
1 / 1
 spawn
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissions
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 onResourceCreated
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getSupportedRights
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
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) 2014 (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 * Simple permissible Permission model
30 *
31 * does not require privileges
32 * does not grant privileges
33 *
34 * @access public
35 * @author Joel Bout, <joel@taotesting.com>
36 */
37class Intersection extends ConfigurableService implements PermissionInterface
38{
39    /**
40     *
41     * @param PermissionInterface[] $persmissionMdels
42     */
43    public static function spawn($persmissionMdels)
44    {
45        return new self(['inner' => $persmissionMdels]);
46    }
47
48    public function __construct($options = [])
49    {
50        parent::__construct($options);
51    }
52
53    protected function getInner()
54    {
55        return $this->getOption('inner');
56    }
57
58
59    /**
60     * (non-PHPdoc)
61     * @see PermissionInterface::getPermissions()
62     */
63    public function getPermissions(User $user, array $resourceIds)
64    {
65
66        $results = [];
67        foreach ($this->getInner() as $impl) {
68            $results[] = $impl->getPermissions($user, $resourceIds);
69        }
70
71        $rights = [];
72        foreach ($resourceIds as $id) {
73            $intersect = null;
74            foreach ($results as $modelResult) {
75                $intersect = is_null($intersect)
76                    ? $modelResult[$id]
77                    : array_intersect($intersect, $modelResult[$id]);
78            }
79            $rights[$id] = array_values($intersect);
80        }
81
82        return $rights;
83    }
84
85    /**
86     * (non-PHPdoc)
87     * @see PermissionInterface::onResourceCreated()
88     */
89    public function onResourceCreated(\core_kernel_classes_Resource $resource)
90    {
91        foreach ($this->getInner() as $impl) {
92            $impl->onResourceCreated($resource);
93        }
94    }
95
96    /**
97     * (non-PHPdoc)
98     * @see PermissionInterface::getSupportedPermissions()
99     */
100    public function getSupportedRights()
101    {
102        $models = $this->getInner();
103        $first = array_pop($models);
104        $supported = $first->getSupportedRights();
105
106        while (!empty($models)) {
107            $model = array_pop($models);
108            $supported = array_intersect($supported, $model->getSupportedRights());
109        }
110
111        return array_values($supported);
112    }
113}