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 ( 9fb0b1...dd411a )
by François
02:47
created

Storage::deleteCertificate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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 
71
                users u, certificates c 
72
             WHERE 
73
                u.user_id = c.user_id AND 
74
                c.common_name = :common_name'
75
        );
76
77
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
78
        $stmt->execute();
79
80
        return $stmt->fetch(PDO::FETCH_ASSOC);
81
    }
82
83
    private function getUserId($externalUserId)
84
    {
85
        $stmt = $this->db->prepare(
86
            'SELECT user_id
87
             FROM users
88
             WHERE external_user_id = :external_user_id'
89
        );
90
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
91
        $stmt->execute();
92
93
        if (false !== $result = $stmt->fetch(PDO::FETCH_ASSOC)) {
94
            return $result['user_id'];
95
        }
96
97
        // user does not exist yet, add it
98
        $stmt = $this->db->prepare(
99
            'INSERT INTO users (external_user_id, user_id) VALUES(:external_user_id, :user_id)'
100
        );
101
102
        $userId = $this->random->get(16);
103
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
104
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
105
        $stmt->execute();
106
107
        return $userId;
108
    }
109
110 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...
111
    {
112
        $userId = $this->getUserId($externalUserId);
113
        $stmt = $this->db->prepare(
114
            'SELECT voot_token
115
             FROM voot_tokens
116
             WHERE user_id = :user_id'
117
        );
118
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
119
        $stmt->execute();
120
121
        return $stmt->fetchColumn();
122
    }
123
124
    public function setVootToken($externalUserId, $vootToken)
125
    {
126
        $userId = $this->getUserId($externalUserId);
127
        $stmt = $this->db->prepare(
128
            'INSERT INTO voot_tokens (user_id, voot_token) VALUES(:user_id, :voot_token)'
129
        );
130
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
131
        $stmt->bindValue(':voot_token', $vootToken, PDO::PARAM_STR);
132
133
        $stmt->execute();
134
135
        return 1 === $stmt->rowCount();
136
    }
137
138 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...
139
    {
140
        $userId = $this->getUserId($externalUserId);
141
        $stmt = $this->db->prepare(
142
            'SELECT COUNT(*)
143
             FROM voot_tokens
144
             WHERE user_id = :user_id'
145
        );
146
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
147
        $stmt->execute();
148
149
        return 1 === intval($stmt->fetchColumn());
150
    }
151
152 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...
153
    {
154
        $userId = $this->getUserId($externalUserId);
155
        $stmt = $this->db->prepare(
156
            'DELETE FROM voot_tokens WHERE user_id = :user_id'
157
        );
158
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
159
160
        $stmt->execute();
161
162
        return 1 === $stmt->rowCount();
163
    }
164
165 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...
166
    {
167
        $userId = $this->getUserId($externalUserId);
168
        $stmt = $this->db->prepare(
169
            'SELECT COUNT(*)
170
             FROM totp_secrets
171
             WHERE user_id = :user_id'
172
        );
173
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
174
        $stmt->execute();
175
176
        return 1 === intval($stmt->fetchColumn());
177
    }
178
179 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...
180
    {
181
        $userId = $this->getUserId($externalUserId);
182
        $stmt = $this->db->prepare(
183
            'SELECT totp_secret
184
             FROM totp_secrets
185
             WHERE user_id = :user_id'
186
        );
187
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
188
        $stmt->execute();
189
190
        return $stmt->fetchColumn();
191
    }
192
193 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...
194
    {
195
        $userId = $this->getUserId($externalUserId);
196
        $stmt = $this->db->prepare(
197
            'INSERT INTO totp_secrets (user_id, totp_secret) VALUES(:user_id, :totp_secret)'
198
        );
199
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
200
        $stmt->bindValue(':totp_secret', $totpSecret, PDO::PARAM_STR);
201
202
        try {
203
            $stmt->execute();
204
        } catch (PDOException $e) {
205
            // unable to add the TOTP secret, probably uniqueness contrains
206
            return false;
207
        }
208
209
        return true;
210
    }
211
212 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...
213
    {
214
        $userId = $this->getUserId($externalUserId);
215
        $stmt = $this->db->prepare(
216
            'DELETE FROM totp_secrets WHERE user_id = :user_id'
217
        );
218
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
219
220
        $stmt->execute();
221
222
        return 1 === $stmt->rowCount();
223
    }
224
225 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...
226
    {
227
        $stmt = $this->db->prepare(
228
            'DELETE FROM users WHERE external_user_id = :external_user_id'
229
        );
230
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
231
232
        $stmt->execute();
233
234
        return 1 === $stmt->rowCount();
235
    }
236
237 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...
238
    {
239
        $userId = $this->getUserId($externalUserId);
240
        $stmt = $this->db->prepare(
241
            'INSERT INTO certificates (common_name, user_id, display_name, valid_from, valid_to) VALUES(:common_name, :user_id, :display_name, :valid_from, :valid_to)'
242
        );
243
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
244
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
245
        $stmt->bindValue(':display_name', $displayName, PDO::PARAM_STR);
246
        $stmt->bindValue(':valid_from', $validFrom, PDO::PARAM_INT);
247
        $stmt->bindValue(':valid_to', $validTo, PDO::PARAM_INT);
248
249
        $stmt->execute();
250
251
        return 1 === $stmt->rowCount();
252
    }
