Completed
Push — master ( 342ffa...2ca792 )
by Andrii
05:25
created

User::getAuthKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\models;
13
14
use Yii;
15
use yii\base\Model;
16
use yii\base\NotSupportedException;
17
use yii\behaviors\TimestampBehavior;
18
use yii\web\IdentityInterface;
19
20
/**
21
 * User model.
22
 *
23
 * @property integer $id
24
 * @property string $username
25
 * @property string $password_hash
26
 * @property string $password_reset_token
27
 * @property string $email
28
 * @property string $auth_key
29
 * @property integer $role
30
 * @property integer $status
31
 * @property integer $created_at
32
 * @property integer $updated_at
33
 * @property string $password write-only password
34
 */
35
class User extends Model implements IdentityInterface
36
{
37
    public $id;
38
    public $name;
39
    public $email;
40
    public $username;
41
    public $type;
42
    public $seller;
43
    public $seller_id;
44
45
    private static $_users;
46
47
    public function save()
48
    {
49
        static::$_users[$this->id]  = $this;
0 ignored issues
show
Bug introduced by
Since $_users is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $_users to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
50
        Yii::$app->session->set('identity:' . $this->id, $this);
51
    }
52
53
    public static function findOne($id)
54
    {
55
        $find = static::$_users[$id];
0 ignored issues
show
Bug introduced by
Since $_users is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $_users to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
56
        if ($find) {
57
            return $find;
58
        }
59
        $find = Yii::$app->session->get('identity:' . $id);
60
        return $find;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public static function tableName()
67
    {
68
        return '{{%user}}';
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function behaviors()
75
    {
76
        return [
77
            TimestampBehavior::className(),
78
        ];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function rules()
85
    {
86
        return [
87
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
88
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
89
90
            ['role', 'default', 'value' => self::ROLE_USER],
91
            ['role', 'in', 'range' => [self::ROLE_USER]],
92
        ];
93
    }
94
95
    /** {@inheritdoc} */
96
    public static function findIdentity($id)
97
    {
98
        return static::findOne($id);
99
    }
100
101
    /** {@inheritdoc} */
102
    public function getAccessToken()
103
    {
104
        return Yii::$app->authClientCollection->getClient()->getAccessToken()->getParam('access_token');
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public static function findIdentityByAccessToken($token, $type = null)
111
    {
112
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
113
    }
114
115
    /**
116
     * Finds user by username.
117
     *
118
     * @param string $username
119
     * @return static|null
120
     */
121
    public static function findByUsername($username)
122
    {
123
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
124
    }
125
126
    /**
127
     * Finds user by password reset token.
128
     *
129
     * @param string $token password reset token
130
     * @return static|null
131
     */
132
    public static function findByPasswordResetToken($token)
133
    {
134
        if (!static::isPasswordResetTokenValid($token)) {
135
            return null;
136
        }
137
138
        return static::findOne([
139
            'password_reset_token' => $token,
140
            'status' => self::STATUS_ACTIVE,
141
        ]);
142
    }
143
144
    /**
145
     * Finds out if password reset token is valid.
146
     *
147
     * @param string $token password reset token
148
     * @return boolean
149
     */
150
    public static function isPasswordResetTokenValid($token)
151
    {
152
        if (empty($token)) {
153
            return false;
154
        }
155
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
156
        $parts = explode('_', $token);
157
        $timestamp = (int) end($parts);
158
        return $timestamp + $expire >= time();
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getId()
165
    {
166
        return $this->id;
167
    }
168
169
    public function is($key)
170
    {
171
        return $this->id === $key || $this->username === $key;
172
    }
173
174
    public function not($key)
175
    {
176
        return $this->id !== $key && $this->username !== $key;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getAuthKey()
183
    {
184
        return 'DUMMY';
185
        //return $this->auth_key;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function validateAuthKey($authKey)
192
    {
193
        return $this->getAuthKey() === $authKey;
194
    }
195
196
    /**
197
     * Validates password.
198
     *
199
     * @param string $password password to validate
200
     * @return boolean if password provided is valid for current user
201
     */
202
    public function validatePassword($password)
203
    {
204
        return Yii::$app->security->validatePassword($password, $this->password_hash);
205
    }
206
207
    /**
208
     * Generates password hash from password and sets it to the model.
209
     *
210
     * @param string $password
211
     */
212
    public function setPassword($password)
213
    {
214
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
215
    }
216
217
    /**
218
     * Generates "remember me" authentication key.
219
     */
220
    public function generateAuthKey()
221
    {
222
        $this->auth_key = 'DUMMY';
223
        //$this->auth_key = Yii::$app->security->generateRandomString();
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
224
    }
225
226
    /**
227
     * Generates new password reset token.
228
     */
229
    public function generatePasswordResetToken()
230
    {
231
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
232
    }
233
234
    /**
235
     * Removes password reset token.
236
     */
237
    public function removePasswordResetToken()
238
    {
239
        $this->password_reset_token = null;
240
    }
241
}
242