Completed
Push — master ( c80ee3...032e4b )
by Patrick
03:06
created

users.php ➔ sendPasswordResetEmail()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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