253
254
    public function getCertificates($externalUserId)
255
    {
256
        $userId = $this->getUserId($externalUserId);
257
        $stmt = $this->db->prepare(
258
            'SELECT common_name, display_name, valid_from, valid_to, is_disabled
259
             FROM certificates
260
             WHERE user_id = :user_id'
261
        );
262
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
263
        $stmt->execute();
264
265
        $certificateList = [];
266
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result) {
267
            $certificateList[] = [
268
                'common_name' => $result['common_name'],
269
                'display_name' => $result['display_name'],
270
                'valid_from' => intval($result['valid_from']),
271
                'valid_to' => intval($result['valid_to']),
272
                'is_disabled' => boolval($result['is_disabled']),
273
            ];
274
        }
275
276
        return $certificateList;
277
    }
278
279 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...
280
    {
281
        $stmt = $this->db->prepare(
282
            'UPDATE certificates SET is_disabled = 1 WHERE common_name = :common_name'
283
        );
284
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
285
286
        $stmt->execute();
287
288
        return 1 === $stmt->rowCount();
289
    }
290
291 View Code Duplication
    public function deleteCertificate($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...
292
    {
293
        $stmt = $this->db->prepare(
294
            'DELETE FROM certificates WHERE common_name = :common_name'
295
        );
296
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
297
298
        $stmt->execute();
299
300
        return 1 === $stmt->rowCount();
301
    }
302
303 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...
304
    {
305
        $stmt = $this->db->prepare(
306
            'UPDATE certificates SET is_disabled = 0 WHERE common_name = :common_name'
307
        );
308
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
309
310
        $stmt->execute();
311
312
        return 1 === $stmt->rowCount();
313
    }
314
315 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...
316
    {
317
        $stmt = $this->db->prepare(
318
            'UPDATE users SET is_disabled = 1 WHERE external_user_id = :external_user_id'
319
        );
320
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
321
322
        $stmt->execute();
323
324
        // XXX it seems on update the rowCount is always 1, even if nothing was
325
        // modified?
326
        return 1 === $stmt->rowCount();
327
    }
328
329 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...
330
    {
331
        $stmt = $this->db->prepare(
332
            'UPDATE users SET is_disabled = 0 WHERE external_user_id = :external_user_id'
333
        );
334
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
335
336
        $stmt->execute();
337
338
        // XXX it seems on update the rowCount is always 1, even if nothing was
339
        // modified?
340
        return 1 === $stmt->rowCount();
341
    }
342
343 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...
344
    {
345
        $stmt = $this->db->prepare(
346
            'SELECT COUNT(*)
347
             FROM users
348
             WHERE external_user_id = :external_user_id AND is_disabled = 1'
349
        );
350
        $stmt->bindValue(':external_user_id', $externalUserId, PDO::PARAM_STR);
351
        $stmt->execute();
352
353
        return 1 === intval($stmt->fetchColumn());
354
    }
355
356
    public function getAllLogEntries()
357
    {
358
        $stmt = $this->db->prepare(
359
            'SELECT 
360
                 c.user_id, l.common_name, l.connected_at, l.disconnected_at, l.bytes_transferred
361
             FROM 
362
                 connection_log l, certificates c
363
             WHERE 
364
                 c.common_name = l.common_name
365
             AND
366
                 l.disconnected_at IS NOT NULL
367
             ORDER BY
368
                 l.connected_at'
369
        );
370
371
        $stmt->execute();
372
373
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
374
    }
375
376 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...
377
    {
378
        $stmt = $this->db->prepare(
379
            'INSERT INTO connection_log (
380
                profile_id,
381
                common_name,
382
                ip4,
383
                ip6,
384
                connected_at
385
             ) 
386
             VALUES(
387
                :profile_id, 
388
                :common_name,
389
                :ip4,
390
                :ip6,
391
                :connected_at
392
             )'
393
        );
394
395
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
396
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
397
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
398
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
399
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
400
401
        $stmt->execute();
402
403
        return 1 === $stmt->rowCount();
404
    }
405
406
    public function clientDisconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)
407
    {
408
        $stmt = $this->db->prepare(
409
            'UPDATE connection_log
410
                SET 
411
                    disconnected_at = :disconnected_at, 
412
                    bytes_transferred = :bytes_transferred
413
                WHERE 
414
                    profile_id = :profile_id AND
415
                    common_name = :common_name AND
416
                    ip4 = :ip4 AND
417
                    ip6 = :ip6 AND
418
                    connected_at = :connected_at
419
            '
420
        );
421
422
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
423
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
424
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
425
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
426
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
427
        $stmt->bindValue(':disconnected_at', $disconnectedAt, PDO::PARAM_INT);
428
        $stmt->bindValue(':bytes_transferred', $bytesTransferred, PDO::PARAM_INT);
429
430
        $stmt->execute();
431
432
        return 1 === $stmt->rowCount();
433
    }
