Auth   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 63
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resetLoginTries() 0 6 1
B loginAttempsValidation() 0 24 9
A updateLoginTries() 0 9 2
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