159 lines
3.5 KiB
PHP
159 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use Yii;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\db\ActiveRecord;
|
|
use yii\db\BaseActiveRecord;
|
|
use yii\web\IdentityInterface;
|
|
|
|
/**
|
|
* This is the model class for table "user".
|
|
*
|
|
* @property int $id
|
|
* @property string $username
|
|
* @property string $auth_key
|
|
* @property string $password_hash
|
|
* @property string|null $password_reset_token
|
|
* @property string $email
|
|
* @property int $status
|
|
* @property int $created_at
|
|
* @property int $updated_at
|
|
* @property string $role
|
|
*/
|
|
class User extends BaseModel implements IdentityInterface
|
|
{
|
|
|
|
const STATUS_DELETED = 0;
|
|
const STATUS_ACTIVE = 10;
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return 'user';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['password_reset_token'], 'default', 'value' => null],
|
|
[['status'], 'default', 'value' => 10],
|
|
[['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
|
|
[['status', 'created_at', 'updated_at'], 'integer'],
|
|
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
|
|
[['auth_key', 'role'], 'string', 'max' => 32],
|
|
[['username'], 'unique'],
|
|
[['email'], 'unique'],
|
|
[['password_reset_token'], 'unique'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id' => 'ID',
|
|
'username' => 'Username',
|
|
'auth_key' => 'Auth Key',
|
|
'password_hash' => 'Password Hash',
|
|
'password_reset_token' => 'Password Reset Token',
|
|
'email' => 'Email',
|
|
'status' => 'Status',
|
|
'created_at' => 'Created At',
|
|
'updated_at' => 'Updated At',
|
|
'role' => 'Role',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function findIdentity($id)
|
|
{
|
|
return static::find()
|
|
->where(['id' => $id])
|
|
->andWhere(['status' => self::STATUS_ACTIVE])
|
|
->one();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function findIdentityByAccessToken($token, $type = null)
|
|
{
|
|
die(__FILE__);
|
|
foreach (self::$users as $user) {
|
|
if ($user['accessToken'] === $token) {
|
|
return new static($user);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Finds user by username
|
|
*
|
|
* @param string $username
|
|
* @return static|null
|
|
*/
|
|
public static function findByUsername($username)
|
|
{
|
|
return static::find()
|
|
->where(['username' => $username])
|
|
->andWhere(['status' => self::STATUS_ACTIVE])
|
|
->one();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getAuthKey()
|
|
{
|
|
return $this->auth_key;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function validateAuthKey($authKey)
|
|
{
|
|
return $this->auth_key === $authKey;
|
|
}
|
|
|
|
/**
|
|
* Validates password
|
|
*
|
|
* @param string $password password to validate
|
|
* @return bool if password provided is valid for current user
|
|
*/
|
|
public function validatePassword($password)
|
|
{
|
|
return Yii::$app->security->validatePassword(
|
|
$password,
|
|
$this->password_hash
|
|
);
|
|
}
|
|
|
|
public function getRole()
|
|
{
|
|
return $this->role;
|
|
}
|
|
}
|