GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d962fa...c70e52 )
by François
02:00
created

Storage::getVootToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
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 Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server;
20
21
use PDO;
22
use PDOException;
23
use SURFnet\VPN\Common\RandomInterface;
24
25
class Storage
26
{
27
    /** @var \PDO */
28
    private $db;
29
30
    /** @var \SURFnet\VPN\Common\RandomInterface */
31
    private $random;
32
33
    public function __construct(PDO $db, RandomInterface $random)
34
    {
35
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
36
        $this->db = $db;
37
        // enable foreign keys
38
        $this->db->query('PRAGMA foreign_keys = ON');
39
40
        $this->random = $random;
41
    }
42
43
    public function getUsers()
44
    {
45
        $stmt = $this->db->prepare(
46
            'SELECT external_user_id, is_disabled
47
             FROM users'
48
        );
49
        $stmt->execute();
50
51
        $userList = [];
52
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result) {
53
            $userList[] = [
54
                'user_id' => $result['external_user_id'],
55
                'is_disabled' => boolval($result['is_disabled']),
56
            ];
57
        }
58
59
        return $userList;
60
    }
61
62 View Code Duplication
    public function getUserCertificateInfo($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $stmt = $this->db->prepare(
65
            'SELECT 
66
                u.external_user_id AS user_id, 
67
                u.is_disabled AS user_is_disabled,
68
                c.display_name AS display_name,
69
                c.is_disabled AS certificate_is_disabled 
70
             FROM users u, certificates c 
71
             WHERE c.common_name = :common_name'
72
        );
73
74
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
75
        $stmt->execute();
76
77
        return $stmt->fetch(PDO::FETCH_ASSOC);
78
    }
79
80
    private function getUserId($externalUserId)
81
    {
82
        $stmt = $this->db->prepare(
83
            'SELECT user_id
84
             FROM users
85
             WHERE external_user_id = :external_user_id'
86
        );
87
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
88
        $stmt->execute();
89
90
        if (false !== $result = $stmt->fetch(PDO::FETCH_ASSOC)) {
91
            return $result['user_id'];
92
        }
93
94
        // user does not exist yet, add it
95
        $stmt = $this->db->prepare(
96
            'INSERT INTO users (external_user_id, user_id) VALUES(:external_user_id, :user_id)'
97
        );
98
99
        $userId = $this->random->get(16);
100
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
101
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
102
        $stmt->execute();
103
104
        return $userId;
105
    }
106
107 View Code Duplication
    public function getVootToken($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $userId = $this->getUserId($externalUserId);
110
        $stmt = $this->db->prepare(
111
            'SELECT voot_token
112
             FROM voot_tokens
113
             WHERE user_id = :user_id'
114
        );
115
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
116
        $stmt->execute();
117
118
        return $stmt->fetchColumn();
119
    }
120
121
    public function setVootToken($externalUserId, $vootToken)
122
    {
123
        $userId = $this->getUserId($externalUserId);
124
        $stmt = $this->db->prepare(
125
            'INSERT INTO voot_tokens (user_id, voot_token) VALUES(:user_id, :voot_token)'
126
        );
127
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
128
        $stmt->bindValue(':voot_token', $vootToken, PDO::PARAM_STR);
129
130
        $stmt->execute();
131
132
        return 1 === $stmt->rowCount();
133
    }
134
135 View Code Duplication
    public function hasVootToken($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $userId = $this->getUserId($externalUserId);
138
        $stmt = $this->db->prepare(
139
            'SELECT COUNT(*)
140
             FROM voot_tokens
141
             WHERE user_id = :user_id'
142
        );
143
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
144
        $stmt->execute();
145
146
        return 1 === intval($stmt->fetchColumn());
147
    }
148
149 View Code Duplication
    public function deleteVootToken($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $userId = $this->getUserId($externalUserId);
152
        $stmt = $this->db->prepare(
153
            'DELETE FROM voot_tokens WHERE user_id = :user_id'
154
        );
155
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
156
157
        $stmt->execute();
158
159
        return 1 === $stmt->rowCount();
160
    }
161
162 View Code Duplication
    public function hasTotpSecret($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $userId = $this->getUserId($externalUserId);
165
        $stmt = $this->db->prepare(
166
            'SELECT COUNT(*)
167
             FROM totp_secrets
168
             WHERE user_id = :user_id'
169
        );
170
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
171
        $stmt->execute();
172
173
        return 1 === intval($stmt->fetchColumn());
174
    }
