Completed
Push — master ( 19490a...c3f733 )
by Patrick
03:31
created

users.php ➔ validateCanCreateUser()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 10
Ratio 62.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 10
loc 16
rs 8.8571
1
<?php
2
require('class.UIDForgotEmail.php');
3
require('class.PasswordResetEmail.php');
4
require('class.PasswordHasBeenResetEmail.php');
5
6
function users()
7
{
8
    global $app;
9
    $app->get('', 'list_users');
10
    $app->post('', 'create_user');
11
    $app->get('/me', 'show_user');
12
    $app->get('/:uid', 'show_user');
13
    $app->patch('/:uid', 'edit_user');
14
    $app->delete('/:uid', 'deleteUser');
15
    $app->get('/me/groups', 'list_groups_for_user');
16
    $app->get('/:uid/groups', 'list_groups_for_user');
17
    $app->post('/me/Actions/link', 'link_user');
18
    $app->post('/:uid/Actions/link', 'link_user');
19
    $app->post('/:uid/Actions/reset_pass', 'reset_pass');
20
    $app->post('/Actions/check_email_available', 'check_email_available');
21
    $app->post('/Actions/check_uid_available', 'check_uid_available');
22
    $app->post('/Actions/remind_uid', 'remind_uid');
23
}
24
25
function list_users()
26
{
27
    global $app;
28
    if(!$app->user)
29
    {
30
        $app->response->setStatus(401);
31
        return;
32
    }
33
    if($app->user && !$app->user->isInGroupNamed("LDAPAdmins"))
34
    {
35
        //Only return this user. This user doesn't have access to other accounts
36
        echo json_encode(array($app->user));
37
    }
38 View Code Duplication
    else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
    {
40
        $auth = AuthProvider::getInstance();
41
        $users = $auth->getUsersByFilter($app->odata->filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
42
        echo json_encode($users);
43
    }
44
}
45
46
function validateCanCreateUser($proposedUser, $auth, &$message)
47
{
48
    $user = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$proposedUser->mail));
