Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IpRegEx
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPermissions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * IpRegEx 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   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Page
28 */
29
30namespace VuFind\Role\PermissionProvider;
31
32use Laminas\Http\PhpEnvironment\Request;
33use VuFind\Net\UserIpReader;
34
35/**
36 * IpRegEx permission provider for VuFind.
37 *
38 * @category VuFind
39 * @package  Authorization
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org Main Page
43 */
44class IpRegEx implements PermissionProviderInterface
45{
46    /**
47     * Request object
48     *
49     * @var Request
50     */
51    protected $request;
52
53    /**
54     * User IP address reader
55     *
56     * @var UserIpReader
57     */
58    protected $userIpReader;
59
60    /**
61     * Constructor
62     *
63     * @param Request      $request      Request object
64     * @param UserIpReader $userIpReader User IP address reader
65     */
66    public function __construct(Request $request, UserIpReader $userIpReader)
67    {
68        $this->request = $request;
69        $this->userIpReader = $userIpReader;
70    }
71
72    /**
73     * Return an array of roles which may be granted the permission based on
74     * the options.
75     *
76     * @param mixed $options Options provided from configuration.
77     *
78     * @return array
79     */
80    public function getPermissions($options)
81    {
82        // Check if any regex matches....
83        $ipAddr = $this->userIpReader->getUserIp();
84        foreach ((array)$options as $current) {
85            if (preg_match($current, $ipAddr)) {
86                // Match? Grant to all users (guest or logged in).
87                return ['guest', 'loggedin'];
88            }
89        }
90
91        //  No match? No permissions.
92        return [];
93    }
94}