/
users_rest.php
372 lines (322 loc) · 11.6 KB
/
users_rest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* A webservice interface to Mantis Bug Tracker
*
* @package MantisBT
* @copyright Copyright MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @noinspection PhpFullyQualifiedNameUsageInspection
*/
/**
* @var \Slim\App $g_app
*/
$g_app->group('/users', function() use ( $g_app ) {
# These 4 cases are just to avoid html errors in case of incomplete urls
$g_app->get( '', 'rest_user_get' );
$g_app->get( '/', 'rest_user_get' );
$g_app->get( '/username/', 'rest_user_get' );
$g_app->get( '/username', 'rest_user_get' );
# This are the real cases for get users
$g_app->get( '/me', 'rest_user_get_me' );
$g_app->get( '/username/{username}', 'rest_user_get' );
$g_app->get( '/{user_id}', 'rest_user_get' );
$g_app->post( '/', 'rest_user_create' );
$g_app->post( '', 'rest_user_create' );
$g_app->patch( '/{id}', 'rest_user_update' );
$g_app->post( '/me/token/', 'rest_user_create_token_for_current_user' );
$g_app->post( '/me/token', 'rest_user_create_token_for_current_user' );
$g_app->post( '/{user_id}/token/', 'rest_user_create_token' );
$g_app->post( '/{user_id}/token', 'rest_user_create_token' );
$g_app->delete( '/me/token/{token_id}/', 'rest_user_delete_token_for_current_user' );
$g_app->delete( '/me/token/{token_id}', 'rest_user_delete_token_for_current_user' );
$g_app->delete( '/{user_id}/token/{token_id}/', 'rest_user_delete_token' );
$g_app->delete( '/{user_id}/token/{token_id}', 'rest_user_delete_token' );
$g_app->delete( '/{id}', 'rest_user_delete' );
$g_app->delete( '/{id}/', 'rest_user_delete' );
$g_app->put( '/{id}/reset', 'rest_user_reset_password' );
});
/**
* A method that does the work to get information about current logged in user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_get_me( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_select = $p_request->getParam( 'select', null );
if( is_null( $t_select ) ) {
$t_select = UserGetCommand::getDefaultFields();
// The `project` field is part of this API's response, but it is not longer a default field
// for newer APIs to get user information, so add it here for backward compatibility.
if( !in_array( 'projects', $t_select ) ) {
$t_select[] = 'projects';
}
} else {
$t_select = explode( ',', $t_select );
}
$t_data = array(
'query' => array(
'user_id' => auth_get_current_user_id(),
'select' => $t_select
),
'options' => array(
'return_as_users' => false
)
);
$t_command = new UserGetCommand( $t_data );
$t_result = $t_command->execute();
return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_result );
}
/**
* A method that does the work to get information about the specified user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*/
function rest_user_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_query = array();
$t_user_id = isset( $p_args['user_id'] ) ? $p_args['user_id'] : null;
if( !is_null( $t_user_id ) ) {
$t_query['user_id'] = (int)$t_user_id;
}
$t_username = isset( $p_args['username'] ) ? $p_args['username'] : null;
if( !is_null( $t_username ) ) {
$t_query['username'] = $t_username;
}
$t_select = $p_request->getParam( 'select', null );
if( !is_null( $t_select ) ) {
$t_select = explode( ',', $t_select );
$t_query['select'] = $t_select;
}
$t_data = array(
'query' => $t_query,
'options' => array(
'return_as_users' => true
)
);
$t_command = new UserGetCommand( $t_data );
$t_result = $t_command->execute();
return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_result );
}
/**
* A method that creates a user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_create( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_payload = $p_request->getParsedBody();
if( !$t_payload ) {
return $p_response->withStatus( HTTP_STATUS_BAD_REQUEST, "Invalid request body or format");
}
$t_data = array( 'payload' => $t_payload );
$t_command = new UserCreateCommand( $t_data );
$t_result = $t_command->execute();
$t_user_id = $t_result['id'];
return $p_response->withStatus( HTTP_STATUS_CREATED, "User created with id $t_user_id" )->
withJson( array( 'user' => mci_user_get( $t_user_id ) ) );
}
/**
* A method that updates a user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_update( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_query = array();
$t_user_id = isset( $p_args['id'] ) ? $p_args['id'] : null;
if( !is_null( $t_user_id ) ) {
$t_query['user_id'] = (int)$t_user_id;
}
$t_payload = $p_request->getParsedBody();
if( !$t_payload ) {
return $p_response->withStatus( HTTP_STATUS_BAD_REQUEST, "Invalid request body or format");
}
$t_data = array(
'query' => $t_query,
'payload' => $t_payload );
$t_command = new UserUpdateCommand( $t_data );
$t_result = $t_command->execute();
$t_user_id = $t_result['user']['id'];
return $p_response->withStatus( HTTP_STATUS_SUCCESS, "User with id $t_user_id updated" )->
withJson( $t_result );
}
/**
* A method that creates a user token for another user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_create_token( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_user_id = $p_args['user_id'];
return execute_create_token_command( $p_request, $p_response, $t_user_id );
}
/**
* A method that creates a user token for current user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_create_token_for_current_user( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_user_id = auth_get_current_user_id();
return execute_create_token_command( $p_request, $p_response, $t_user_id );
}
/**
* Helper method for creation of user tokens
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param integer $p_user_id The id of the user to create token for.
*
* @return \Slim\Http\Response The augmented response.
*/
function execute_create_token_command( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, $p_user_id ) {
// if body is empty or {} it will fail, this is acceptable for this API.
$t_payload = $p_request->getParsedBody();
if( !$t_payload ) {
$t_payload = array();
}
$t_data = array(
'query' => array(
'user_id' => (int)$p_user_id
),
'payload' => $t_payload
);
$t_command = new UserTokenCreateCommand( $t_data );
$t_result = $t_command->execute();
return $p_response->withStatus( HTTP_STATUS_CREATED, "User token created" )->
withJson( $t_result );
}
/**
* A method that deletes a user token for another user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_delete_token( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_token_id = $p_args['token_id'];
$t_user_id = $p_args['user_id'];
return execute_delete_token_command( $p_request, $p_response, $t_user_id, $t_token_id );
}
/**
* A method that deletes a user token for current user.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_delete_token_for_current_user( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_token_id = $p_args['token_id'];
$t_user_id = auth_get_current_user_id();
return execute_delete_token_command( $p_request, $p_response, $t_user_id, $t_token_id );
}
/**
* Helper method for creation of user tokens
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param integer $p_user_id The id of the user to create token for.
*
* @return \Slim\Http\Response The augmented response.
*/
function execute_delete_token_command( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, $p_user_id, $p_token_id ) {
$t_data = array(
'query' => array(
'id' => $p_token_id,
'user_id' => (int)$p_user_id
)
);
$t_command = new UserTokenDeleteCommand( $t_data );
$t_command->execute();
return $p_response->withStatus( HTTP_STATUS_NO_CONTENT, "User token deleted" );
}
/**
* Delete a user given its id.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_delete( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_user_id = $p_args['id'];
$t_data = array(
'query' => array( 'id' => $t_user_id )
);
$t_command = new UserDeleteCommand( $t_data );
$t_command->execute();
return $p_response->withStatus( HTTP_STATUS_NO_CONTENT );
}
/**
* Reset a user's password given its id.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
*
* @return \Slim\Http\Response The augmented response.
*
* @noinspection PhpUnusedParameterInspection
*/
function rest_user_reset_password( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_user_id = $p_args['id'];
$t_data = array(
'query' => array( 'id' => $t_user_id )
);
$t_command = new UserResetPasswordCommand( $t_data );
$t_command->execute();
return $p_response->withStatus( HTTP_STATUS_NO_CONTENT );
}