|
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 Psr\Log\LoggerInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Handle API calls for Users. |
|
24
|
|
|
* |
|
25
|
|
|
* XXX more logging! |
|
26
|
|
|
*/ |
|
27
|
|
|
class UsersModule implements ServiceModuleInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var Users */ |
|
30
|
|
|
private $users; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
33
|
|
|
private $logger; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(Users $users, LoggerInterface $logger) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->users = $users; |
|
38
|
|
|
$this->logger = $logger; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function init(Service $service) |
|
42
|
|
|
{ |
|
43
|
|
|
// DISABLED |
|
44
|
|
|
$service->get( |
|
45
|
|
|
'/users/disabled', |
|
46
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
47
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
48
|
|
|
|
|
49
|
|
|
return new ApiResponse('users', $this->users->getDisabled()); |
|
50
|
|
|
} |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$service->get( |
|
54
|
|
|
'/users/is_disabled', |
|
55
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
56
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
57
|
|
|
$userId = Utils::requireParameter($getData, 'user_id'); |
|
58
|
|
|
InputValidation::userId($userId); |
|
59
|
|
|
|
|
60
|
|
|
return new ApiResponse('disabled', $this->users->isDisabled($userId)); |
|
61
|
|
|
} |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$service->post( |
|
65
|
|
|
'/users/disable', |
|
66
|
|
View Code Duplication |
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
|
|
|
|
|
67
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
68
|
|
|
$userId = Utils::requireParameter($postData, 'user_id'); |
|
69
|
|
|
InputValidation::userId($userId); |
|
70
|
|
|
$this->logger->info(sprintf('disabling user "%s"', $userId)); |
|
71
|
|
|
|
|
72
|
|
|
return new ApiResponse('ok', $this->users->setDisabled($userId)); |
|
73
|
|
|
} |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$service->post( |
|
77
|
|
|
'/users/enable', |
|
78
|
|
View Code Duplication |
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
|
|
|
|
|
79
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
80
|
|
|
$userId = Utils::requireParameter($postData, 'user_id'); |
|
81
|
|
|
InputValidation::userId($userId); |
|
82
|
|
|
$this->logger->info(sprintf('enabling user "%s"', $userId)); |
|
83
|
|
|
|
|
84
|
|
|
return new ApiResponse('ok', $this->users->setEnabled($userId)); |
|
85
|
|
|
} |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
// OTP_SECRETS |
|
89
|
|
|
$service->get( |
|
90
|
|
|
'/users/has_otp_secret', |
|
91
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
92
|
|
|
Utils::requireUser($hookData, ['admin', 'portal']); |
|
93
|
|
|
$userId = Utils::requireParameter($getData, 'user_id'); |
|
94
|
|
|
InputValidation::userId($userId); |
|
95
|
|
|
|
|
96
|
|
|
return new ApiResponse('otp_secret', $this->users->hasOtpSecret($userId)); |
|
97
|
|
|
} |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$service->post( |
|
101
|
|
|
'/users/set_otp_secret', |
|
102
|
|
View Code Duplication |
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
|
|
|
|
|
103
|
|
|
Utils::requireUser($hookData, ['portal']); |
|
104
|
|
|
$userId = Utils::requireParameter($postData, 'user_id'); |
|
105
|
|
|
InputValidation::userId($userId); |
|
106
|
|
|
$otpSecret = Utils::requireParameter($postData, 'otp_secret'); |
|
107
|
|
|
InputValidation::otpSecret($otpSecret); |
|
108
|
|
|
|
|
109
|
|
|
return new ApiResponse('ok', $this->users->setOtpSecret($userId, $otpSecret)); |
|
110
|
|
|
} |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$service->post( |
|
114
|
|
|
'/users/delete_otp_secret', |
|
115
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
116
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
117
|
|
|
$userId = Utils::requireParameter($postData, 'user_id'); |
|
118
|
|
|
InputValidation::userId($userId); |
|
119
|
|
|
|
|
120
|
|
|
return new ApiResponse('ok', $this->users->deleteOtpSecret($userId)); |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
// VOOT_TOKENS |
|
125
|
|
|
$service->get( |
|
126
|
|
|
'/users/has_voot_token', |
|
127
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
128
|
|
|
Utils::requireUser($hookData, ['portal']); |
|
129
|
|
|
$userId = Utils::requireParameter($getData, 'user_id'); |
|
130
|
|
|
InputValidation::userId($userId); |
|
131
|
|
|
|
|
132
|
|
|
return new ApiResponse('voot_token', $this->users->hasVootToken($userId)); |
|
133
|
|
|
} |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$service->post( |
|
137
|
|
|
'/users/set_vook_token', |
|
138
|
|
View Code Duplication |
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
|
|
|
|
|
139
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
140
|
|
|
$userId = Utils::requireParameter($postData, 'user_id'); |
|
141
|
|
|
InputValidation::userId($userId); |
|
142
|
|
|
$vootToken = Utils::requireParameter($postData, 'voot_token'); |
|
143
|
|
|
InputValidation::vootToken($vootToken); |
|
144
|
|
|
|
|
145
|
|
|
return new ApiResponse('ok', $this->users->setVootToken($userId, $vootToken)); |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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.