Passed
Push — master ( 36be2d...32b990 )
by Carlos
02:58
created

User::setPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use OAuth2\Storage\UserCredentialsInterface;
6
use roaresearch\yii2\oauth2server\{
7
    RevokeAccessTokenInterface,
8
    RevokeAccessTokenTrait
0 ignored issues
show
Bug introduced by
The type roaresearch\yii2\oauth2s...\RevokeAccessTokenTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
};
10
use Yii;
11
use yii\web\IdentityInterface;
12
13
class User extends \yii\db\ActiveRecord implements
14
    UserCredentialsInterface,
15
    IdentityInterface,
16
    RevokeAccessTokenInterface
17
{
18
    use RevokeAccessTokenTrait;
19
20
    public static function tableName()
21
    {
22
        return '{{%user}}';
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public static function findIdentity($id)
29
    {
30
        return static::findOne(['id' => $id]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(array('id' => $id)) returns the type yii\db\ActiveRecord which is incompatible with the return type mandated by yii\web\IdentityInterface::findIdentity() of null|yii\web\IdentityInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
31
    }
32
33
    /**
34
     * Finds user by username
35
     *
36
     * @param string $username
37
     * @return static|null
38
     */
39
    public static function findByUsername($username)
40
    {
41
        return static::findOne(['username' => $username]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(a...sername' => $username)) returns the type yii\db\ActiveRecord which is incompatible with the documented return type app\models\User|null.
Loading history...
42
    }
43
44
    /**
45
     * Finds user by password reset token
46
     *
47
     * @param string $token password reset token
48
     * @return static|null
49
     */
50
    public static function findByPasswordResetToken($token)
51
    {
52
        if (!static::isPasswordResetTokenValid($token)) {
53
            return null;
54
        }
55
56
        return static::findOne([
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(a...> self::STATUS_ACTIVE)) returns the type yii\db\ActiveRecord which is incompatible with the documented return type app\models\User|null.
Loading history...
57
            'password_reset_token' => $token,
58
            'status' => self::STATUS_ACTIVE,
59
        ]);
60
    }
61
62
    /**
63
     * Finds out if password reset token is valid
64
     *
65
     * @param string $token password reset token
66
     * @return bool
67
     */
68
    public static function isPasswordResetTokenValid($token)
69
    {
70
        if (empty($token)) {
71
            return false;
72
        }
73
74
        $timestamp = (int)substr($token, strrpos($token, '_') + 1);
75
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
76
77
        return $timestamp + $expire >= time();
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function getId()
84
    {
85
        return $this->getPrimaryKey();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getPrimaryKey() also could return the type array which is incompatible with the return type mandated by yii\web\IdentityInterface::getId() of integer|string.
Loading history...
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function getAuthKey()
92
    {
93
        return $this->auth_key;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function validateAuthKey($authKey)
100
    {
101
        return $this->getAuthKey() === $authKey;
102
    }
103
104
    /**
105
     * Validates password
106
     *
107
     * @param string $password password to validate
108
     * @return bool if password provided is valid for current user
109
     */
110
    public function validatePassword($password)
111
    {
112
        return Yii::$app->security->validatePassword($password, $this->password_hash);
113
    }
114
115
    /**
116
     * Generates password hash from password and sets it to the model
117
     *
118
     * @param string $password
119
     */
120
    public function setPassword($password)
121
    {
122
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
0 ignored issues
show
Bug Best Practice introduced by
The property password_hash does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
    }
124
125
    /**
126
     * Generates "remember me" authentication key
127
     */
128
    public function generateAuthKey()
129
    {
130
        $this->auth_key = Yii::$app->security->generateRandomString();
0 ignored issues
show
Bug Best Practice introduced by
The property auth_key does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
131
    }
132
133
    /**
134
     * Generates new password reset token
135
     */
136
    public function generatePasswordResetToken()
137
    {
138
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
0 ignored issues
show
Bug Best Practice introduced by
The property password_reset_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
139
    }
140
141
    /**
142
     * Removes password reset token
143
     */
144
    public function removePasswordResetToken()
145
    {
146
        $this->password_reset_token = null;
0 ignored issues
show
Bug Best Practice introduced by
The property password_reset_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152
    public function checkUserCredentials($username, $password)
153
    {
154
        $user = static::findByUsername($username);
155
        if (empty($user)) {
156
            return false;
157
        }
158
159
        return $user->validatePassword($password);
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function getUserDetails($username = null)
166
    {
167
        $user = $username
168
            ? static::findByUsername($username)
169
            : $this;
170
171
        return ['user_id' => $user->id];
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on app\models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177
    public function rules()
178
    {
179
        return [
180
            [['username'], 'string'],
181
        ];
182
    }
183
}
184