49 View Code Duplication
    if($user !== false && isset($user[0]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
    {
51
        $message = 'Email already exists!';
52
        return false;
53
    }
54
    $user = $auth->getUsersByFilter(new \Data\Filter('uid eq '.$proposedUser->uid));
55 View Code Duplication
    if($user !== false && isset($user[0]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
    {
57
        $message = 'Username already exists!';
58
        return false;
59
    }
60
    return true;
61
}
62
63
function create_user()
64
{
65
    global $app;
66
    //This one is different. If they are logged in fail...
67
    if($app->user)
68
    {
69
        $app->response->setStatus(404);
70
        return;
71
    }
72
    $body = $app->request->getBody();
73
    $obj  = json_decode($body);
74
    if(!isset($obj->captcha))
75
    {
76
        $app->response->setStatus(401);
77
        return;
78
    }
79
    $captcha = FlipSession::getVar('captcha');
80
    if($captcha === false)
81
    {
82
        $app->response->setStatus(401);
83
        return;
84
    }
85
    if(!$captcha->is_answer_right($obj->captcha))
86
    {
87
        echo json_encode(array('res'=>false, 'message'=>'Incorrect answer to CAPTCHA!'));
88
        return;
89
    }
90
    $auth = AuthProvider::getInstance();
91
    $message = false;
92
    if(validateCanCreateUser($obj, $auth, $message) === false)
93
    {
94
        echo json_encode(array('res'=>false, 'message'=>$message));
95
        return;
96
    }
97
    $ret = $auth->createPendingUser($obj);
98
    echo json_encode($ret);
99
}
100
101
function show_user($uid = 'me')
102
{
103
    global $app;
104
    if(!$app->user)
105
    {
106
        $app->response->setStatus(401);
107
        return;
108
    }
109
    $user = false;
110
    if($uid === 'me' || $uid === $app->user->getUid())
111
    {
112
        $user = $app->user;
113
    }
114
    else if($app->user->isInGroupNamed("LDAPAdmins"))
115
    {
116
        $user = \AuthProvider::getInstance()->getUsersByFilter(new \Data\Filter("uid eq $uid"));
117
    }
118
    else if($app->user->isInGroupNamed("Leads") || $app->user->isInGroupNamed("CC"))
119
    {
120
        $user = \AuthProvider::getInstance()->getUsersByFilter(new \Data\Filter("uid eq $uid"));
121
    }
122
    if($user === false)
123
    {
124
        $app->halt(404);
125
    }
126
    if(!is_object($user) && isset($user[0]))
127
    {
128
        $user = $user[0];
129
    }
130
    if($app->fmt === 'vcard')
131
    {
132
        $app->response->headers->set('Content-Type', 'text/x-vCard');
133
        echo $user->getVcard();
134
        $app->fmt = 'passthru';
135
    }
136
    else
137
    {
138
        echo $user->serializeObject();
139
    }
140
}
141
142
function edit_user($uid = 'me')
143
{
144
    global $app;
145
    $body = $app->request->getBody();
146
    $obj  = json_decode($body);
147
    $auth = AuthProvider::getInstance();
148
    if(!$app->user)
149
    {
150
        if(isset($obj->hash))
151
        {
152
            $app->user = $auth->getUserByResetHash($obj->hash);
153
        }
154
        if(!$app->user)
155
        {
156
            $app->response->setStatus(401);
157
            return;
158
        }
159
    }
160
    $user = false;
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
    if($uid === 'me' || $uid === $app->user->getUid())
162
    {
163
        try
164
        {
165
            $app->user->editUser($obj);
166
        }
167
        catch(\Exception $e)
168
        {
169
            if($e->getCode() === 3)
170
            {
171
                $app->response->setStatus(401);
172
                echo json_encode($e);
173
            }
174
            else
175
            {
176
                $app->response->setStatus(500);
177
                echo json_encode($e);
178
            }
179
        }
180
        $user = $app->user;
181
        \FlipSession::setUser($user);
182
    }
183
    else if($app->user->isInGroupNamed("LDAPAdmins"))
184
    {
185
        $user = $auth->getUsersByFilter(new \Data\Filter("uid eq $uid"));
186
        if($user === false || !isset($user[0]))
187
        {
188
            $app->response->setStatus(404);
189
            return;
190
        }
191
        $user[0]->editUser($obj);
192
    }
193
    else
194
    {
195
        $app->response->setStatus(404);
196
        return;
197
    }
198
    if(isset($obj->password))
199
    {
200
        $forwarded_for = false;
201
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
202
        {
203
            $forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
204
        }
205
        $email_msg = new PasswordHasBeenResetEmail($user, $_SERVER['REMOTE_ADDR'], $forwarded_for);
206
        $email_provider = EmailProvider::getInstance();
207
        if($email_provider->sendEmail($email_msg) === false)
208
        {
209
            throw new \Exception('Unable to send password reset email!');
210
        }
211
    }
212
    echo json_encode(array('success'=>true));
213
}
214
215
function deleteUser($uid = 'me')
216
{
217
    global $app;
218
    if(!$app->user)
219
    {
220
        $app->response->setStatus(401);
221
        return;
222
    }
223
    $user = false;
224
    if($uid === 'me' || $uid === $app->user->getUid())
225
    {
226
        $user = $app->user;
227
    }
228
    else if($app->user->isInGroupNamed("LDAPAdmins"))
229
    {
230
        $auth = AuthProvider::getInstance();
231
        $filter = new \Data\Filter("uid eq $uid");
232
        $user = $auth->getUsersByFilter($filter);
233
        if(isset($user[0]))
234
        {
235
            $user = $user[0];
236
        }
237
    }
238
    return $user->delete();
239
}
240
241
function list_groups_for_user($uid = 'me')
242
{
243
    global $app;
244
    if(!$app->user)
245
    {
246
        $app->response->setStatus(401);
247
        return;
248
    }
249
    $groups = false;
0 ignored issues
show
Unused Code introduced by
$groups is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
250
    if($uid === 'me' || $uid === $app->user->getUid())
251
    {
252
        $groups = $app->user->getGroups();
253
    }
254 View Code Duplication
    else if($app->user->isInGroupNamed("LDAPAdmins"))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
255
    {
256
        $user = AuthProvider::getInstance()->getUser($uid);
257
        if($user === false)
258
        {
259
            $app->response->setStatus(404);
260
            return;
261
        }
262
        $groups = $user->getGroups();
263
    }
264
    else
265
    {
266
        $app->response->setStatus(404);
267
        return;
268
    }
269
    if($groups === false)
270
    {
271
        echo json_encode(array());
272
    }
273
    else
274
    {
275
        echo json_encode($groups);
276
    }
277
}
278
279
function link_user($uid = 'me')
280
{
281
    global $app;
282
    if(!$app->user)
283
    {
284
        $app->response->setStatus(401);
285
        return;
286
    }
287
    $body = $app->request->getBody();
288
    $obj  = json_decode($body);
289
    if($uid === 'me' || $uid === $app->user->getUid())
290
    {
291
        $app->user->addLoginProvider($obj->provider);
292
        AuthProvider::getInstance()->impersonateUser($app->user);
293
    }
294 View Code Duplication
    else if($app->user->isInGroupNamed("LDAPAdmins"))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
295
    {
296
        $user = AuthProvider::getInstance()->getUser($uid);
297
        if($user === false)
298
        {
299
            $app->response->setStatus(404);
300
            return;
301
        }
302
        $user->addLoginProvider($obj->provider);
303
    }
304
    else
305
    {
306
        $app->response->setStatus(404);
307
        return;
308
    }
309
    echo json_encode(array('success'=>true));
310
}
311
312
function check_email_available()
313
{
314
    global $app;
315
    $email = $app->request->params('email');
316
    if(strpos($email, '@') === false)
317
    {
318
        //Not a valid email
319
        echo 'false';
320
    }
321
    if(strstr($email, '+') !== false)
322
    {
323
        //Remove everything between the + and the @
324
        $begining = strpos($email, '+');
325
        $end = strpos($email, '@');
326
        $to_delete = substr($email, $begining, $end - $begining);
327
        $email = str_replace($to_delete, '', $email);
328
    }
329
    $auth = AuthProvider::getInstance();
330
    $filter = new \Data\Filter('mail eq '.$email);
331
    $user = $auth->getUsersByFilter($filter);
332 View Code Duplication
    if($user === false || !isset($user[0]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
333
    {
334
        $user = $auth->getPendingUsersByFilter($filter);
335
        if($user === false || !isset($user[0]))
336
        {
337
            echo 'true';
338
        }
339
        else
340
        {
341
            echo json_encode(array('res'=>false, 'email'=>$user[0]->getEmail(), 'pending'=>true));
342
        }
343
    }
344
    else
345
    {
346
        echo json_encode(array('res'=>false, 'email'=>$user[0]->getEmail()));
347
    }
348
}
349
350
function check_uid_available()
351
{
352
    global $app;
353
    $uid = $app->request->params('uid');
354
    if(strpos($uid, '=') !== false || strpos($uid, ',') !== false)
355
    {
356
        return false;
357
    }
358
    $auth = AuthProvider::getInstance();
359
    $filter = new \Data\Filter('uid eq '.$uid);
360
    $user = $auth->getUsersByFilter($filter);
361 View Code Duplication
    if($user === false || !isset($user[0]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
362
    {
363
        $user = $auth->getPendingUsersByFilter($filter);
364
        if($user === false || !isset($user[0]))
365
        {
366
            echo 'true';
367
        }
368
        else
369
        {
370
            echo json_encode(array('res'=>false, 'uid'=>$user[0]->getUid(), 'pending'=>true));
371
        }
372
    }
373
    else
374
    {
375
        echo json_encode(array('res'=>false, 'uid'=>$user[0]->getUid()));
376
    }
377
}
378
379 View Code Duplication
function reset_pass($uid)
0 ignored issues
show
Duplication introduced by
This function 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...
380
{
381
    global $app;
382
    $auth = AuthProvider::getInstance();
383
    $users = $auth->getUsersByFilter(new \Data\Filter('uid eq '.$uid));
384
    if($users === false || !isset($users[0]))
385
    {
386
        $app->response->setStatus(404);
387
        return;
388
    }
389
    else
390
    {
391
        $email_msg = new PasswordResetEmail($users[0]);
392
        $email_provider = EmailProvider::getInstance();
393
        if($email_provider->sendEmail($email_msg) === false)
394
        {
395
            throw new \Exception('Unable to send email!');
396
        }
397
    }
398
}
399
400 View Code Duplication
function remind_uid()
0 ignored issues
show
Duplication introduced by
This function 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...
401
{
402
    global $app;
403
    $email = $app->request->params('email');
404
    $auth = AuthProvider::getInstance();
405
    $users = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$email));
406
    if($users === false || !isset($users[0]))
407
    {
408
        $app->response->setStatus(404);
409
        return;
410
    }
411
    else
412
    {
413
        $email_msg = new UIDForgotEmail($users[0]);
414
        $email_provider = EmailProvider::getInstance();
415
        if($email_provider->sendEmail($email_msg) === false)
416
        {
417
            throw new \Exception('Unable to send email!');
418
        }
419
    }
420
}
421
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
422
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
423