Issues (81)

models/User.php (2 issues)

1
<?php
2
3
namespace toir427\admin\models;
4
5
use toir427\admin\components\Configs;
6
use toir427\admin\components\UserStatus;
7
use Yii;
8
use yii\base\NotSupportedException;
9
use yii\behaviors\TimestampBehavior;
10
use yii\db\ActiveRecord;
11
use yii\web\IdentityInterface;
12
13
/**
14
 * User model
15
 *
16
 * @property integer $id
17
 * @property string $username
18
 * @property string $password_hash
19
 * @property string $password_reset_token
20
 * @property string $email
21
 * @property string $auth_key
22
 * @property integer $status
23
 * @property integer $created_at
24
 * @property integer $updated_at
25
 * @property string $password write-only password
26
 *
27
 * @property UserProfile $profile
28
 */
29
class User extends ActiveRecord implements IdentityInterface
30
{
31
    const STATUS_INACTIVE = 0;
32
    const STATUS_ACTIVE = 10;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public static function tableName()
38
    {
39
        return Configs::instance()->userTable;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function behaviors()
46
    {
47
        return [
48
            TimestampBehavior::className(),
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function rules()
56
    {
57
        return [
58
            ['status', 'in', 'range' => [UserStatus::ACTIVE, UserStatus::INACTIVE]],
59
        ];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public static function findIdentity($id)
66
    {
67
        return static::findOne(['id' => $id, 'status' => UserStatus::ACTIVE]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(a...ts\UserStatus::ACTIVE)) returns the type yii\db\ActiveRecord which is incompatible with the return type mandated by yii\web\IdentityInterface::findIdentity() of 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...
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public static function findIdentityByAccessToken($token, $type = null)
74
    {
75
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
76
    }
77
78
    /**
79
     * Finds user by username
80
     *
81
     * @param string $username
82
     * @return static|null
83
     */
84
    public static function findByUsername($username)
85
    {
86
        return static::findOne(['username' => $username, 'status' => UserStatus::ACTIVE]);
87
    }
88
89
    /**
90
     * Finds user by password reset token
91
     *
92
     * @param string $token password reset token
93
     * @return static|null
94
     */
95
    public static function findByPasswordResetToken($token)
96
    {
97
        if (!static::isPasswordResetTokenValid($token)) {
98
            return null;
99
        }
100
101
        return static::findOne([
102
                'password_reset_token' => $token,
103
                'status' => UserStatus::ACTIVE,
104
        ]);
105
    }
106
107
    /**
108
     * Finds out if password reset token is valid
109
     *
110
     * @param string $token password reset token
111
     * @return boolean
112
     */
113
    public static function isPasswordResetTokenValid($token)
114
    {
115
        if (empty($token)) {
116
            return false;
117
        }
118
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
119
        $parts = explode('_', $token);
120
        $timestamp = (int) end($parts);
121
        return $timestamp + $expire >= time();
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function getId()
128
    {
129
        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...
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function getAuthKey()
136
    {
137
        return $this->auth_key;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function validateAuthKey($authKey)
144
    {
145
        return $this->getAuthKey() === $authKey;
146
    }
147
148
    /**
149
     * Validates password
150
     *
151
     * @param string $password password to validate
152
     * @return boolean if password provided is valid for current user
153
     */
154
    public function validatePassword($password)
155
    {
156
        return Yii::$app->security->validatePassword($password, $this->password_hash);
157
    }
158
159
    /**
160
     * Generates password hash from password and sets it to the model
161
     *
162
     * @param string $password
163
     */
164
    public function setPassword($password)
165
    {
166
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
167
    }
168
169
    /**
170
     * Generates "remember me" authentication key
171
     */
172
    public function generateAuthKey()
173
    {
174
        $this->auth_key = Yii::$app->security->generateRandomString();
175
    }
176
177
    /**
178
     * Generates new password reset token
179
     */
180
    public function generatePasswordResetToken()
181
    {
182
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
183
    }
184
185
    /**
186
     * Removes password reset token
187
     */
188
    public function removePasswordResetToken()
189
    {
190
        $this->password_reset_token = null;
191
    }
192
193
    public static function getDb()
194
    {
195
        return Configs::userDb();
196
    }
197
}
198