|
1
|
|
|
<?php |
|
2
|
|
|
class PendingUserAPI extends Http\Rest\RestAPI |
|
3
|
|
|
{ |
|
4
|
|
|
public function setup($app) |
|
5
|
|
|
{ |
|
6
|
|
|
$app->get('[/]', array($this, 'listPendingUsers')); |
|
7
|
|
|
$app->get('/{hash}[/]', array($this, 'showPendingUser')); |
|
8
|
|
|
$app->delete('/{hash}[/]', array($this, 'deletePendingUser')); |
|
9
|
|
|
$app->map(['GET', 'POST'], '/{hash}/Actions/activate[/]', array($this, 'activatePendingUser')); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
View Code Duplication |
protected function validateIsAdmin($request) |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
$user = $request->getAttribute('user'); |
|
15
|
|
|
if($user === false) |
|
16
|
|
|
{ |
|
17
|
|
|
throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED); |
|
18
|
|
|
} |
|
19
|
|
|
if(!$user->isInGroupNamed('LDAPAdmins')) |
|
20
|
|
|
{ |
|
21
|
|
|
throw new Exception('Must be Admin', \Http\Rest\ACCESS_DENIED); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function listPendingUsers($request, $response, $args) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->validateIsAdmin($request); |
|
28
|
|
|
$odata = $request->getAttribute('odata', new \ODataParams(array())); |
|
29
|
|
|
$auth = AuthProvider::getInstance(); |
|
30
|
|
|
$users = $auth->getPendingUsersByFilter($odata->filter, $odata->select, $odata->top, $odata->skip, |
|
31
|
|
|
$odata->orderby); |
|
32
|
|
|
return $response->withJson($users); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function showPendingUser($request, $response, $args) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->validateIsAdmin($request); |
|
38
|
|
|
$user = \AuthProvider::getInstance()->getPendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'")); |
|
39
|
|
|
if($user === false) |
|
40
|
|
|
{ |
|
41
|
|
|
return $response->withStatus(404); |
|
42
|
|
|
} |
|
43
|
|
|
if(!is_object($user) && isset($user[0])) |
|
44
|
|
|
{ |
|
45
|
|
|
$user = $user[0]; |
|
46
|
|
|
} |
|
47
|
|
|
return $response->withJson($user); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function deletePendingUser($request, $response, $args) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->validateIsAdmin($request); |
|
53
|
|
|
$auth = \AuthProvider::getInstance(); |
|
54
|
|
|
$res = $auth->deletePendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'")); |
|
55
|
|
|
return $response->withJson($res); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function activatePendingUser($request, $response, $args) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$user = $request->getAttribute('user'); |
|
61
|
|
|
if($user === false) |
|
62
|
|
|
{ |
|
63
|
|
|
throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED); |
|
64
|
|
|
} |
|
65
|
|
|
$auth = \AuthProvider::getInstance(); |
|
66
|
|
|
$user = $auth->getPendingUsersByFilter(new \Data\Filter("hash eq '$hash'")); |
|
|
|
|
|
|
67
|
|
|
if($user === false || !isset($user[0])) |
|
68
|
|
|
{ |
|
69
|
|
|
return $app->withStatus(404); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
$res = $auth->activatePendingUser($user[0]); |
|
72
|
|
|
if($request->isGet()) |
|
73
|
|
|
{ |
|
74
|
|
|
$uri = '../../activate_error.php'; |
|
75
|
|
|
if($res) |
|
76
|
|
|
{ |
|
77
|
|
|
$uri = '../../'; |
|
78
|
|
|
} |
|
79
|
|
|
return $response->withStatus(302)->withHeader('Location', $uri); |
|
80
|
|
|
} |
|
81
|
|
|
else |
|
82
|
|
|
{ |
|
83
|
|
|
return $response->withJson($res); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
88
|
|
|
|
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.