App   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 76
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updatePassword() 0 14 2
B login() 0 40 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Auth;
6
7
use Canvas\Models\Users;
8
use Exception;
9
use Phalcon\Di;
10
use RuntimeException;
11
use Canvas\Hashing\Password;
12
13
class App extends Auth
14
{
15
    /**
16
     * User login.
17
     *
18
     * @param string $email
19
     * @param string $password
20
     * @param integer $autologin
21
     * @param integer $admin
22
     * @param string $userIp
23
     * @return Users
24
     */
25
    public static function login(string $email, string $password, int $autologin = 1, int $admin, string $userIp) : Users
0 ignored issues
show
Unused Code introduced by
The parameter $userIp is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
    public static function login(string $email, string $password, int $autologin = 1, int $admin, /** @scrutinizer ignore-unused */ string $userIp) : Users

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $admin is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
    public static function login(string $email, string $password, int $autologin = 1, /** @scrutinizer ignore-unused */ int $admin, string $userIp) : Users

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $autologin is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
    public static function login(string $email, string $password, /** @scrutinizer ignore-unused */ int $autologin = 1, int $admin, string $userIp) : Users

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        //trim email
28
        $email = ltrim(trim($email));
29
        $password = ltrim(trim($password));
30
31
        //if its a email lets by it by email, if not by displayname
32
        $user = Users::getByEmail($email);
33
34
        //first we find the user
35
        if (!$user) {
36
            throw new Exception(_('Invalid Username or Password.'));
37
        }
38
39
        self::loginAttempsValidation($user);
40
41
        //check if the user exist on this app
42
        $currentAppUserInfo = $user->getApp();
43
44
        if (!is_object($currentAppUserInfo) || empty($currentAppUserInfo->password)) {
45
            throw new Exception(_('Invalid Username or Password.'));
46
        }
47
48
        //password verification
49
        if (Password::check($password, $currentAppUserInfo->password) && $user->isActive()) {
50
            //rehash password
51
            Password::rehash($password, $currentAppUserInfo);
52
53
            // Reset login tries
54
            self::resetLoginTries($user);
55
            return $user;
56
        } elseif ($user->isActive()) {
57
            // Only store a failed login attempt for an active user - inactive users can't login even with a correct password
58
            self::updateLoginTries($user);
59
60
            throw new Exception(_('Invalid Username or Password..'));
61
        } elseif ($user->isBanned()) {
62
            throw new Exception(_('User has not been banned, please check your email for the activation link.'));
63
        } else {
64
            throw new Exception(_('User has not been activated, please check your email for the activation link.'));
65
        }
66
    }
67
68
    /**
69
     * Update the password for the current app of all the companies, FOR NOW.
70
     *
71
     * @param Users $user
72
     * @param string $password
73
     * @return bool
74
     */
75
    public static function updatePassword(Users $user, string $password): bool
76
    {
77
        $app = Di::getDefault()->getApp();
78
79
        $userApps = $user->getApps([
80
            'conditions' => 'apps_id = ?0',
81
            'bind' => [$app->getId()]
82
        ]);
83
84
        if (is_object($userApps)) {
85
            $userApps->update(['password' => $password]);
86
        }
87
88
        return true;
89
    }
90
}
91