|
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
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Manages user configuration. |
|
25
|
|
|
* |
|
26
|
|
|
* XXX deal better with exceptions, not everything is a RuntimeException, |
|
27
|
|
|
*/ |
|
28
|
|
|
class Users |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $disableDir; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $otpDir; |
|
35
|
|
|
|
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $vootDir; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct($dataDir) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->disableDir = sprintf('%s/disabled', $dataDir); |
|
42
|
|
|
FileIO::createDir($this->disableDir, 0711); |
|
43
|
|
|
$this->otpDir = sprintf('%s/otp_secrets', $dataDir); |
|
44
|
|
|
FileIO::createDir($this->otpDir, 0711); |
|
45
|
|
|
$this->vootDir = sprintf('%s/voot_tokens', $dataDir); |
|
46
|
|
|
FileIO::createDir($this->vootDir, 0711); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
public function getDisabled() |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$disabledList = []; |
|
52
|
|
|
if (false === $fileList = glob(sprintf('%s/*', $this->disableDir), GLOB_ERR)) { |
|
53
|
|
|
throw new RuntimeException(sprintf('unable to read directory "%s"', $this->disableDir)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($fileList as $fileName) { |
|
57
|
|
|
$disabledList[] = basename($fileName); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $disabledList; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function isDisabled($userId) |
|
64
|
|
|
{ |
|
65
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
66
|
|
|
|
|
67
|
|
|
return @file_exists($disableFile); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setDisabled($userId) |
|
71
|
|
|
{ |
|
72
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
73
|
|
|
FileIO::writeFile($disableFile, time(), 0644); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function setEnabled($userId) |
|
77
|
|
|
{ |
|
78
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
79
|
|
|
FileIO::deleteFile($disableFile); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setOtpSecret($userId, $otpSecret) |
|
83
|
|
|
{ |
|
84
|
|
|
// do not allow override of the OTP secret |
|
85
|
|
|
if (false !== $this->hasOtpSecret($userId)) { |
|
86
|
|
|
// XXX should not be RuntimeException, but e.g. UsersException |
|
87
|
|
|
throw new RuntimeException('cannot overwrite OTP secret'); |
|
88
|
|
|
} |
|
89
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
90
|
|
|
FileIO::writeFile($otpFile, $otpSecret, 0644); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function deleteOtpSecret($userId) |
|
94
|
|
|
{ |
|
95
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
96
|
|
|
FileIO::deleteFile($otpFile); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function hasOtpSecret($userId) |
|
100
|
|
|
{ |
|
101
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
102
|
|
|
|
|
103
|
|
|
return @file_exists($otpFile); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setVootToken($userId, $vootToken) |
|
107
|
|
|
{ |
|
108
|
|
|
$vootFile = sprintf('%s/%s', $this->vootDir, $userId); |
|
109
|
|
|
FileIO::writeFile($vootFile, $vootToken, 0644); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function hasVootToken($userId) |
|
113
|
|
|
{ |
|
114
|
|
|
$vootFile = sprintf('%s/%s', $this->vootDir, $userId); |
|
115
|
|
|
|
|
116
|
|
|
return @file_exists($vootFile); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.