LoginForm::getUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
namespace zacksleo\yii2\backend\models\forms;
4
5
use yii;
6
use yii\base\Model;
7
use zacksleo\yii2\backend\models\Admin;
8
use zacksleo\yii2\backend\Module;
9
10
/**
11
 * LoginForm is the model behind the login form.
12
 */
13
class LoginForm extends Model
14
{
15
    public $username;
16
    public $password;
17
    public $rememberMe = true;
18
19
    private $_user = false;
20
21
    /**
22
     * @return array the validation rules.
23
     */
24 1
    public function rules()
25
    {
26
        return [
27
            // username and password are both required
28 1
            [['username', 'password'], 'required'],
29
            // rememberMe must be a boolean value
30
            ['rememberMe', 'boolean'],
31
            // password is validated by validatePassword()
32
            ['password', 'validatePassword'],
33
        ];
34
    }
35
36
    /**
37
     * Validates the password.
38
     * This method serves as the inline validation for password.
39
     */
40 1
    public function validatePassword()
41
    {
42 1
        if (!$this->hasErrors()) {
43 1
            $user = $this->getUser();
44 1
            if (!$user || !$user->validatePassword($this->password)) {
45 1
                $this->addError('password', Module::t('backend', 'Incorrect username or password'));
46
            }
47
        }
48 1
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function attributeLabels()
54
    {
55
        return [
56 1
            'username' => Module::t('backend', 'username'),
57 1
            'password' => Module::t('backend', 'password'),
58
        ];
59
    }
60
61
    /**
62
     * Logs in a user using the provided username and password.
63
     * @return boolean whether the user is logged in successfully
64
     */
65 1
    public function login()
66
    {
67 1
        if ($this->validate()) {
68
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
69
        } else {
70 1
            return false;
71
        }
72
    }
73
74
    /**
75
     * Finds user by [[username]]
76
     *
77
     * @return Admin|null
78
     */
79 1
    public function getUser()
80
    {
81 1
        if ($this->_user === false) {
82
            /* @var $class Admin */
83 1
            $class = Yii::$app->user->identityClass;
84 1
            $this->_user = $class::find()->canLogin()->username($this->username)->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like $class::find()->canLogin...$this->username)->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $_user is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
85
        }
86
87 1
        return $this->_user;
88
    }
89
}
90