Passed
Pull Request — main (#5039)
by
unknown
17:52
created

User::genQRcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
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
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
        $qrinfo['qrcode'] = (new QRCode)->render($data);
128
        return $qrinfo;
129
    }
130
131
    /**
132
     * Set the real name of this user.
133
     *
134
     * @param string $real_name
135
     *
136
     * @return User
137
     */
138
    public function setRealName(string $real_name): User
139
    {
140
        if ($this->real_name !== $real_name) {
141
            $this->real_name = $real_name;
142
143
            DB::table('user')
144
                ->where('user_id', '=', $this->user_id)
145
                ->update([
146
                    'real_name' => $real_name,
147
                ]);
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * The user‘s login name.
155
     *
156
     * @return string
157
     */
158
    public function userName(): string
159
    {
160
        return $this->user_name;
161
    }
162
163
    /**
164
     * Set the login name for this user.
165
     *
166
     * @param string $user_name
167
     *
168
     * @return self
169
     */
170
    public function setUserName(string $user_name): self
171
    {
172
        if ($this->user_name !== $user_name) {
173
            $this->user_name = $user_name;
174
175
            DB::table('user')
176
                ->where('user_id', '=', $this->user_id)
177
                ->update([
178
                    'user_name' => $user_name,
179
                ]);
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * Fetch a user option/setting from the wt_user_setting table.
187
     * Since we'll fetch several settings for each user, and since there aren't
188
     * that many of them, fetch them all in one database query
189
     *
190
     * @param string $setting_name
191
     * @param string $default
192
     *
193
     * @return string
194
     */
195
    public function getPreference(string $setting_name, string $default = ''): string
196
    {
197
        return $this->preferences[$setting_name] ?? $default;
198
    }
199
200
    /**
201
     * Update a setting for the user.
202
     *
203
     * @param string $setting_name
204
     * @param string $setting_value
205
     *
206
     * @return void
207
     */
208
    public function setPreference(string $setting_name, string $setting_value): void
209
    {
210
        if ($this->getPreference($setting_name) !== $setting_value) {
211
            DB::table('user_setting')->updateOrInsert([
212
                'user_id'      => $this->user_id,
213
                'setting_name' => $setting_name,
214
            ], [
215
                'setting_value' => $setting_value,
216
            ]);
217
218
            $this->preferences[$setting_name] = $setting_value;
219
        }
220
    }
221
222
    /**
223
     * Set the password of this user.
224
     *
225
     * @param string $password
226
     *
227
     * @return User
228
     */
229
    public function setPassword(#[\SensitiveParameter] string $password): User
230
    {
231
        DB::table('user')
232
            ->where('user_id', '=', $this->user_id)
233
            ->update([
234
                'password' => password_hash($password, PASSWORD_DEFAULT),
235
            ]);
236
237
        return $this;
238
    }
239
240
    /**
241
     * Validate a supplied password
242
     *
243
     * @param string $password
244
     *
245
     * @return bool
246
     */
247
    public function checkPassword(#[\SensitiveParameter] string $password): bool
248
    {
249
        $password_hash = DB::table('user')
250
            ->where('user_id', '=', $this->id())
251
            ->value('password');
252
253
        if (is_string($password_hash) && password_verify($password, $password_hash)) {
254
            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
255
                $this->setPassword($password);
256
            }
257
258
            return true;
259
        }
260
261
        return false;
262
    }
263
    /**
264
     * Set the Secret of this user.
265
     *
266
     * @param string $secret
267
     *
268
     * @return User
269
     */
270
    public function setSecret(#[\SensitiveParameter] string $secret): User
271
    {
272
        DB::table('user')
273
            ->where('user_id', '=', $this->user_id)
274
            ->update([
275
                'secret' => $secret,
276
            ]);
277
278
        return $this;
279
    }
280
   
281
      /**
282
      * Validate a supplied 2fa code
283
      *
284
      * @param string $code2fa
285
      *
286
      * @return bool
287
      */
288
     public function check2facode(string $code2fa): bool
289
     {
290
         $secret = DB::table('user')
291
             ->where('user_id', '=', $this->id())
292
             ->value('secret');
293
       $google2fa = new Google2FA;
294
         if($google2fa->verifyKey($secret, $code2fa)) {
295
                  return true;
296
               }
297
         return false;
298
     }
299
300
    /**
301
     * A closure which will create an object from a database row.
302
     *
303
     * @return Closure(object):User
304
     */
305
    public static function rowMapper(): Closure
306
    {
307
        return static fn (object $row): User => new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
308
    }
309
}
310