Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
User
92.86% covered (success)
92.86%
13 / 14
50.00% covered (danger)
50.00%
1 / 2
7.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
 getPermissions
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
6.02
1<?php
2
3/**
4 * User permission provider for VuFind.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2007.
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   Markus Beh <markus.beh@ub.uni-freiburg.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     http://www.vufind.org  Main Page
28 */
29
30namespace VuFind\Role\PermissionProvider;
31
32use LmcRbacMvc\Service\AuthorizationService;
33
34use function count;
35
36/**
37 * LDAP permission provider for VuFind.
38 * based on permission provider Username.php
39 *
40 * @category VuFind
41 * @package  Authorization
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     http://www.vufind.org  Main Page
45 */
46class User implements
47    PermissionProviderInterface,
48    \Laminas\Log\LoggerAwareInterface
49{
50    use \VuFind\Log\LoggerAwareTrait;
51
52    /**
53     * Authorization object
54     *
55     * @var AuthorizationService
56     */
57    protected $auth;
58
59    /**
60     * Constructor
61     *
62     * @param AuthorizationService $authorization Authorization service
63     */
64    public function __construct(AuthorizationService $authorization)
65    {
66        $this->auth = $authorization;
67    }
68
69    /**
70     * Return an array of roles which may be granted the permission based on
71     * the options.
72     *
73     * @param mixed $options Options provided from configuration.
74     *
75     * @return array
76     */
77    public function getPermissions($options)
78    {
79        // If no user is logged in, or the user doesn't match the passed-in
80        // filter, we can't grant the permission to any roles.
81        if (!($user = $this->auth->getIdentity())) {
82            return [];
83        }
84
85        // which user attribute has to match which pattern to get permissions?
86        foreach ((array)$options as $option) {
87            $parts = explode(' ', $option, 2);
88            if (count($parts) < 2) {
89                $this->logError("configuration option '{$option}' invalid");
90                return [];
91            } else {
92                [$attribute, $pattern] = $parts;
93
94                // check user attribute values against the pattern
95                if (! preg_match('/^\/.*\/$/', $pattern)) {
96                    $pattern = '/' . $pattern . '/';
97                }
98
99                if (preg_match($pattern, $user[$attribute])) {
100                    return ['loggedin'];
101                }
102            }
103        }
104
105        //no matches found, so the user don't get any permissions
106        return [];
107    }
108}