yii2-netdisk/models/User.php
Chenx221 7303af1048 add login,register page
尚未检验登录注册是否工作
2024-02-09 12:47:48 +08:00

136 lines
3.3 KiB
PHP

<?php
namespace app\models;
use Yii;
use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* @property int $id 用户ID
* @property string|null $username 用户名
* @property string|null $password 密码
* @property string|null $auth_key authkey
* @property string|null $email 邮箱
* @property int|null $status 用户状态
*/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'password'], 'required', 'on' => 'login'],
[['username', 'password', 'email'], 'required', 'on' => 'register'],
[['username', 'password', 'auth_key', 'email'], 'string', 'max' => 255],
[['status'], 'integer'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'auth_key' => 'Auth Key',
'email' => 'Email',
'status' => 'Status',
];
}
/**
* Finds an identity by the given ID.
*
* @param string|int $id the ID to be looked for
* @return IdentityInterface|null the identity object that matches the given ID.
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* Finds an identity by the given token.
*
* @param mixed $token the token to be looked for
* @param mixed $type the type of the token. The value of this parameter depends on the implementation.
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
* @return IdentityInterface|null the identity object that matches the given token.
*/
public static function findIdentityByAccessToken($token, $type = null)
{
// This method is not needed if you don't use access tokens for authentication.
return null;
}
/**
* Returns the ID of the user.
*
* @return string|int the ID of the user
*/
public function getId()
{
return $this->id;
}
/**
* Returns an auth key used to authenticate cookie-based login.
*
* @return string the auth key
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* Validates the given auth key.
*
* @param string $authKey the given auth key
* @return bool whether the given auth key is valid.
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* 验证用户名密码
*
* @return bool 返回用户名密码验证状态
*/
public function login()
{
$user = User::findOne(['username' => $this->username]);
if ($user !== null && $user->validatePassword($this->password)) {
return Yii::$app->user->login($user);
}
return false;
}
/**
* @param $password
* @return bool
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
}