434
435 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...
436
    {
437
        $stmt = $this->db->prepare(
438
            'SELECT profile_id, common_name, ip4, ip6, connected_at, disconnected_at
439
             FROM connection_log
440
             WHERE
441
                (ip4 = :ip_address OR ip6 = :ip_address)
442
                AND connected_at < :date_time_unix
443
                AND (disconnected_at > :date_time_unix OR disconnected_at IS NULL)'
444
        );
445
        $stmt->bindValue(':ip_address', $ipAddress, PDO::PARAM_STR);
446
        $stmt->bindValue(':date_time_unix', $dateTimeUnix, PDO::PARAM_STR);
447
        $stmt->execute();
448
449
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
450
    }
451
452 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...
453
    {
454
        $userId = $this->getUserId($externalUserId);
455
        $stmt = $this->db->prepare(
456
            'INSERT INTO totp_log (
457
                user_id,
458
                totp_key,
459
                time_unix
460
             ) 
461
             VALUES(
462
                :user_id, 
463
                :totp_key,
464
                :time_unix
465
             )'
466
        );
467
468
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
469
        $stmt->bindValue(':totp_key', $totpKey, PDO::PARAM_STR);
470
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
471
472
        try {
473
            $stmt->execute();
474
        } catch (PDOException $e) {
475
            // unable to record the TOTP, probably uniqueness contrains
476
            return false;
477
        }
478
479
        return true;
480
    }
481
482 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...
483
    {
484
        $stmt = $this->db->prepare(
485
            sprintf(
486
                'DELETE FROM connection_log
487
                    WHERE connected_at < :time_unix'
488
            )
489
        );
490
491
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
492
493
        return $stmt->execute();
494
    }
495
496 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...
497
    {
498
        $stmt = $this->db->prepare(
499
            sprintf(
500
                'DELETE FROM totp_log
501
                    WHERE time_unix < :time_unix'
502
            )
503
        );
504
505
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
506
507
        return $stmt->execute();
508
    }
509
510
    public function motd()
511
    {
512
        $stmt = $this->db->prepare(
513
            'SELECT motd_message FROM motd'
514
        );
515
516
        $stmt->execute();
517
518
        return $stmt->fetchColumn();
519
    }
520
521 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...
522
    {
523
        $this->deleteMotd();
524
525
        $stmt = $this->db->prepare(
526
            'INSERT INTO motd (motd_message) VALUES(:motd_message)'
527
        );
528
529
        $stmt->bindValue(':motd_message', $motdMessage, PDO::PARAM_STR);
530
        $stmt->execute();
531
532
        return 1 === $stmt->rowCount();
533
    }
534
535
    public function deleteMotd()
536
    {
537
        $stmt = $this->db->prepare(
538
            'DELETE FROM motd'
539
        );
540
541
        $stmt->execute();
542
543
        return 1 === $stmt->rowCount();
544
    }
545
546
    public function init()
547
    {
548
        $queryList = [
549
            'CREATE TABLE IF NOT EXISTS users (
550
                user_id VARCHAR(255) PRIMARY KEY,
551
                external_user_id VARCHAR(255) UNIQUE NOT NULL,
552
                is_disabled BOOLEAN DEFAULT 0
553
            )',
554
            'CREATE TABLE IF NOT EXISTS voot_tokens (
555
                voot_token VARCHAR(255) NOT NULL PRIMARY KEY,   
556
                user_id VARCHAR(255) UNIQUE NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
557
            )',
558
            'CREATE TABLE IF NOT EXISTS totp_secrets (
559
                totp_secret VARCHAR(255) NOT NULL PRIMARY KEY,   
560
                user_id VARCHAR(255) UNIQUE NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
561
            )',
562
            'CREATE TABLE IF NOT EXISTS certificates (
563
                common_name VARCHAR(255) NOT NULL PRIMARY KEY,
564
                display_name VARCHAR(255) NOT NULL,
565
                valid_from INTEGER NOT NULL,
566
                valid_to INTEGER NOT NULL,
567
                is_disabled BOOLEAN DEFAULT 0,
568
                user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
569
            )',
570
            'CREATE TABLE IF NOT EXISTS connection_log (
571
                common_name VARCHAR(255) NOT NULL REFERENCES certificates(common_name),
572
                profile_id VARCHAR(255) NOT NULL,
573
                ip4 VARCHAR(255) NOT NULL,
574
                ip6 VARCHAR(255) NOT NULL,
575
                connected_at INTEGER NOT NULL,
576
                disconnected_at INTEGER DEFAULT NULL,
577
                bytes_transferred INTEGER DEFAULT NULL                
578
            )',
579
            'CREATE TABLE IF NOT EXISTS totp_log (
580
                totp_key VARCHAR(255) NOT NULL,
581
                time_unix INTEGER NOT NULL,
582
                user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
583
                UNIQUE(user_id, totp_key)
584
            )',
585
            'CREATE TABLE IF NOT EXISTS motd (
586
                motd_message TEXT NOT NULL
587
            )',
588
        ];
589
590
        foreach ($queryList as $query) {
591
            $this->db->query($query);
592
        }
593
    }
594
}
595