Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiILS
77.78% covered (warning)
77.78%
14 / 18
50.00% covered (danger)
50.00%
2 / 4
7.54
0.00% covered (danger)
0.00%
0 / 1
 authenticate
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 getLoginTargets
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultLoginTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCatalog
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
2.86
1<?php
2
3/**
4 * Multiple ILS authentication module that works with MultiBackend driver
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2013.
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  Authentication
26 * @author   Franck Borel <franck.borel@gbv.de>
27 * @author   Demian Katz <demian.katz@villanova.edu>
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/wiki/development:plugins:authentication_handlers Wiki
31 */
32
33namespace VuFind\Auth;
34
35use VuFind\Db\Entity\UserEntityInterface;
36use VuFind\Exception\Auth as AuthException;
37use VuFind\ILS\Driver\MultiBackend;
38
39use function in_array;
40
41/**
42 * Multiple ILS authentication module that works with MultiBackend driver
43 *
44 * @category VuFind
45 * @package  Authentication
46 * @author   Franck Borel <franck.borel@gbv.de>
47 * @author   Demian Katz <demian.katz@villanova.edu>
48 * @author   Ere Maijala <ere.maijala@helsinki.fi>
49 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
50 * @link     https://vufind.org/wiki/development:plugins:authentication_handlers Wiki
51 */
52class MultiILS extends ILS
53{
54    /**
55     * Attempt to authenticate the current user. Throws exception if login fails.
56     *
57     * @param \Laminas\Http\PhpEnvironment\Request $request Request object containing
58     * account credentials.
59     *
60     * @throws AuthException
61     * @return UserEntityInterface Object representing logged-in user.
62     */
63    public function authenticate($request)
64    {
65        $username = trim($request->getPost()->get('username', ''));
66        $password = trim($request->getPost()->get('password', ''));
67        $target = trim($request->getPost()->get('target', ''));
68        $loginMethod = $this->getILSLoginMethod($target);
69        $rememberMe = (bool)$request->getPost()->get('remember_me', false);
70
71        // We should have target either separately or already embedded into username
72        if ($target) {
73            $username = "$target.$username";
74        } else {
75            [$target] = explode('.', $username);
76        }
77
78        // Check that the target is valid:
79        if (!in_array($target, $this->getLoginTargets())) {
80            throw new AuthException('authentication_error_admin');
81        }
82
83        return $this->handleLogin($username, $password, $loginMethod, $rememberMe);
84    }
85
86    /**
87     * Get login targets (ILS drivers/source ID's)
88     *
89     * @return array
90     */
91    public function getLoginTargets()
92    {
93        return $this->getCatalog()->getLoginDrivers();
94    }
95
96    /**
97     * Get default login target (ILS driver/source ID)
98     *
99     * @return array
100     */
101    public function getDefaultLoginTarget()
102    {
103        return $this->getCatalog()->getDefaultLoginDriver();
104    }
105
106    /**
107     * Set the ILS connection for this object.
108     *
109     * @param \VuFind\ILS\Connection $connection ILS connection to set
110     *
111     * @return void
112     */
113    public function setCatalog(\VuFind\ILS\Connection $connection)
114    {
115        // Right now, MultiILS authentication only works with the MultiBackend
116        // driver; if other ILS drivers eventually support this option, we
117        // should define an interface containing getLoginDrivers() and
118        // getDefaultLoginDriver().
119        if (!($connection->getDriver() instanceof MultiBackend)) {
120            throw new \Exception(
121                'MultiILS authentication requires MultiBackend ILS driver.'
122            );
123        }
124        parent::setCatalog($connection);
125    }
126}