Passed
Pull Request — main (#5039)
by Bernard
09:03
created

User::genQRcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use Closure;
23
use Fisharebest\Webtrees\Contracts\UserInterface;
24
use PragmaRX\Google2FA\Google2FA;
25
use chillerlan\QRCode\QRCode;
26
27
use function is_string;
28
29
/**
30
 * Provide an interface to the wt_user table.
31
 */
32
class User implements UserInterface
33
{
34
    private int $user_id;
35
36
    private string $user_name;
37
38
    private string $real_name;
39
40
    private string $email;
41
42
    /** @var array<string,string> */
43
    private array $preferences;
44
45
    /**
46
     * @param int    $user_id
47
     * @param string $user_name
48
     * @param string $real_name
49
     * @param string $email
50
     */
51
    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
52
    {
53
        $this->user_id   = $user_id;
54
        $this->user_name = $user_name;
55
        $this->real_name = $real_name;
56
        $this->email     = $email;
57
58
        $this->preferences = DB::table('user_setting')
59
            ->where('user_id', '=', $this->user_id)
60
            ->pluck('setting_value', 'setting_name')
61
            ->all();
62
    }
63
64
    /**
65
     * The user‘s internal identifier.
66
     *
67
     * @return int
68
     */
69
    public function id(): int
70
    {
71
        return $this->user_id;
72
    }
73
74
    /**
75
     * The users email address.
76
     *
77
     * @return string
78
     */
79
    public function email(): string
80
    {
81
        return $this->email;
82
    }
83
84
    /**
85
     * Set the email address of this user.
86
     *
87
     * @param string $email
88
     *
89
     * @return User
90
     */
91
    public function setEmail(string $email): User
92
    {
93
        if ($this->email !== $email) {
94
            $this->email = $email;
95
96
            DB::table('user')
97
                ->where('user_id', '=', $this->user_id)
98
                ->update([
99
                    'email' => $email,
100
                ]);
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * The user‘s real name.
108
     *
109
     * @return string
110
     */
111
    public function realName(): string
112
    {
113
        return $this->real_name;
114
    }
115
    /**
116
     * Generate a QR code image based on 2FA secret and return both.
117
     *
118
     * @return array<string, mixed>
119
     */
120
121
    public function genQRcode(): array
122
    {
123
        $qrinfo = array();
124
        $google2fa = new Google2FA();
125
        $qrinfo['secret'] = $google2fa->generateSecretKey();
126
        $data = 'otpauth://totp/' . $this->user_id . '?secret=' . $qrinfo['secret'] . '&issuer=' . $_SERVER['SERVER_NAME'];
127
        $qrcode = new QRCode();
128
        $qrinfo['qrcode'] = $qrcode->render($data);
129
        return $qrinfo;
130
    }
131
132
    /**
133
     * Set the real name of this user.
134
     *
135
     * @param string $real_name
136
     *
137
     * @return User
138
     */
139
    public function setRealName(string $real_name): User
140
    {
141
        if ($this->real_name !== $real_name) {
142
            $this->real_name = $real_name;
143
144
            DB::table('user')
145
                ->where('user_id', '=', $this->user_id)
146
                ->update([
147
                    'real_name' => $real_name,
148
                ]);
149
        }
150
151
        return $this;
152
    }
153
154
    /**
155
     * The user‘s login name.
156
     *
157
     * @return string
158
     */
159
    public function userName(): string
160
    {
161
        return $this->user_name;
162
    }
163
164
    /**
165
     * Set the login name for this user.
166
     *
167
     * @param string $user_name
168
     *
169
     * @return self
170
     */
171
    public function setUserName(string $user_name): self
172
    {
173
        if ($this->user_name !== $user_name) {
174
            $this->user_name = $user_name;
175
176
            DB::table('user')
177
                ->where('user_id', '=', $this->user_id)
178
                ->update([
179
                    'user_name' => $user_name,
180
                ]);
181
        }
182
183
        return $this;
184
    }
185
186
    /**
187
     * Fetch a user option/setting from the wt_user_setting table.
188
     * Since we'll fetch several settings for each user, and since there aren't
189
     * that many of them, fetch them all in one database query
190
     *
191
     * @param string $setting_name
192
     * @param string $default
193
     *
194
     * @return string
195
     */
196
    public function getPreference(string $setting_name, string $default = ''): string
197
    {
198
        return $this->preferences[$setting_name] ?? $default;
199
    }
200
201
    /**
202
     * Update a setting for the user.
203
     *
204
     * @param string $setting_name
205
     * @param string $setting_value
206
     *
207
     * @return void
208
     */
209
    public function setPreference(string $setting_name, string $setting_value): void
210
    {
211
        if ($this->getPreference($setting_name) !== $setting_value) {
212
            DB::table('user_setting')->updateOrInsert([
213
                'user_id'      => $this->user_id,
214
                'setting_name' => $setting_name,
215
            ], [
216
                'setting_value' => $setting_value,
217
            ]);
218
219
            $this->preferences[$setting_name] = $setting_value;
220
        }
221
    }
222
223
    /**
224
     * Set the password of this user.
225
     *
226
     * @param string $password
227
     *
228
     * @return User
229
     */
230
    public function setPassword(#[\SensitiveParameter] string $password): User
231
    {
232
        DB::table('user')
233
            ->where('user_id', '=', $this->user_id)
234
            ->update([
235
                'password' => password_hash($password, PASSWORD_DEFAULT),
236
            ]);
237
238
        return $this;
239
    }
240
241
    /**
242
     * Validate a supplied password
243
     *
244
     * @param string $password
245
     *
246
     * @return bool
247
     */
248
    public function checkPassword(#[\SensitiveParameter] string $password): bool
249
    {
250
        $password_hash = DB::table('user')
251
            ->where('user_id', '=', $this->id())
252
            ->value('password');
253
254
        if (is_string($password_hash) && password_verify($password, $password_hash)) {
255
            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
256
                $this->setPassword($password);
257
            }
258
259
            return true;
260
        }
261
262
        return false;
263
    }
264
    /**
265
     * Set the Secret of this user.
266
     *
267
     * @param string $secret
268
     *
269
     * @return User
270
     */
271
    public function setSecret(#[\SensitiveParameter] string $secret): User
272
    {
273
        DB::table('user')
274
            ->where('user_id', '=', $this->user_id)
275
            ->update([
276
                'secret' => $secret,
277
            ]);
278
279
        return $this;
280
    }
281
282
    /**
283
     * Validate a supplied 2fa code
284
     *
285
     * @param string $code2fa
286
     *
287
     * @return bool
288
     */
289
    public function check2facode(string $code2fa): bool
290
    {
291
        $secret = DB::table('user')
292
             ->where('user_id', '=', $this->id())
293
             ->value('secret');
294
        $google2fa = new Google2FA();
295
        if ($google2fa->verifyKey($secret, $code2fa)) {
296
            return true;
297
        }
298
        return false;
299
    }
300
301
    /**
302
     * A closure which will create an object from a database row.
303
     *
304
     * @return Closure(object):User
305
     */
306
    public static function rowMapper(): Closure
307
    {
308
        return static fn (object $row): User => new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
309
    }
310
}
311