Completed
Push — master ( a4fdfe...a232d7 )
by Joram van den
04:04
created

UserController::profile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
class UserController extends Ajde_User_Controller
4
{
5
    protected $_allowedActions = [
6
        'forgot',
7
        'reset',
8
        'logon',
9
        'logoff',
10
        'register',
11
        'keepalive'
12
    ];
13
    protected $_logonRoute     = 'user/logon/html';
14
    protected $includeDomain   = false;
15
16
    public function beforeInvoke()
17
    {
18
        $adminAccess = false; //Ajde_Acl::validatePage('admin', '', '');
19
        if (
20
            (isset($_GET['_route']) && substr($_GET['_route'], 0, 5) == 'admin') ||
21
            (isset($_GET['returnto']) && substr($_GET['returnto'], 0, 5) == 'admin') ||
22
            $adminAccess
23
        ) {
24
            Ajde::app()->getDocument()->setLayout(new Ajde_Layout(Config::get('adminLayout')));
25
        }
26
        Ajde_Cache::getInstance()->disable();
27
28
        return parent::beforeInvoke();
29
    }
30
31
    // Default to profile
32
    public function view()
33
    {
34
        return $this->profile();
35
    }
36
37
    public function menu()
38
    {
39
        return $this->render();
40
    }
41
42
    public function app()
43
    {
44
        $user = $this->getLoggedInUser();
45
        $this->getView()->assign('user', $user);
46
47
        return $this->render();
48
    }
49
50
    public function keepaliveJson()
51
    {
52
        return ['success' => true];
53
    }
54
55
    // Profile
56
    public function profile()
57
    {
58
        $user = $this->getLoggedInUser();
59
        $this->setAction('profile');
0 ignored issues
show
Documentation Bug introduced by
The method setAction does not exist on object<UserController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60
        $user->refresh();
61
        $user->login();
62
        $this->getView()->assign('user', $user);
63
64
        return $this->render();
65
    }
66
67
    public function social()
68
    {
69
        $user = $this->getLoggedInUser();
70
        $this->getView()->assign('sso', Config::get('ssoProviders'));
71
        $this->getView()->assign('user', $user);
72
73
        return $this->render();
74
    }
75
76
    // Settings
77
    public function settingsHtml()
78
    {
79
        $user = $this->getLoggedInUser();
80
        $this->getView()->assign('user', $user);
81
82
        return $this->render();
83
    }
84
85
    public function settingsJson()
86
    {
87
        $user = $this->getLoggedInUser();
88
89
        if (!$user) {
90
            $return = [
0 ignored issues
show
Unused Code introduced by
$return 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...
91
                'success' => false,
92
                'message' => __("Not logged in")
93
            ];
94
        }
95
96
        $returnto = 'user/profile';
97
98
        $username      = Ajde::app()->getRequest()->getPostParam($user->usernameField);
99
        $password      = Ajde::app()->getRequest()->getPostParam('password');
100
        $passwordCheck = Ajde::app()->getRequest()->getPostParam('passwordCheck');
101
        $email         = Ajde::app()->getRequest()->getPostParam('email', false);
102
        $fullname      = Ajde::app()->getRequest()->getPostParam('fullname', false);
103
104
        $return = [false];
0 ignored issues
show
Unused Code introduced by
$return 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...
105
106
        if (empty($username)) {
107
            $return = [
108
                'success' => false,
109
                'message' => __("Please provide a " . $user->usernameField)
110
            ];
111
        } else {
112
            if (!$user->canChangeUsernameTo($username)) {
113
                $return = [
114
                    'success' => false,
115
                    'message' => __(ucfirst($user->usernameField) . " already exist")
116
                ];
117
            } else {
118
                if ($password && $password !== $passwordCheck) {
119
                    $return = [
120
                        'success' => false,
121
                        'message' => __("Passwords do not match")
122
                    ];
123
                } else {
124
                    if (empty($email)) {
125
                        $return = [
126
                            'success' => false,
127
                            'message' => __("Please provide an e-mail address")
128
                        ];
129
                    } else {
130
                        if (Ajde_Component_String::validEmail($email) === false) {
131
                            $return = [
132
                                'success' => false,
133
                                'message' => __('Please provide a valid e-mail address')
134
                            ];
135
                        } else {
136
                            if (!$user->canChangeEmailTo($email)) {
137
                                $return = [
138
                                    'success' => false,
139
                                    'message' => __("A user with this e-mail address already exist")
140
                                ];
141
                            } else {
142
                                if (empty($fullname)) {
143
                                    $return = [
144
                                        'success' => false,
145
                                        'message' => __("Please provide a full name")
146
                                    ];
147
                                } else {
148
                                    $user->set($user->usernameField, $username);
149
                                    $user->set('email', $email);
150
                                    $user->set('fullname', $fullname);
151
                                    if ($password) {
152
                                        $hash = $user->createHash($password);
153
                                        $user->set($user->passwordField, $hash);
154
                                    }
155 View Code Duplication
                                    if ($user->save()) {
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...
156
                                        Ajde_Session_Flash::alert(__('Your settings have been saved'));
157
                                        $return = [
158
                                            'success'  => true,
159
                                            'returnto' => $returnto
160
                                        ];
161
                                    } else {
162
                                        $return = [
163
                                            'success' => false,
164
                                            'message' => __("Something went wrong")
165
                                        ];
166
                                    }
167
                                }
168
                            }
169
                        }
170
                    }
171
                }
172
            }
173
        }
174
175
        return $return;
176
    }
177
178
    // Logon
179
    public function logonHtml()
180
    {
181
        if (($user = $this->getLoggedInUser())) {
182
            if (($returnto = Ajde::app()->getRequest()->getParam('returnto', false))) {
183
                return $this->redirect($returnto);
184
            }
185
            $this->setAction('relogon');
0 ignored issues
show
Documentation Bug introduced by
The method setAction does not exist on object<UserController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
186
            $message = Ajde::app()->getRequest()->getParam('message', '');
187
            $this->getView()->assign('message', $message);
188
            $this->getView()->assign('user', $user);
189
        } else {
190
            $user = new UserModel();
191
            $this->setAction('logon');
0 ignored issues
show
Documentation Bug introduced by
The method setAction does not exist on object<UserController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
192
            //			$message = Ajde::app()->getRequest()->getParam('message', 'Please login');
193
            $this->getView()->assign('message', '');
194
            $this->getView()->assign('user', $user);
195
            $this->getView()->assign('returnto',
196
                Ajde::app()->getRequest()->getParam('returnto', $_SERVER['REDIRECT_STATUS'] == 200 ? 'user' : false));
197
        }
198
        $this->getView()->assign('sso', Config::get('ssoProviders'));
199
200
        return $this->render();
201
    }
202
203
    public function logonJson()
204
    {
205
        $user = new UserModel();
206
207
        $username   = Ajde::app()->getRequest()->getPostParam($user->usernameField);
208
        $password   = Ajde::app()->getRequest()->getPostParam('password');
209
        $rememberme = Ajde::app()->getRequest()->hasPostParam('rememberme');
210
211
        $return = [false];
0 ignored issues
show
Unused Code introduced by
$return 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...
212
213
        if (false !== $user->loadByCredentials($username, $password)) {
214
            $user->login();
215
            Ajde_Session_Flash::alert(sprintf(__('Welcome back %s'), $user->getFullname()));
216
            if ($rememberme === true) {
217
                $user->storeCookie($this->includeDomain);
218
            }
219
            $return = ['success' => true];
220 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...
221
            $session  = new Ajde_Session('user');
222
            $attempts = $session->has('attempts') ? $session->get('attempts') : 1;
223
            $session->set('attempts', $attempts + 1);
224
            if ($attempts % 4 === 0) {
225
                sleep(5);
226
            }
227
            $return = [
228
                'success' => false,
229
                'message' => __("We could not log you in with these credentials")
230
            ];
231
        }
232
233
        return $return;
234
    }
235
236
    // Reset
237
    public function forgotHtml()
238
    {
239
        return $this->render();
240
    }
241
242
    public function forgotJson()
243
    {
244
        $user = new UserModel();
245
246
        $ident  = Ajde::app()->getRequest()->getPostParam('user');
247
        $found  = false;
248
        $return = [false];
0 ignored issues
show
Unused Code introduced by
$return 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...
249
250
        if (false !== $user->loadByField('email', $ident)) {
251
            $found = true;
252
        }
253
        if (false === $found && false !== $user->loadByField($user->usernameField, $ident)) {
254
            $found = true;
255
        }
256
257
        if (false !== $found) {
258
            if ($user->resetUser()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->resetUser() of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
259
                Ajde_Session_Flash::alert(__('A password reset link is sent to your e-mail address.'));
260
                $return = ['success' => true];
261
            } else {
262
                $return = [
263
                    'success' => false,
264
                    'message' => __("We could not reset your password. Please contact our technical staff.")
265
                ];
266
            }
267 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...
268
            $session  = new Ajde_Session('user');
269
            $attempts = $session->has('attempts') ? $session->get('attempts') : 1;
270
            $session->set('attempts', $attempts + 1);
271
            if ($attempts % 4 === 0) {
272
                sleep(5);
273
            }
274
            $return = [
275
                'success' => false,
276
                'message' => __("No matching user found")
277
            ];
278
        }
279
280
        return $return;
281
    }
282
283
    public function resetHtml()
284
    {
285
        $resetHash = Ajde::app()->getRequest()->getParam('h', false);
286
        if (!$resetHash) {
287
            return $this->render();
288
        }
289
290
        $resetArray = explode(':', $resetHash);
291
        $timestamp  = $resetArray[0];
292
        if (time() > $timestamp) {
293
            return $this->render();
294
        }
295
296
        $user  = new UserModel();
297
        $found = $user->loadByField('reset_hash', $resetHash);
298
        if (!$found) {
299
            return $this->render();
300
        }
301
302
        $user->login();
303
304
        $this->getView()->assign('user', $user);
305
306
        return $this->render();
307
    }
308
309
    public function resetJson()
310
    {
311
        $user = $this->getLoggedInUser();
312
313
        $password      = Ajde::app()->getRequest()->getPostParam('password');
314
        $passwordCheck = Ajde::app()->getRequest()->getPostParam('passwordCheck');
315
316
        $return = [false];
0 ignored issues
show
Unused Code introduced by
$return 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...
317
318
        $shadowUser = new UserModel();
0 ignored issues
show
Unused Code introduced by
$shadowUser 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...
319
320
        if (empty($password)) {
321
            $return = [
322
                'success' => false,
323
                'message' => __("Please provide a password")
324
            ];
325
        } else {
326
            if ($password !== $passwordCheck) {
327
                $return = [
328
                    'success' => false,
329
                    'message' => __("Passwords do not match")
330
                ];
331
            } else {
332
                $hash = $user->createHash($password);
333
                $user->set($user->passwordField, $hash);
334
                $user->set('secret', $user->generateSecret());
335
                $user->set('reset_hash', '');
336
337 View Code Duplication
                if ($user->save()) {
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...
338
                    $user->login();
339
                    Ajde_Session_Flash::alert(sprintf(__('Welcome %s, you are now logged in'), $user->getFullname()));
340
                    $return = [
341
                        'success' => true
342
                    ];
343
                } else {
344
                    $return = [
345
                        'success' => false,
346
                        'message' => __("Something went wrong")
347
                    ];
348
                }
349
            }
350
        }
351
352
        return $return;
353
    }
354
355
    // Logoff
356
    public function logoff()
357
    {
358
        if (($user = $this->getLoggedInUser())) {
359
            $user->logout();
360
        }
361
        if (($returnto = Ajde::app()->getRequest()->getParam('returnto', false))) {
362
            $this->redirect($returnto);
363
        } elseif (substr_count(Ajde_Http_Request::getRefferer(), 'logoff') > 0 || !Ajde_Http_Request::getRefferer()) {
364
            $this->redirect('user');
365
        } else {
366
            $this->redirect(Ajde_Http_Response::REDIRECT_REFFERER);
367
        }
368
    }
369
370
    public function switchUser()
371
    {
372
        if (($user = $this->getLoggedInUser())) {
373
            $user->logout();
374
            $this->_user = null;
375
        }
376
377
        return $this->logonHtml();
378
    }
379
380
    public function registerHtml()
381
    {
382
        $user = new UserModel();
383
        $this->getView()->assign('returnto', Ajde::app()->getRequest()->getParam('returnto', false));
384
        $this->getView()->assign('username', Ajde::app()->getRequest()->getParam('username', false));
385
        $this->getView()->assign('email', Ajde::app()->getRequest()->getParam('email', false));
386
        $this->getView()->assign('fullname', Ajde::app()->getRequest()->getParam('fullname', false));
387
        $this->getView()->assign('provider', Ajde::app()->getRequest()->getParam('provider', false));
388
        $this->getView()->assign('hidepassword', Ajde::app()->getRequest()->getParam('hidepassword', 0));
389
        $this->getView()->assign('user', $user);
390
391
        return $this->render();
392
    }
393
394
    public function registerJson()
395
    {
396
        $user = new UserModel();
397
398
        $returnto = Ajde::app()->getRequest()->getPostParam('returnto', false);
399
400
        $username      = Ajde::app()->getRequest()->getPostParam($user->usernameField);
401
        $password      = Ajde::app()->getRequest()->getPostParam('password', '');
402
        $passwordCheck = Ajde::app()->getRequest()->getPostParam('passwordCheck', '');
403
        $providername  = Ajde::app()->getRequest()->getPostParam('provider', false);
404
        $email         = Ajde::app()->getRequest()->getPostParam('email', false);
405
        $fullname      = Ajde::app()->getRequest()->getPostParam('fullname', false);
406
407
        $return = [false];
0 ignored issues
show
Unused Code introduced by
$return 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...
408
409
        $shadowUser = new UserModel();
410
411
        $provider = false;
412
        if ($providername) {
413
            $sso = Config::get('ssoProviders');
414
            if (!in_array($providername, $sso)) {
415
                Ajde_Http_Response::redirectNotFound();
416
            }
417
418
            $classname = 'Ajde_User_Sso_' . ucfirst($providername);
419
            /* @var $provider Ajde_User_SSO_Interface */
420
            $provider = new $classname;
421
        }
422
423
        if (empty($username)) {
424
            $return = [
425
                'success' => false,
426
                'message' => __("Please provide a " . $user->usernameField . "")
427
            ];
428
        } else {
429
            if (!$provider && empty($password)) {
430
                $return = [
431
                    'success' => false,
432
                    'message' => __("Please provide a password")
433
                ];
434
            } else {
435
                if ($shadowUser->loadByField($shadowUser->usernameField, $username)) {
436
                    $return = [
437
                        'success' => false,
438
                        'message' => __(ucfirst($user->usernameField) . " already exist")
439
                    ];
440
                } else {
441
                    if (!$provider && $password !== $passwordCheck) {
442
                        $return = [
443
                            'success' => false,
444
                            'message' => __("Passwords do not match")
445
                        ];
446
                    } else {
447
                        if (empty($email)) {
448
                            $return = [
449
                                'success' => false,
450
                                'message' => __("Please provide an e-mail address")
451
                            ];
452
                        } else {
453
                            if (Ajde_Component_String::validEmail($email) === false) {
454
                                $return = [
455
                                    'success' => false,
456
                                    'message' => __('Please provide a valid e-mail address')
457
                                ];
458
                            } else {
459
                                if ($shadowUser->loadByField('email', $email)) {
460
                                    $return = [
461
                                        'success' => false,
462
                                        'message' => __("A user with this e-mail address already exist")
463
                                    ];
464
                                } else {
465
                                    if (empty($fullname)) {
466
                                        $return = [
467
                                            'success' => false,
468
                                            'message' => __("Please provide a full name")
469
                                        ];
470
                                    } else {
471
                                        if ($provider && !$provider->getData()) {
472
                                            $return = [
473
                                                'success' => false,
474
                                                'message' => __("Something went wrong with fetching your credentials from an external service")
475
                                            ];
476
                                        } else {
477
                                            $user->set('email', $email);
478
                                            $user->set('fullname', $fullname);
479
                                            if ($user->add($username, $password)) {
480
                                                if ($provider) {
481
                                                    $sso = new SsoModel();
482
                                                    $sso->populate([
483
                                                        'user'     => $user->getPK(),
484
                                                        'provider' => $providername,
485
                                                        'username' => $provider->getUsernameSuggestion(),
486
                                                        'avatar'   => $provider->getAvatarSuggestion(),
487
                                                        'profile'  => $provider->getProfileSuggestion(),
488
                                                        'uid'      => $provider->getUidHash(),
489
                                                        'data'     => serialize($provider->getData())
490
                                                    ]);
491
                                                    $sso->insert();
492
                                                    $user->copyAvatarFromSso($sso);
493
                                                }
494
                                                $user->login();
495
                                                $user->storeCookie($this->includeDomain);
496
                                                Ajde_Session_Flash::alert(sprintf(__('Welcome %s, you are now logged in'),
497
                                                    $fullname));
498
                                                $return = [
499
                                                    'success'  => true,
500
                                                    'returnto' => $returnto
501
                                                ];
502
                                            } else {
503
                                                $return = [
504
                                                    'success' => false,
505
                                                    'message' => __("Something went wrong")
506
                                                ];
507
                                            }
508
                                        }
509
                                    }
510
                                }
511
                            }
512
                        }
513
                    }
514
                }
515
            }
516
        }
517
518
        return $return;
519
    }
520
}
521