2013-05-24 10:14:49 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\models;
|
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
use Yii;
|
|
|
|
use yii\web\IdentityInterface;
|
2016-04-28 18:06:20 +02:00
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2024-02-09 13:54:07 +08:00
|
|
|
public $password2; // 重复密码
|
|
|
|
public $rememberMe; // 记住我
|
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
2018-02-19 00:29:21 +02:00
|
|
|
* {@inheritdoc}
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public static function tableName()
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return 'user';
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
2024-02-09 13:54:07 +08:00
|
|
|
* // rules说明
|
|
|
|
* // 1. username, password, password2, email 必填
|
|
|
|
* // 2. username 长度在3-12之间
|
|
|
|
* // 3. password 长度在6-12之间
|
|
|
|
* // 4. password2 必须和password一致
|
|
|
|
* // 5. email 必须是邮箱格式
|
|
|
|
* // 6. username, email 必须是唯一的
|
|
|
|
* *
|
2018-02-19 00:29:21 +02:00
|
|
|
* {@inheritdoc}
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public function rules()
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return [
|
2024-02-10 12:00:16 +08:00
|
|
|
[['username', 'password'], 'required', 'on' => 'login'],
|
2024-02-09 13:54:07 +08:00
|
|
|
[['username', 'password', 'email', 'password2'], 'required', 'on' => 'register'],
|
|
|
|
['username', 'string', 'min' => 3, 'max' => 12],
|
2024-02-10 12:00:16 +08:00
|
|
|
['password', 'string', 'min' => 6, 'max' => 24],
|
2024-02-09 13:54:07 +08:00
|
|
|
['password2', 'compare', 'compareAttribute' => 'password', 'on' => 'register'],
|
|
|
|
['email', 'email', 'on' => 'register'],
|
|
|
|
['username', 'unique', 'on' => 'register'],
|
|
|
|
['email', 'unique', 'on' => 'register'],
|
2024-02-09 12:15:09 +08:00
|
|
|
];
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
|
|
|
'username' => 'Username',
|
|
|
|
'password' => 'Password',
|
|
|
|
'auth_key' => 'Auth Key',
|
|
|
|
'email' => 'Email',
|
|
|
|
'status' => 'Status',
|
|
|
|
];
|
|
|
|
}
|
2024-02-09 12:47:48 +08:00
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
/**
|
|
|
|
* Finds an identity by the given ID.
|
2014-03-16 10:46:16 +06:00
|
|
|
*
|
2024-02-09 12:15:09 +08:00
|
|
|
* @param string|int $id the ID to be looked for
|
|
|
|
* @return IdentityInterface|null the identity object that matches the given ID.
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public static function findIdentity($id)
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return static::findOne($id);
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* 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.
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public static function findIdentityByAccessToken($token, $type = null)
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
// This method is not needed if you don't use access tokens for authentication.
|
|
|
|
return null;
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Returns the ID of the user.
|
|
|
|
*
|
|
|
|
* @return string|int the ID of the user
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public function getId()
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->id;
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Returns an auth key used to authenticate cookie-based login.
|
|
|
|
*
|
|
|
|
* @return string the auth key
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public function getAuthKey()
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->auth_key;
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Validates the given auth key.
|
2014-03-16 10:46:16 +06:00
|
|
|
*
|
2024-02-09 12:15:09 +08:00
|
|
|
* @param string $authKey the given auth key
|
|
|
|
* @return bool whether the given auth key is valid.
|
2014-03-16 10:46:16 +06:00
|
|
|
*/
|
2024-02-09 12:15:09 +08:00
|
|
|
public function validateAuthKey($authKey)
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->getAuthKey() === $authKey;
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2024-02-09 12:47:48 +08:00
|
|
|
|
|
|
|
/**
|
2024-02-09 13:54:07 +08:00
|
|
|
* 用户登录处理
|
2024-02-09 12:47:48 +08:00
|
|
|
*
|
|
|
|
* @return bool 返回用户名密码验证状态
|
|
|
|
*/
|
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
$user = User::findOne(['username' => $this->username]);
|
|
|
|
|
|
|
|
if ($user !== null && $user->validatePassword($this->password)) {
|
2024-02-09 13:54:07 +08:00
|
|
|
// check user status
|
|
|
|
if ($user->status == 0) {
|
|
|
|
$this->addError('username', '此用户已被禁用,请联系管理员获取支持');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rememberMe = $this->rememberMe ? 3600 * 24 * 30 : 0;
|
|
|
|
return Yii::$app->user->login($user, $rememberMe);
|
2024-02-09 12:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-09 13:54:07 +08:00
|
|
|
* 验证密码
|
|
|
|
*
|
2024-02-09 12:47:48 +08:00
|
|
|
* @param $password
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validatePassword($password)
|
|
|
|
{
|
|
|
|
return Yii::$app->security->validatePassword($password, $this->password);
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
}
|