Passed
Push — master ( 3596af...da6412 )
by Wilmer
02:03
created

UserModels::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\basic\models;
4
5
use yii\activerecord\ActiveRecord;
6
use yii\base\Security;
7
use yii\behaviors\TimestampBehavior;
8
use yii\exceptions\NotSupportedException;
9
use yii\web\IdentityInterface;
10
11
/**
12
 * User is the model Web Application Basic.
13
 *
14
 * @property int $id
15
 * @property string $username
16
 * @property string $password_hash
17
 * @property string $password_reset_token
18
 * @property string $email
19
 * @property string $auth_key
20
 * @property int $status
21
 * @property int $created_at
22
 * @property int $updated_at
23
 * @property string $password write-only password
24
 **/
25
class UserModels extends ActiveRecord implements IdentityInterface
26
{
27
	const STATUS_DELETED = 0;
28
	const STATUS_ACTIVE = 10;
29
30
    protected $_Security;
31
32
    /**
33
     * __construct
34
     *
35
     * @param Application $app
0 ignored issues
show
Bug introduced by
The type app\basic\models\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     **/
37
    public function __construct()
38
    {
39
        $this->_Security = new Security();
40
    }
41
42
43
    /**
44
     * tableName
45
     *
46
	 * @return string table name.
47
	 **/
48
	public static function tableName()
49
	{
50
		return '{{%user}}';
51
	}
52
53
	/**
54
     * behaviors
55
     *
56
	 * @return array behaviors config.
57
	 **/
58
	public function behaviors()
59
	{
60
		return [
61
			TimestampBehavior::class,
62
		];
63
	}
64
65
	/**
66
     * rules
67
     *
68
	 * @return array the validation rules.
69
	 **/
70
	public function rules()
71
	{
72
		return [
73
			['status', 'default', 'value' => self::STATUS_ACTIVE],
74
			['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
75
		];
76
	}
77
78
	/**
79
     * findIdentity
80
     * Search user for id.
81
     *
82
	 * {@inheritdoc}
83
	 **/
84
	public static function findIdentity($id)
85
	{
86
		return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(a...> self::STATUS_ACTIVE)) returns the type yii\activerecord\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...
87
	}
88
89
	/**
90
     * findIdentityByAccessToken
91
     *
92
	 * {@inheritdoc}
93
	 **/
94
	public static function findIdentityByAccessToken($token, $type = null)
95
	{
96
		throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
97
	}
98
99
	/**
100
     * findByUsername
101
	 * Finds user by username.
102
	 *
103
	 * @param string $username
104
	 * @return static|null
105
	 **/
106
	public static function findByUsername($username)
107
	{
108
		return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
109
	}
110
111
	/**
112
     * findByPasswordResetToken
113
	 * Finds user by password reset token.
114
	 *
115
	 * @param string $token password reset token.
116
     * @param int $passwordResetTokenExpire password reset token.
117
	 * @return static|null
118
	 **/
119
	public static function findByPasswordResetToken($token, int $passwordResetTokenExpire)
120
	{
121
		if (!static::isPasswordResetTokenValid($token, $passwordResetTokenExpire)) {
122
			return null;
123
		}
124
125
		return static::findOne([
126
			'password_reset_token' => $token,
127
			'status' => self::STATUS_ACTIVE,
128
		]);
129
	}
130
131
	/**
132
	 * Finds out if password reset token is valid.
133
	 *
134
	 * @param string $token password reset token.
135
     * @param int $passwordResetTokenExpire password reset token.
136
	 * @return bool
137
	 **/
138
	public static function isPasswordResetTokenValid($token, int $passwordResetTokenExpire)
139
	{
140
		if (empty($token)) {
141
			return false;
142
		}
143
144
		$timestamp = (int) substr($token, strrpos($token, '_') + 1);
145
		$expire = $passwordResetTokenExpire;
146
147
		return $timestamp + $expire >= time();
148
	}
149
150
	/**
151
	 * getId
152
     *
153
     * @return int id.
154
	 **/
155
	public function getId()
156
	{
157
		return (int) ($this->getPrimaryKey());
158
	}
159
160
	/**
161
	 * getAuthKey
162
     *
163
     * @return string authentication key.
164
	 **/
165
	public function getAuthKey()
166
	{
167
		return $this->auth_key;
168
	}
169
170
	/**
171
     * validateAuthKey
172
     *
173
	 * @return bool validate authentication key.
174
	 **/
175
	public function validateAuthKey($authKey)
176
	{
177
		return $this->getAuthKey() === $authKey;
178
	}
179
180
	/**
181
     * validatePassword
182
	 * Validates password.
183
	 *
184
	 * @param string $password password to validate
185
	 * @return bool if password provided is valid for current user
186
	 **/
187
	public function validatePassword($password)
188
	{
189
		return $this->_Security->validatePassword($password, $this->password_hash);
190
	}
191
192
	/**
193
     * setPassword
194
	 * Generates password hash from password and sets it to the model.
195
	 *
196
	 * @param string $password
197
	 **/
198
	public function setPassword($password)
199
	{
200
		$this->password_hash = $this->_Security->generatePasswordHash($password);
201
	}
202
203
	/**
204
     * generateAuthKey
205
	 * Generates "remember me" authentication key.
206
     *
207
     * @return string authentication key.
208
	 **/
209
	public function generateAuthKey()
210
	{
211
		$this->auth_key = $this->_Security->generateRandomString();
212
	}
213
214
	/**
215
     * generatePasswordResetToken()
216
	 * Generates new password reset token.
217
     *
218
     * @return string new token.
219
	 **/
220
	public function generatePasswordResetToken()
221
	{
222
		$this->password_reset_token = $this->_Security->generateRandomString() . '_' . time();
223
	}
224
225
	/**
226
     * removePasswordResetToken
227
	 * Removes password reset token.
228
     *
229
     * @return null
230
	 **/
231
	public function removePasswordResetToken()
232
	{
233
		$this->password_reset_token = null;
234
	}
235
}
236