Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
16 / 22
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
User
72.73% covered (warning)
72.73%
16 / 22
62.50% covered (warning)
62.50%
5 / 8
12.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createRowForUsername
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getById
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getByCatalogId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getByUsername
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getByEmail
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getInsecureRows
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getByVerifyHash
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Table Definition for user
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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  Db_Table
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 Site
28 */
29
30namespace VuFind\Db\Table;
31
32use Laminas\Config\Config;
33use Laminas\Db\Adapter\Adapter;
34use Laminas\Session\Container;
35use VuFind\Db\Row\RowGateway;
36use VuFind\Db\Row\User as UserRow;
37
38/**
39 * Table Definition for user
40 *
41 * @category VuFind
42 * @package  Db_Table
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class User extends Gateway
48{
49    /**
50     * VuFind configuration
51     *
52     * @var Config
53     */
54    protected $config;
55
56    /**
57     * Session container
58     *
59     * @var Container
60     */
61    protected $session;
62
63    /**
64     * Constructor
65     *
66     * @param Adapter       $adapter Database adapter
67     * @param PluginManager $tm      Table manager
68     * @param array         $cfg     Laminas configuration
69     * @param RowGateway    $rowObj  Row prototype object (null for default)
70     * @param Config        $config  VuFind configuration
71     * @param Container     $session Session container to inject into rows
72     * (optional; used for privacy mode)
73     * @param string        $table   Name of database table to interface with
74     */
75    public function __construct(
76        Adapter $adapter,
77        PluginManager $tm,
78        $cfg,
79        ?RowGateway $rowObj,
80        Config $config,
81        Container $session = null,
82        $table = 'user'
83    ) {
84        $this->config = $config;
85        $this->session = $session;
86        parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
87    }
88
89    /**
90     * Create a row for the specified username.
91     *
92     * @param string $username Username
93     *
94     * @return UserRow
95     */
96    public function createRowForUsername($username)
97    {
98        $row = $this->createRow();
99        $row->username = $username;
100        $row->created = date('Y-m-d H:i:s');
101        // Failing to initialize this here can cause Laminas\Db errors in
102        // the VuFind\Auth\Shibboleth and VuFind\Auth\ILS integration tests.
103        $row->user_provided_email = 0;
104        return $row;
105    }
106
107    /**
108     * Retrieve a user object from the database based on ID.
109     *
110     * @param int $id ID.
111     *
112     * @return ?UserRow
113     */
114    public function getById($id)
115    {
116        return $this->select(['id' => $id])->current();
117    }
118
119    /**
120     * Retrieve a user object from the database based on catalog ID.
121     *
122     * @param string $catId Catalog ID.
123     *
124     * @return ?UserRow
125     */
126    public function getByCatalogId($catId)
127    {
128        return $this->select(['cat_id' => $catId])->current();
129    }
130
131    /**
132     * Retrieve a user object from the database based on username; when requested,
133     * create a new row if no existing match is found.
134     *
135     * @param string $username Username to use for retrieval.
136     * @param bool   $create   Should we create users that don't already exist?
137     *
138     * @return ?UserRow
139     */
140    public function getByUsername($username, $create = true)
141    {
142        $callback = function ($select) use ($username) {
143            $select->where->literal('lower(username) = lower(?)', [$username]);
144        };
145        $row = $this->select($callback)->current();
146        return ($create && empty($row))
147            ? $this->createRowForUsername($username) : $row;
148    }
149
150    /**
151     * Retrieve a user object from the database based on email.
152     *
153     * @param string $email email to use for retrieval.
154     *
155     * @return ?UserRow
156     */
157    public function getByEmail($email)
158    {
159        $row = $this->select(['email' => $email])->current();
160        return $row;
161    }
162
163    /**
164     * Get user rows with insecure passwords and/or catalog passwords
165     *
166     * @return mixed
167     */
168    public function getInsecureRows()
169    {
170        $callback = function ($select) {
171            $select->where
172                ->notEqualTo('password', '')
173                ->OR->isNotNull('cat_password');
174        };
175        return $this->select($callback);
176    }
177
178    /**
179     * Return a row by a verification hash
180     *
181     * @param string $hash User-unique hash string
182     *
183     * @return ?UserRow
184     */
185    public function getByVerifyHash($hash)
186    {
187        $row = $this->select(['verify_hash' => $hash])->current();
188        return $row;
189    }
190}