Test Failed
Pull Request — master (#5)
by aguevaraIL
07:24
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
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace app\models;
4
5
use OAuth2\Storage\UserCredentialsInterface;
6
use roaresearch\yii2\oauth2server\models\OauthAccessTokens as AccessToken;
7
use Yii;
8
use yii\web\IdentityInterface;
9
10
class User extends \yii\db\ActiveRecord implements UserCredentialsInterface, IdentityInterface
11
{
12
    public static function tableName()
13
    {
14
        return '{{%user}}';
15
    }
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public static function findIdentity($id)
21
    {
22
        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...
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public static function findIdentityByAccessToken($token, $type = null)
29
    {
30
        return static::find()->joinWith('accessTokens', false)
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::find()->j...ken' => $token))->one() also could return the type array|yii\db\ActiveRecord which is incompatible with the return type mandated by yii\web\IdentityInterfac...IdentityByAccessToken() of null|yii\web\IdentityInterface.
Loading history...
31
            ->andWhere(['access_token' => $token])
32
            ->one();
33
    }
34
35
    /**
36
     * Finds user by username
37
     *
38
     * @param string $username
39
     * @return static|null
40
     */
41
    public static function findByUsername($username)
42
    {
43
        return static::findOne(['username' => $username]);
44
    }
45
46
    /**
47
     * Finds user by password reset token
48
     *
49
     * @param string $token password reset token
50
     * @return static|null
51
     */
52
    public static function findByPasswordResetToken($token)
53
    {
54
        if (!static::isPasswordResetTokenValid($token)) {
55
            return null;
56
        }
57
58
        return static::findOne([
59
            'password_reset_token' => $token,
60
            'status' => self::STATUS_ACTIVE,
0 ignored issues
show
Bug introduced by
The constant app\models\User::STATUS_ACTIVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
        ]);
62
    }
63
64
    /**
65
     * Finds out if password reset token is valid
66
     *
67
     * @param string $token password reset token
68
     * @return bool
69
     */
70
    public static function isPasswordResetTokenValid($token)
71
    {
72
        if (empty($token)) {
73
            return false;
74
        }
75
76
        $timestamp = (int)substr($token, strrpos($token, '_') + 1);
77
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
78
79
        return $timestamp + $expire >= time();
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function getId()
86
    {
87
        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...
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function getAuthKey()
94
    {
95
        return $this->auth_key;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function validateAuthKey($authKey)
102
    {
103
        return $this->getAuthKey() === $authKey;
104
    }
105
106
    /**
107
     * Validates password
108
     *
109
     * @param string $password password to validate
110
     * @return bool if password provided is valid for current user
111
     */
112
    public function validatePassword($password)
113
    {
114
        return Yii::$app->security->validatePassword($password, $this->password_hash);
115
    }
116
117
    /**
118
     * Generates password hash from password and sets it to the model
119
     *
120
     * @param string $password
121
     */
122
    public function setPassword($password)
123
    {
124
        $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...
125
    }
126
127
    /**
128
     * Generates "remember me" authentication key
129
     */
130
    public function generateAuthKey()
131
    {
132
        $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...
133
    }
134
135
    /**
136
     * Generates new password reset token
137
     */
138
    public function generatePasswordResetToken()
139
    {
140
        $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...
141
    }
142
143
    /**
144
     * Removes password reset token
145
     */
146
    public function removePasswordResetToken()
147
    {
148
        $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...
149
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154
    public function checkUserCredentials($username, $password)
155
    {
156
        $user = static::findByUsername($username);
157
        if (empty($user)) {
158
            return false;
159
        }
160
161
        return $user->validatePassword($password);
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    public function getUserDetails($username = null)
168
    {
169
        $user = $username
170
            ? static::findByUsername($username)
171
            : $this;
172
173
        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...
174
    }
175
176
    /**
177
     * @return \yii\db\ActiveQuery
178
     */
179
    public function getAccessTokens()
180
    {
181
        return $this->hasMany(AccessToken::class, ['user_id' => 'id'])
182
            ->andOnCondition(['client_id' => 'testclient']);
183
    }
184
185
186
    public function rules()
187
    {
188
        return [
189
            [['username'], 'string'],
190
        ];
191
    }
192
}
193