175
176 View Code Duplication
    public function getTotpSecret($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $userId = $this->getUserId($externalUserId);
179
        $stmt = $this->db->prepare(
180
            'SELECT totp_secret
181
             FROM totp_secrets
182
             WHERE user_id = :user_id'
183
        );
184
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
185
        $stmt->execute();
186
187
        return $stmt->fetchColumn();
188
    }
189
190 View Code Duplication
    public function setTotpSecret($externalUserId, $totpSecret)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $userId = $this->getUserId($externalUserId);
193
        $stmt = $this->db->prepare(
194
            'INSERT INTO totp_secrets (user_id, totp_secret) VALUES(:user_id, :totp_secret)'
195
        );
196
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
197
        $stmt->bindValue(':totp_secret', $totpSecret, PDO::PARAM_STR);
198
199
        try {
200
            $stmt->execute();
201
        } catch (PDOException $e) {
202
            // unable to add the TOTP secret, probably uniqueness contrains
203
            return false;
204
        }
205
206
        return true;
207
    }
208
209 View Code Duplication
    public function deleteTotpSecret($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211
        $userId = $this->getUserId($externalUserId);
212
        $stmt = $this->db->prepare(
213
            'DELETE FROM totp_secrets WHERE user_id = :user_id'
214
        );
215
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
216
217
        $stmt->execute();
218
219
        return 1 === $stmt->rowCount();
220
    }
221
222 View Code Duplication
    public function deleteUser($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224
        $stmt = $this->db->prepare(
225
            'DELETE FROM users WHERE external_user_id = :external_user_id'
226
        );
227
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
228
229
        $stmt->execute();
230
231
        return 1 === $stmt->rowCount();
232
    }
233
234 View Code Duplication
    public function addCertificate($externalUserId, $commonName, $displayName, $validFrom, $validTo)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
    {
236
        $userId = $this->getUserId($externalUserId);
237
        $stmt = $this->db->prepare(
238
            'INSERT INTO certificates (common_name, user_id, display_name, valid_from, valid_to) VALUES(:common_name, :user_id, :display_name, :valid_from, :valid_to)'
239
        );
240
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
241
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
242
        $stmt->bindValue(':display_name', $displayName, PDO::PARAM_STR);
243
        $stmt->bindValue(':valid_from', $validFrom, PDO::PARAM_INT);
244
        $stmt->bindValue(':valid_to', $validTo, PDO::PARAM_INT);
245
246
        $stmt->execute();
247
248
        return 1 === $stmt->rowCount();
249
    }
250
251
    public function getCertificates($externalUserId)
252
    {
253
        $userId = $this->getUserId($externalUserId);
254
        $stmt = $this->db->prepare(
255
            'SELECT common_name, display_name, valid_from, valid_to, is_disabled
256
             FROM certificates
257
             WHERE user_id = :user_id'
258
        );
259
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
260
        $stmt->execute();
261
262
        $certificateList = [];
263
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result) {
264
            $certificateList[] = [
265
                'common_name' => $result['common_name'],
266
                'display_name' => $result['display_name'],
267
                'valid_from' => intval($result['valid_from']),
268
                'valid_to' => intval($result['valid_to']),
269
                'is_disabled' => boolval($result['is_disabled']),
270
            ];
271
        }
272
273
        return $certificateList;
274
    }
