Completed
Push — master ( e7aa93...2a1bc3 )
by Klochok
04:08
created

User::generatePasswordResetToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * HiPanel core package.
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\models;
12
13
use Yii;
14
use yii\base\Model;
15
use yii\base\NotSupportedException;
16
use yii\web\IdentityInterface;
17
18
/**
19
 * User model.
20
 *
21
 * @property integer $id
22
 * @property string $username
23
 * @property string $email
24
 * @property string $login
25
 * @property string $roles
26
 * @property string $status
27
 * @property string $auth_key
28
 * @property string $password write-only password
29
 * @property string $password_hash
30
 * @property string $password_reset_token
31
 */
32
class User extends Model implements IdentityInterface
33
{
34
    public $id;
35
    public $email;
36
    public $username;
37
    public $type;
38
    public $roles;
39
    public $seller;
40
    public $seller_id;
41
    public $last_name;
42
    public $first_name;
43
44
    public $auth_key;
45
    public $password_hash;
46
47
    private static $_users = [];
48
49
    const TYPE_CLIENT = 'client';
50
    const TYPE_ADMIN = 'admin';
51
    const TYPE_MANAGER = 'manager';
52
    const TYPE_RESELLER = 'reseller';
53
    const TYPE_OWNER = 'owner';
54
55
    public function save()
56
    {
57
        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...
58
        Yii::$app->session->set('identity:' . $this->id, $this);
59
    }
60
61
    public static function findOne($id)
62
    {
63
        if (isset(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...
64
            return 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...
65
        }
66
67
        return Yii::$app->session->get('identity:' . $id);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function rules()
74
    {
75
        return [
76
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
77
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
78
79
            ['role', 'default', 'value' => self::ROLE_USER],
80
            ['role', 'in', 'range' => [self::ROLE_USER]],
81
        ];
82
    }
83
84
    /** {@inheritdoc} */
85
    public static function findIdentity($id)
86
    {
87
        return static::findOne($id);
88
    }
89
90
    /** {@inheritdoc} */
91
    public function getAccessToken()
92
    {
93
        $client = Yii::$app->authClientCollection->getClient();
94
        $token = $client->getAccessToken();
95
96
        return $token ? $token->getParam('access_token') : null;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public static function findIdentityByAccessToken($token, $type = null)
103
    {
104
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
105
    }
106
107
    /**
108
     * Finds user by username.
109
     *
110
     * @param string $username
111
     * @return static|null
112
     */
113
    public static function findByUsername($username)
114
    {
115
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
116
    }
117
118
    /**
119
     * Finds user by password reset token.
120
     *
121
     * @param string $token password reset token
122
     * @return static|null
123
     */
124
    public static function findByPasswordResetToken($token)
125
    {
126
        if (!static::isPasswordResetTokenValid($token)) {
127
            return null;
128
        }
129
130
        return static::findOne([
131
            'password_reset_token' => $token,
132
            'status' => self::STATUS_ACTIVE,
133
        ]);
134
    }
135
136
    /**
137
     * Finds out if password reset token is valid.
138
     *
139
     * @param string $token password reset token
140
     * @return boolean
141
     */
142
    public static function isPasswordResetTokenValid($token)
143
    {
144
        if (empty($token)) {
145
            return false;
146
        }
147
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
148
        $parts = explode('_', $token);
149
        $timestamp = (int)end($parts);
150
        return $timestamp + $expire >= time();
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getId()
157
    {
158
        return $this->id;
159
    }
160
161
    public function is($key)
162
    {
163
        return (int)$this->id === (int)$key || (string)$this->username === (string)$key;
164
    }
165
166
    public function not($key)
167
    {
168
        return (int)$this->id !== (int)$key && (string)$this->username !== (string)$key;
169
    }
170
171
    public function getLogin()
172
    {
173
        return $this->username;
174
    }
175
176
    public function getName()
177
    {
178
        return trim($this->first_name . ' ' . $this->last_name);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function getAuthKey()
185
    {
186
        return 'DUMMY';
187
        //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...
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function validateAuthKey($authKey)
194
    {
195
        return $this->getAuthKey() === $authKey;
196
    }
197
198
    /**
199
     * Validates password.
200
     *
201
     * @param string $password password to validate
202
     * @return boolean if password provided is valid for current user
203
     */
204
    public function validatePassword($password)
205
    {
206
        return Yii::$app->security->validatePassword($password, $this->password_hash);
207
    }
208
209
    /**
210
     * Generates password hash from password and sets it to the model.
211
     *
212
     * @param string $password
213
     */
214
    public function setPassword($password)
215
    {
216
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
217
    }
218
219
    /**
220
     * Generates "remember me" authentication key.
221
     */
222
    public function generateAuthKey()
223
    {
224
        $this->auth_key = 'DUMMY';
225
        //$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...
226
    }
227
228
    /**
229
     * Generates new password reset token.
230
     */
231
    public function generatePasswordResetToken()
232
    {
233
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
234
    }
235
236
    /**
237
     * Removes password reset token.
238
     */
239
    public function removePasswordResetToken()
240
    {
241
        $this->password_reset_token = null;
242
    }
243
244
    /**
245
     * @param $seller string|integer
246
     * @return bool
247
     */
248
    public function hasSeller($seller)
249
    {
250
        return (((string)$seller === (string)$this->seller) || ((int)$seller === (int)$this->seller_id));
251
    }
252
}
253