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
IpRange
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getPermissions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * IpRange permission provider for VuFind.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2007.
9 * Copyright (C) The National Library of Finland 2015.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Authorization
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Jochen Lienhard <lienhard@ub.uni-freiburg.de>
28 * @author   Ere Maijala <ere.maijala@helsinki.fi>
29 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
30 * @link     https://vufind.org Main Page
31 */
32
33namespace VuFind\Role\PermissionProvider;
34
35use Laminas\Stdlib\RequestInterface;
36use VuFind\Net\IpAddressUtils;
37use VuFind\Net\UserIpReader;
38
39/**
40 * IpRange permission provider for VuFind.
41 *
42 * @category VuFind
43 * @package  Authorization
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @author   Jochen Lienhard <lienhard@ub.uni-freiburg.de>
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org Main Page
49 */
50class IpRange implements PermissionProviderInterface
51{
52    /**
53     * Request object
54     *
55     * @var Request
56     */
57    protected $request;
58
59    /**
60     * IpAddressUtils object
61     *
62     * @var IpAddressUtils
63     */
64    protected $ipAddressUtils;
65
66    /**
67     * User IP address reader
68     *
69     * @var UserIpReader
70     */
71    protected $userIpReader;
72
73    /**
74     * Constructor
75     *
76     * @param RequestInterface $request      Request object
77     * @param IpAddressUtils   $ipUtils      IpAddressUtils object
78     * @param UserIpReader     $userIpReader User IP address reader
79     */
80    public function __construct(
81        RequestInterface $request,
82        IpAddressUtils $ipUtils,
83        UserIpReader $userIpReader
84    ) {
85        $this->request = $request;
86        $this->ipAddressUtils = $ipUtils;
87        $this->userIpReader = $userIpReader;
88    }
89
90    /**
91     * Return an array of roles which may be granted the permission based on
92     * the options.
93     *
94     * @param mixed $options Options provided from configuration.
95     *
96     * @return array
97     */
98    public function getPermissions($options)
99    {
100        // Check if any regex matches....
101        $ipAddr = $this->userIpReader->getUserIp();
102        if ($this->ipAddressUtils->isInRange($ipAddr, (array)$options)) {
103            // Match? Grant to all users (guest or logged in).
104            return ['guest', 'loggedin'];
105        }
106
107        //  No match? No permissions.
108        return [];
109    }
110}