cleric.test/models/User.php

127 lines
2.5 KiB
PHP

<?php
namespace app\models;
use Yii;
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $name;
public $login;
/// Path to a file stored in an imaginatory S3 bucket
public $avatar;
public $phone;
public $website;
public $passHash;
public $accessToken;
/**
* {@inheritDoc}
*/
public function rules()
{
return [
[['id', 'name', 'login', 'phone', 'passHash'], 'required'],
[['name', 'login', 'avatar', 'phone', 'website', 'passHash'], 'string'],
[['id'], 'integer'],
];
}
/**
* {@inheritDoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Имя',
'login' => 'Логин',
'phone' => 'Телефон',
'website' => 'Личный сайт',
'accessToken' => '',
'passHash' => 'Пароль',
];
}
/**
* {@inheritDoc}
*/
public function fields()
{
return [
'id', 'name', 'login', 'avatar', 'phone', 'website', 'accessToken'
];
}
/**
* {@inheritDoc}
*/
public static function tableName()
{
return "user";
}
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
{
return self::findOne([ 'id' => $id ]);
}
/**
* {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return self::findOne([ 'accessToken' => $token ]);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return self::findOne([ 'login' => $username ]);
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $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->passHash);
}
}