Completed
Push — master ( a311e0...01e3d1 )
by Andrii
13:22
created

User::getName()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
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 $email
25
 * @property string $login
26
 * @property integer $role
27
 * @property integer $status
28
 * @property string $auth_key
29
 * @property string $password write-only password
30
 * @property string $password_hash
31
 * @property string $password_reset_token
32
 */
33
class User extends Model implements IdentityInterface
34
{
35
    public $id;
36
    public $email;
37
    public $username;
38
    public $type;
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
    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
        $token = $client->getAccessToken();
89
90
        return $token ? $token->getParam('access_token') : null;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public static function findIdentityByAccessToken($token, $type = null)
97
    {
98
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
99
    }
100
101
    /**
102
     * Finds user by username.
103
     *
104
     * @param string $username
105
     * @return static|null
106
     */
107
    public static function findByUsername($username)
108
    {
109
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
110
    }
111
112
    /**
113
     * Finds user by password reset token.
114
     *
115
     * @param string $token password reset token
116
     * @return static|null
117
     */
118
    public static function findByPasswordResetToken($token)
119
    {
120
        if (!static::isPasswordResetTokenValid($token)) {
121
            return null;
122
        }
123
124
        return static::findOne([
125
            'password_reset_token' => $token,
126
            'status' => self::STATUS_ACTIVE,
127
        ]);
128
    }
129
130
    /**
131
     * Finds out if password reset token is valid.
132
     *
133
     * @param string $token password reset token
134
     * @return boolean
135
     */
136
    public static function isPasswordResetTokenValid($token)
137
    {
138
        if (empty($token)) {
139
            return false;
140
        }
141
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
142
        $parts = explode('_', $token);
143
        $timestamp = (int) end($parts);
144
        return $timestamp + $expire >= time();
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getId()
151
    {
152
        return $this->id;
153
    }
154
155
    public function is($key)
156
    {
157
        return (int)$this->id === (int)$key || (string)$this->username === (string)$key;
158
    }
159
160
    public function not($key)
161
    {
162
        return (int)$this->id !== (int)$key && (string)$this->username !== (string)$key;
163
    }
164
165
    public function getLogin()
166
    {
167
        return $this->username;
168
    }
169
170
    public function getName()
171
    {
172
        return trim($this->first_name . ' ' . $this->last_name);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getAuthKey()
179
    {
180
        return 'DUMMY';
181
        //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...
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function validateAuthKey($authKey)
188
    {
189
        return $this->getAuthKey() === $authKey;
190
    }
191
192
    /**
193
     * Validates password.
194
     *
195
     * @param string $password password to validate
196
     * @return boolean if password provided is valid for current user
197
     */
198
    public function validatePassword($password)
199
    {
200
        return Yii::$app->security->validatePassword($password, $this->password_hash);
201
    }
202
203
    /**
204
     * Generates password hash from password and sets it to the model.
205
     *
206
     * @param string $password
207
     */
208
    public function setPassword($password)
209
    {
210
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
211
    }
212
213
    /**
214
     * Generates "remember me" authentication key.
215
     */
216
    public function generateAuthKey()
217
    {
218
        $this->auth_key = 'DUMMY';
219
        //$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...
220
    }
221
222
    /**
223
     * Generates new password reset token.
224
     */
225
    public function generatePasswordResetToken()
226
    {
227
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
228
    }
229
230
    /**
231
     * Removes password reset token.
232
     */
233
    public function removePasswordResetToken()
234
    {
235
        $this->password_reset_token = null;
236
    }
237
}
238