275
276 View Code Duplication
    public function disableCertificate($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
277
    {
278
        $stmt = $this->db->prepare(
279
            'UPDATE certificates SET is_disabled = 1 WHERE common_name = :common_name'
280
        );
281
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
282
283
        $stmt->execute();
284
285
        return 1 === $stmt->rowCount();
286
    }
287
288 View Code Duplication
    public function enableCertificate($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
    {
290
        $stmt = $this->db->prepare(
291
            'UPDATE certificates SET is_disabled = 0 WHERE common_name = :common_name'
292
        );
293
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
294
295
        $stmt->execute();
296
297
        return 1 === $stmt->rowCount();
298
    }
299
300 View Code Duplication
    public function disableUser($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
    {
302
        $stmt = $this->db->prepare(
303
            'UPDATE users SET is_disabled = 1 WHERE external_user_id = :external_user_id'
304
        );
305
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
306
307
        $stmt->execute();
308
309
        // XXX it seems on update the rowCount is always 1, even if nothing was
310
        // modified?
311
        return 1 === $stmt->rowCount();
312
    }
313
314 View Code Duplication
    public function enableUser($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
315
    {
316
        $stmt = $this->db->prepare(
317
            'UPDATE users SET is_disabled = 0 WHERE external_user_id = :external_user_id'
318
        );
319
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
320
321
        $stmt->execute();
322
323
        // XXX it seems on update the rowCount is always 1, even if nothing was
324
        // modified?
325
        return 1 === $stmt->rowCount();
326
    }
327
328 View Code Duplication
    public function isDisabledUser($externalUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
329
    {
330
        $stmt = $this->db->prepare(
331
            'SELECT COUNT(*)
332
             FROM users
333
             WHERE external_user_id = :external_user_id AND is_disabled = 1'
334
        );
335
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
336
        $stmt->execute();
337
338
        return 1 === intval($stmt->fetchColumn());
339
    }
340
341
    public function getAllLogEntries()
342
    {
343
        $stmt = $this->db->prepare(
344
            'SELECT c.user_id, l.common_name, l.connected_at, l.disconnected_at, l.bytes_transferred
345
             FROM connection_log l, certificates c
346
             WHERE c.common_name = l.common_name'
347
        );
348
349
        $stmt->execute();
350
351
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
352
    }
353
354 View Code Duplication
    public function clientConnect($profileId, $commonName, $ip4, $ip6, $connectedAt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
355
    {
356
        $stmt = $this->db->prepare(
357
            'INSERT INTO connection_log (
358
                profile_id,
359
                common_name,
360
                ip4,
361
                ip6,
362
                connected_at
363
             ) 
364
             VALUES(
365
                :profile_id, 
366
                :common_name,
367
                :ip4,
368
                :ip6,
369
                :connected_at
370
             )'
371
        );
372
373
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
374
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
375
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
376
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
377
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
378
379
        $stmt->execute();
380
381
        return 1 === $stmt->rowCount();
382
    }
383
384
    public function clientDisconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)
385
    {
386
        $stmt = $this->db->prepare(
387
            'UPDATE connection_log
388
                SET 
389
                    disconnected_at = :disconnected_at, 
390
                    bytes_transferred = :bytes_transferred
391
                WHERE 
392
                    profile_id = :profile_id AND
393
                    common_name = :common_name AND
394
                    ip4 = :ip4 AND
395
                    ip6 = :ip6 AND
396
                    connected_at = :connected_at
397
            '
398
        );
399
400
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
401
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
402
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
403
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
404
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
405
        $stmt->bindValue(':disconnected_at', $disconnectedAt, PDO::PARAM_INT);
406
        $stmt->bindValue(':bytes_transferred', $bytesTransferred, PDO::PARAM_INT);
407
408
        $stmt->execute();
409
410
        return 1 === $stmt->rowCount();
411
    }
412
413 View Code Duplication
    public function getLogEntry($dateTimeUnix, $ipAddress)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
414
    {
415
        $stmt = $this->db->prepare(
416
            'SELECT profile_id, common_name, ip4, ip6, connected_at, disconnected_at
417
             FROM connection_log
418
             WHERE
419
                (ip4 = :ip_address OR ip6 = :ip_address)
420
                AND connected_at < :date_time_unix
421
                AND (disconnected_at > :date_time_unix OR disconnected_at IS NULL)'
422
        );
423
        $stmt->bindValue(':ip_address', $ipAddress, PDO::PARAM_STR);
424
        $stmt->bindValue(':date_time_unix', $dateTimeUnix, PDO::PARAM_STR);
425
        $stmt->execute();
426
427
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
428
    }
429
430 View Code Duplication
    public function recordTotpKey($externalUserId, $totpKey, $timeUnix)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
    {
432
        $userId = $this->getUserId($externalUserId);
433
        $stmt = $this->db->prepare(
434
            'INSERT INTO totp_log (
435
                user_id,
436
                totp_key,
437
                time_unix
438
             ) 
439
             VALUES(
440
                :user_id, 
441
                :totp_key,
442
                :time_unix
443
             )'
444
        );
445
446
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
447
        $stmt->bindValue(':totp_key', $totpKey, PDO::PARAM_STR);
448
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
449
450
        try {
451
            $stmt->execute();
452
        } catch (PDOException $e) {
453
            // unable to record the TOTP, probably uniqueness contrains
454
            return false;
455
        }
456
457
        return true;
458
    }
459
460 View Code Duplication
    public function cleanConnectionLog($timeUnix)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
461
    {
462
        $stmt = $this->db->prepare(
463
            sprintf(
464
                'DELETE FROM connection_log
465
                    WHERE connected_at < :time_unix'
466
            )
467
        );
468
469
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
470
471
        return $stmt->execute();
472
    }
473
474 View Code Duplication
    public function cleanTotpLog($timeUnix)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
475
    {
476
        $stmt = $this->db->prepare(
477
            sprintf(
478
                'DELETE FROM totp_log
479
                    WHERE time_unix < :time_unix'
480
            )
481
        );
482
483
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
484
485
        return $stmt->execute();
486
    }
487
488
    public function motd()
489
    {
490
        $stmt = $this->db->prepare(
491
            'SELECT motd_message FROM motd'
492
        );
493
494
        $stmt->execute();
495
496
        return $stmt->fetchColumn();
497
    }
498
499 View Code Duplication
    public function setMotd($motdMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
500
    {
501
        $this->deleteMotd();
502
503
        $stmt = $this->db->prepare(
504
            'INSERT INTO motd (motd_message) VALUES(:motd_message)'
505
        );
506
507
        $stmt->bindValue(':motd_message', $motdMessage, PDO::PARAM_STR);
508
        $stmt->execute();
509
510
        return 1 === $stmt->rowCount();
511
    }
512
513
    public function deleteMotd()
514
    {
515
        $stmt = $this->db->prepare(
516
            'DELETE FROM motd'
517
        );
518
519
        $stmt->execute();
520
521
        return 1 === $stmt->rowCount();
522
    }
523
524
    public function init()
525
    {
526
        $queryList = [
527
            'CREATE TABLE IF NOT EXISTS users (
528
                user_id VARCHAR(255) PRIMARY KEY,
529
                external_user_id VARCHAR(255) UNIQUE NOT NULL,
530
                is_disabled BOOLEAN DEFAULT 0
531
            )',
532
            'CREATE TABLE IF NOT EXISTS voot_tokens (
533
                voot_token VARCHAR(255) NOT NULL PRIMARY KEY,   
534
                user_id VARCHAR(255) UNIQUE NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
535
            )',
536
            'CREATE TABLE IF NOT EXISTS totp_secrets (
537
                totp_secret VARCHAR(255) NOT NULL PRIMARY KEY,   
538
                user_id VARCHAR(255) UNIQUE NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
539
            )',
540
            'CREATE TABLE IF NOT EXISTS certificates (
541
                common_name VARCHAR(255) NOT NULL PRIMARY KEY,
542
                display_name VARCHAR(255) NOT NULL,
543
                valid_from INTEGER NOT NULL,
544
                valid_to INTEGER NOT NULL,
545
                is_disabled BOOLEAN DEFAULT 0,
546
                user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
547
            )',
548
            'CREATE TABLE IF NOT EXISTS connection_log (
549
                common_name VARCHAR(255) NOT NULL REFERENCES certificates(common_name),
550
                profile_id VARCHAR(255) NOT NULL,
551
                ip4 VARCHAR(255) NOT NULL,
552
                ip6 VARCHAR(255) NOT NULL,
553
                connected_at INTEGER NOT NULL,
554
                disconnected_at INTEGER DEFAULT NULL,
555
                bytes_transferred INTEGER DEFAULT NULL                
556
            )',
557
            'CREATE TABLE IF NOT EXISTS totp_log (
558
                totp_key VARCHAR(255) NOT NULL,
559
                time_unix INTEGER NOT NULL,
560
                user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
561
                UNIQUE(user_id, totp_key)
562
            )',
563
            'CREATE TABLE IF NOT EXISTS motd (
564
                motd_message TEXT NOT NULL
565
            )',
566
        ];
567
568
        foreach ($queryList as $query) {
569
            $this->db->query($query);
570
        }
571
    }
572
}
573