Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AlmaDatabase
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Alma Database authentication class
5 *
6 * PHP version 8
7 *
8 * Copyright (C) AK Bibliothek Wien für Sozialwissenschaften 2018.
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  Authentication
25 * @author   Michael Birkner <michael.birkner@akwien.at>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:authentication_handlers Wiki
28 */
29
30namespace VuFind\Auth;
31
32use Laminas\Http\PhpEnvironment\Request;
33use VuFind\Db\Entity\UserEntityInterface;
34use VuFind\Exception\Auth as AuthException;
35
36/**
37 * Authentication class for Alma. The VuFind database and the Alma API are
38 * combined for authentication by this classe.
39 *
40 * @category VuFind
41 * @package  Authentication
42 * @author   Michael Birkner <michael.birkner@akwien.at>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development:plugins:authentication_handlers Wiki
45 */
46class AlmaDatabase extends Database
47{
48    /**
49     * Alma driver
50     *
51     * @var \VuFind\ILS\Driver\Alma
52     */
53    protected $almaDriver = null;
54
55    /**
56     * Alma config
57     *
58     * @var array
59     */
60    protected $almaConfig = null;
61
62    /**
63     * Constructor
64     *
65     * @param \VuFind\ILS\Connection        $catalog       The ILS connection
66     * @param \VuFind\Auth\ILSAuthenticator $authenticator The ILS authenticator
67     */
68    public function __construct(
69        protected \VuFind\ILS\Connection $catalog,
70        protected \VuFind\Auth\ILSAuthenticator $authenticator
71    ) {
72        $this->almaDriver = $catalog->getDriver();
73        $this->almaConfig = $catalog->getDriverConfig();
74    }
75
76    /**
77     * Create a new user account in Alma AND in the VuFind Database.
78     *
79     * @param Request $request Request object containing new account details.
80     *
81     * @return UserEntityInterface New user entity.
82     */
83    public function create($request)
84    {
85        // When in privacy mode, don't create an Alma account and delegate
86        // further code execution to the parent.
87        if ($this->getConfig()->Authentication->privacy) {
88            return parent::create($request);
89        }
90
91        // Collect POST parameters from request
92        $params = $this->collectParamsFromRequest($request);
93
94        // Validate username and password
95        $this->validateUsername($params);
96        $this->validatePassword($params);
97
98        // Get the user service
99        $userService = $this->getUserService();
100
101        // Make sure parameters are correct
102        $this->validateParams($params, $userService);
103
104        // Create user account in Alma
105        $almaAnswer = $this->almaDriver->createAlmaUser($params);
106
107        // Create user account in VuFind user table if Alma gave us an answer
108        if ($almaAnswer !== null) {
109            // If we got this far, we're ready to create the account:
110            $user = $this->createUserFromParams($params, $userService);
111
112            // Add the Alma primary ID as cat_id to the VuFind user table
113            $user->setCatId($almaAnswer->primary_id ?? null);
114
115            // Save the new user to the user table
116            $this->getUserService()->persistEntity($user);
117
118            // Save the credentials to cat_username and cat_password to bypass
119            // the ILS login screen from VuFind
120            $this->authenticator->saveUserCatalogCredentials($user, $params['username'], $params['password']);
121        } else {
122            throw new AuthException($this->translate('ils_account_create_error'));
123        }
124
125        return $user;
126    }
127}