Completed
Push — master ( 4e8f5b...6ca395 )
by Andrii
07:34
created

User::tableName()   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
/*
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\web\IdentityInterface;
18
19
/**
20
 * User model.
21
 *
22
 * @property integer $id
23
 * @property string $username
24
 * @property string $password_hash
25
 * @property string $password_reset_token
26
 * @property string $email
27
 * @property string $auth_key
28
 * @property integer $role
29
 * @property integer $status
30
 * @property integer $created_at
31
 * @property integer $updated_at
32
 * @property string $password write-only password
33
 */
34
class User extends Model implements IdentityInterface
35
{
36
    public $id;
37
    public $name;
38
    public $email;
39
    public $username;
40
    public $type;
41
    public $seller;
42
    public $seller_id;
43
44
    public $auth_key;
45
    public $password_hash;
46
47
    private static $_users = [];
48
49
    public function save()
50
    {
51
        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...
52
        Yii::$app->session->set('identity:' . $this->id, $this);
53
    }
54
55
    public static function findOne($id)
56
    {
57
        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...
58
            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...
59
        }
60
61
        return Yii::$app->session->get('identity:' . $id);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function rules()
68
    {
69
        return [
70
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
71
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
72
73
            ['role', 'default', 'value' => self::ROLE_USER],
74
            ['role', 'in', 'range' => [self::ROLE_USER]],
75
        ];
76
    }
77
78
    /** {@inheritdoc} */
79
    public static function findIdentity($id)
80
    {
81
        return static::findOne($id);
82
    }
83
84
    /** {@inheritdoc} */
85
    public function getAccessToken()
86
    {
87
        $client = Yii::$app->authClientCollection->getClient();
88
        if ($client->getAccessToken() === null) {
89
            return null;
90
        }
91
92
        return $client->getAccessToken()->getParam('access_token');
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public static function findIdentityByAccessToken($token, $type = null)
99
    {
100
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
101
    }
102
103
    /**
104
     * Finds user by username.
105
     *
106
     * @param string $username
107
     * @return static|null
108
     */
109
    public static function findByUsername($username)
110
    {
111
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
112
    }
113
114
    /**
115
     * Finds user by password reset token.
116
     *
117
     * @param string $token password reset token
118
     * @return static|null
119
     */
120
    public static function findByPasswordResetToken($token)
121
    {
122
        if (!static::isPasswordResetTokenValid($token)) {
123
            return null;
124
        }
125
126
        return static::findOne([
127
            'password_reset_token' => $token,
128
            'status' => self::STATUS_ACTIVE,
129
        ]);
130
    }
131
132
    /**
133
     * Finds out if password reset token is valid.
134
     *
135
     * @param string $token password reset token
136
     * @return boolean
137
     */
138
    public static function isPasswordResetTokenValid($token)
139
    {
140
        if (empty($token)) {
141
            return false;
142
        }
143
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
144
        $parts = explode('_', $token);
145
        $timestamp = (int) end($parts);
146
        return $timestamp + $expire >= time();
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getId()
153
    {
154
        return $this->id;
155
    }
156
157
    public function is($key)
158
    {
159
        return (int)$this->id === (int)$key || (string)$this->username === (string)$key;
160
    }
161
162
    public function not($key)
163
    {
164
        return (int)$this->id !== (int)$key && (string)$this->username !== (string)$key;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function getAuthKey()
171
    {
172
        return 'DUMMY';
173
        //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...
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function validateAuthKey($authKey)
180
    {
181
        return $this->getAuthKey() === $authKey;
182
    }
183
184
    /**
185
     * Validates password.
186
     *
187
     * @param string $password password to validate
188
     * @return boolean if password provided is valid for current user
189
     */
190
    public function validatePassword($password)
191
    {
192
        return Yii::$app->security->validatePassword($password, $this->password_hash);
193
    }
194
195
    /**
196
     * Generates password hash from password and sets it to the model.
197
     *
198
     * @param string $password
199
     */
200
    public function setPassword($password)
201
    {
202
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
203
    }
204
205
    /**
206
     * Generates "remember me" authentication key.
207
     */
208
    public function generateAuthKey()
209
    {
210
        $this->auth_key = 'DUMMY';
211
        //$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...
212
    }
213
214
    /**
215
     * Generates new password reset token.
216
     */
217
    public function generatePasswordResetToken()
218
    {
219
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
220
    }
221
222
    /**
223
     * Removes password reset token.
224
     */
225
    public function removePasswordResetToken()
226
    {
227
        $this->password_reset_token = null;
228
    }
229
}
230