Completed
Push — master ( 032e4b...251c57 )
by Patrick
02:59
created

users.php ➔ userIsMe()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
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
    else
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 userIsMe($app, $uid)
102
{
103
    return ($uid === 'me' || $uid === $app->user->getUid());
104
}
105
106
function getUserByUIDReadOnly($app, $uid)
107
{
108
    if(userIsMe($app, $uid))
109
    {
110
        return $app->user;
111
    }
112
    if($app->user->isInGroupNamed('LDAPAdmins') || $app->user->isInGroupNamed('Leads') || $app->user->isInGroupNamed('CC'))
113
    {
114
        $auth = \AuthProvider::getInstance();
115
        $filter = new \Data\Filter("uid eq $uid");
116
        $users = $auth->getUsersByFilter($filter);
117
        if($users !== false && isset($users[0]))
118
        {
119
            return $users[0];
120
        }
121
    }
122
    return false;
123
}
124
125
function getUserByUID($app, $uid)
126
{
127
    if(userIsMe($app, $uid))
128
    {
129
        return $app->user;
130
    }
131 View Code Duplication
    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...
132
    {
133
        $auth = \AuthProvider::getInstance();
134
        $filter = new \Data\Filter("uid eq $uid");
135
        $users = $auth->getUsersByFilter($filter);
136
        if($users !== false && isset($users[0]))
137
        {
138
            return $users[0];
139
        }
140
    }
141
    return false;
142
}
143
144
function show_user($uid = 'me')
145
{
146
    global $app;
147
    if(!$app->user)
148
    {
149
        $app->response->setStatus(401);
150
        return;
151
    }
152
    $user = getUserByUIDReadOnly($app, $uid);
153
    if($user === false)
154
    {
155
        $app->halt(404);
156
    }
157
    if(!is_object($user) && isset($user[0]))
158
    {
159
        $user = $user[0];
160
    }
161
    if($app->fmt === 'vcard')
162
    {
163
        $app->response->headers->set('Content-Type', 'text/x-vCard');
164
        echo $user->getVcard();
165
        $app->fmt = 'passthru';
166
    }
167
    else
168
    {
169
        echo $user->serializeObject();
170
    }
171
}
172
173
function sendPasswordResetEmail($user)
174
{
175
    $forwardedFor = false;
176
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
177
    {
178
        $forwardedFor = $_SERVER['HTTP_X_FORWARDED_FOR'];
179
    }
180
    $emailMsg = new PasswordHasBeenResetEmail($user, $_SERVER['REMOTE_ADDR'], $forwardedFor);
181
    $emailProvider = EmailProvider::getInstance();
182
    if($emailProvider->sendEmail($emailMsg) === false)
183
    {
184
        throw new \Exception('Unable to send password reset email!');
185
    }
186
}
187
188
function exceptionCodeToHttpCode($e)
189
{
190
    if($e->getCode() === 3)
191
    {
192
        return 401;
193
    }
194
    return 500;
195
}
196
197
function edit_user($uid = 'me')
198
{
199
    global $app;
200
    $obj = $app->request->getJsonBody();
201
    $auth = AuthProvider::getInstance();
202
    if(!$app->user)
203
    {
204
        if(isset($obj->hash))
205
        {
206
            $app->user = $auth->getUserByResetHash($obj->hash);
207
        }
208
        if(!$app->user)
209
        {
210
            $app->response->setStatus(401);
211
            return;
212
        }
213
    }
214
    $user = getUserByUID($app, $uid);
215
    if($user === false)
216
    {
217
        $app->response->setStatus(404);
218
        return;
219
    }
220
    try
221
    {
222
        $user->editUser($obj);
223
    }
224
    catch(\Exception $e)
225
    {
226
        $app->response->setStatus(exceptionCodeToHttpCode($e));
227
        echo json_encode($e);
228
        return;
229
    }
230
    if(userIsMe($app, $uid))
231
    {
232
        \FlipSession::setUser($user);
233
    }
234
    if(isset($obj->password))
235
    {
236
        sendPasswordResetEmail($user);
237
    }
238
    echo json_encode(array('success'=>true));
239
}
240
241
function deleteUser($uid = 'me')
242
{
243
    global $app;
244
    if(!$app->user)
245
    {
246
        $app->response->setStatus(401);
247
        return;
248
    }
249
    $user = false;
250
    if(userIsMe($app, $uid))
251
    {
252
        $user = $app->user;
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
        $auth = AuthProvider::getInstance();
257
        $filter = new \Data\Filter("uid eq $uid");
258
        $user = $auth->getUsersByFilter($filter);
259
        if(isset($user[0]))
260
        {
261
            $user = $user[0];
262
        }
263
    }
264
    return $user->delete();
265
}
266
267
function list_groups_for_user($uid = 'me')
268
{
269
    global $app;
270
    if(!$app->user)
271
    {
272
        $app->response->setStatus(401);
273
        return;
274
    }
275
    $user = getUserByUID($app, $uid);
276
    if($user === false)
277
    {
278
        $app->response->setStatus(404);
279
        return;
280
    }
281
    $groups = $user->getGroups();
282
    if($groups === false)
283
    {
284
        echo json_encode(array());
285
    }
286
    else
287
    {
288
        echo json_encode($groups);
289
    }
290
}
291
292
function link_user($uid = 'me')
293
{
294
    global $app;
295
    if(!$app->user)
296
    {
297
        $app->response->setStatus(401);
298
        return;
299
    }
300
    $body = $app->request->getBody();
301
    $obj  = json_decode($body);
302
    if($uid === 'me' || $uid === $app->user->getUid())
303
    {
304
        $app->user->addLoginProvider($obj->provider);
305
        AuthProvider::getInstance()->impersonateUser($app->user);
306
    }
307
    else if($app->user->isInGroupNamed("LDAPAdmins"))
308
    {
309
        $user = AuthProvider::getInstance()->getUser($uid);
310
        if($user === false)
311
        {
312
            $app->response->setStatus(404);
313
            return;
314
        }
315
        $user->addLoginProvider($obj->provider);
316
    }
317
    else
318
    {
319
        $app->response->setStatus(404);
320
        return;
321
    }
322
    echo json_encode(array('success'=>true));
323
}
324
325
function getAllUsersByFilter($filter, &$pending)
326
{
327
    $auth = AuthProvider::getInstance();
328
    $user = $auth->getUsersByFilter($filter);
329 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...
330
    {
331
        $pending = false;
332
        return $user[0];
333
    }
334
    $user = $auth->getPendingUsersByFilter($filter);
335 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...
336
    {
337
        $pending = true;
338
        return $user[0];
339
    }
340
    return false;
341
}
342
343
function check_email_available()
344
{
345
    global $app;
346
    $email = $app->request->params('email');
347
    if(strpos($email, '@') === false)
348
    {
349
        //Not a valid email
350
        echo 'false';
351
    }
352
    if(strstr($email, '+') !== false)
353
    {
354
        //Remove everything between the + and the @
355
        $begining = strpos($email, '+');
356
        $end = strpos($email, '@');
357
        $to_delete = substr($email, $begining, $end - $begining);
358
        $email = str_replace($to_delete, '', $email);
359
    }
360
    $filter = new \Data\Filter('mail eq '.$email);
361
    $pending = false;
362
    $user = getAllUsersByFilter($filter, $pending);
363
    if($user === false)
364
    {
365
        echo 'true';
366
        return;
367
    }
368
    echo json_encode(array('res'=>false, 'email'=>$user->getEmail(), 'pending'=>$pending));
369
}
370
371
function check_uid_available()
372
{
373
    global $app;
374
    $uid = $app->request->params('uid');
375
    if(strpos($uid, '=') !== false || strpos($uid, ',') !== false)
376
    {
377
        return false;
378
    }
379
    $filter = new \Data\Filter('uid eq '.$uid);
380
    $pending = false;
381
    $user = getAllUsersByFilter($filter, $pending);
382
    if($user === false)
383
    {
384
        echo 'true';
385
        return;
386
    }
387
    echo json_encode(array('res'=>false, 'uidl'=>$user->getUid(), 'pending'=>$pending));
388
}
389
390 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...
391
{
392
    global $app;
393
    $auth = AuthProvider::getInstance();
394
    $users = $auth->getUsersByFilter(new \Data\Filter('uid eq '.$uid));
395
    if($users === false || !isset($users[0]))
396
    {
397
        $app->response->setStatus(404);
398
        return;
399
    }
400
    else
401
    {
402
        $email_msg = new PasswordResetEmail($users[0]);
403
        $email_provider = EmailProvider::getInstance();
404
        if($email_provider->sendEmail($email_msg) === false)
405
        {
406
            throw new \Exception('Unable to send email!');
407
        }
408
    }
409
}
410
411 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...
412
{
413
    global $app;
414
    $email = $app->request->params('email');
415
    $auth = AuthProvider::getInstance();
416
    $users = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$email));
417
    if($users === false || !isset($users[0]))
418
    {
419
        $app->response->setStatus(404);
420
        return;
421
    }
422
    else
423
    {
424
        $email_msg = new UIDForgotEmail($users[0]);
425
        $email_provider = EmailProvider::getInstance();
426
        if($email_provider->sendEmail($email_msg) === false)
427
        {
428
            throw new \Exception('Unable to send email!');
429
        }
430
    }
431
}
432
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
433
?>
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...
434