Auth::resetLoginTries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Auth;
6
7
use Canvas\Models\Users;
8
use Exception;
9
use stdClass;
10
11
abstract class Auth
12
{
13
    /**
14
     * Check the user login attems to the app.
15
     *
16
     * @param Users $user
17
     * @throws Exception
18
     * @return void
19
     */
20
    protected static function loginAttempsValidation(Users $user): bool
21
    {
22
        //load config
23
        $config = new stdClass();
24
        $config->login_reset_time = getenv('AUTH_MAX_AUTOLOGIN_TIME');
25
        $config->max_login_attempts = getenv('AUTH_MAX_AUTOLOGIN_ATTEMPS');
26
27
        // If the last login is more than x minutes ago, then reset the login tries/time
28
        if ($user->user_last_login_try && $config->login_reset_time && $user->user_last_login_try < (time() - ($config->login_reset_time * 60))) {
29
            $user->user_login_tries = 0; //turn back to 0 attems, succes
30
            $user->user_last_login_try = 0;
31
            $user->updateOrFail();
32
        }
33
34
        // Check to see if user is allowed to login again... if his tries are exceeded
35
        if ($user->user_last_login_try
36
            && $config->login_reset_time
37
            && $config->max_login_attempts
38
            && $user->user_last_login_try >= (time() - ($config->login_reset_time * 60))
39
            && $user->user_login_tries >= $config->max_login_attempts) {
40
            throw new Exception(sprintf(_('You have exhausted all login attempts.'), $config->max_login_attempts));
41
        }
42
43
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
44
    }
45
46
    /**
47
     * Reset login tries.
48
     *
49
     * @param Users $user
50
     * @return boolean
51
     */
52
    protected static function resetLoginTries(Users $user): bool
53
    {
54
        $user->lastvisit = date('Y-m-d H:i:s');
55
        $user->user_login_tries = 0;
56
        $user->user_last_login_try = 0;
57
        return $user->updateOrFail();
58
    }
59
60
    /**
61
     * Update login tries for the given user.
62
     *
63
     * @return bool
64
     */
65
    protected static function updateLoginTries(Users $user): bool
66
    {
67
        if ($user->getId() != Users::ANONYMOUS) {
68
            $user->user_login_tries += 1;
69
            $user->user_last_login_try = time();
70
            return $user->updateOrFail();
71
        }
72
73
        return false;
74
    }
75
}
76