Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PermissionManager
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
10.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAuthorized
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 permissionRuleExists
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3/**
4 * Permission Manager
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Authorization
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     http://vufind.org/wiki/ Wiki
29 */
30
31namespace VuFind\Role;
32
33use LmcRbacMvc\Service\AuthorizationServiceAwareTrait;
34
35use function in_array;
36use function is_array;
37
38/**
39 * Permission Manager
40 *
41 * @category VuFind
42 * @package  Authorization
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @author   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     http://vufind.org/wiki/ Wiki
47 */
48class PermissionManager
49{
50    use AuthorizationServiceAwareTrait;
51
52    /**
53     * List config
54     *
55     * @var array
56     */
57    protected $config;
58
59    /**
60     * Constructor
61     *
62     * @param array $config configuration
63     */
64    public function __construct(array $config)
65    {
66        $this->config = $config;
67    }
68
69    /**
70     * Determine if the user is authorized in a certain context or not
71     *
72     * @param string $permission Permission
73     * @param mixed  $context    Context for the permission behavior (optional)
74     *
75     * @return bool
76     */
77    public function isAuthorized($permission, $context = null)
78    {
79        $authService = $this->getAuthorizationService();
80
81        // if no authorization service is available return false
82        if (!$authService) {
83            return false;
84        }
85
86        if ($authService->isGranted($permission, $context)) {
87            return true;
88        }
89
90        return false;
91    }
92
93    /**
94     * Check if a permission rule exists
95     *
96     * @param string $permission Permission
97     *
98     * @return bool
99     */
100    public function permissionRuleExists($permission)
101    {
102        foreach ($this->config as $value) {
103            if (!isset($value['permission'])) {
104                continue;
105            }
106            if ($value['permission'] == $permission) {
107                return true;
108            }
109            if (
110                is_array($value['permission'])
111                && in_array($permission, $value['permission'])
112            ) {
113                return true;
114            }
115        }
116        return false;
117    }
118}