2013-05-24 22:14:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\models;
|
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
use Yii;
|
2024-03-01 18:04:04 +08:00
|
|
|
use yii\db\ActiveQuery;
|
2024-02-16 13:59:54 +08:00
|
|
|
use yii\db\ActiveRecord;
|
2024-02-09 12:15:09 +08:00
|
|
|
use yii\web\IdentityInterface;
|
2016-04-29 00:06:20 +08:00
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
/**
|
|
|
|
* This is the model class for table "user".
|
|
|
|
*
|
|
|
|
* @property int $id 用户ID
|
2024-03-02 19:14:39 +08:00
|
|
|
* @property string|null $username 用户名
|
2024-03-03 14:25:14 +08:00
|
|
|
* @property string|null $name 昵称
|
2024-03-02 19:14:39 +08:00
|
|
|
* @property string|null $password 密码
|
|
|
|
* @property string|null $auth_key authkey
|
|
|
|
* @property string|null $email 邮箱
|
|
|
|
* @property int|null $status 账户是否启用
|
|
|
|
* @property string|null $created_at 账户创建时间
|
|
|
|
* @property string|null $last_login 上次登陆时间
|
|
|
|
* @property string|null $last_login_ip 上次登录ip
|
|
|
|
* @property string|null $bio 备注
|
|
|
|
* @property string|null $role 身份
|
|
|
|
* @property string|null $encryption_key 加密密钥
|
|
|
|
* @property string|null $otp_secret otp密钥
|
|
|
|
* @property int|null $is_encryption_enabled 启用加密
|
|
|
|
* @property int|null $is_otp_enabled 启用otp
|
|
|
|
* @property int|null $storage_limit 存储容量限制,MB
|
2024-03-06 20:14:47 +08:00
|
|
|
* @property string|null $recovery_codes OTP恢复代码
|
2024-03-01 18:04:04 +08:00
|
|
|
*
|
|
|
|
* @property CollectionTasks[] $collectionTasks
|
|
|
|
* @property Share[] $shares
|
2024-02-09 12:15:09 +08:00
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
class User extends ActiveRecord implements IdentityInterface
|
2024-02-09 12:15:09 +08:00
|
|
|
{
|
2024-02-09 13:54:07 +08:00
|
|
|
public $password2; // 重复密码
|
|
|
|
public $rememberMe; // 记住我
|
|
|
|
|
2024-03-03 15:32:49 +08:00
|
|
|
public $oldPassword; // 旧密码 修改密码用
|
|
|
|
public $newPassword; // 新密码 修改密码用
|
|
|
|
public $newPasswordRepeat; // 重复新密码 修改密码用
|
2024-03-06 20:14:47 +08:00
|
|
|
public $totp_input; // otp用户输入值
|
2024-03-03 15:32:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
2018-02-19 06:29:21 +08:00
|
|
|
* {@inheritdoc}
|
2014-03-16 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public static function tableName(): string
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return 'user';
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
2018-02-19 06:29:21 +08:00
|
|
|
* {@inheritdoc}
|
2014-03-16 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function rules(): array
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return [
|
2024-03-01 18:04:04 +08:00
|
|
|
[['status', 'is_encryption_enabled', 'is_otp_enabled'], 'integer'],
|
|
|
|
[['created_at', 'last_login'], 'safe'],
|
2024-03-07 20:35:50 +08:00
|
|
|
[['bio', 'totp_input'], 'string'],
|
|
|
|
[['encryption_key', 'otp_secret', 'recovery_codes'], 'string', 'max' => 255],
|
2024-03-01 18:04:04 +08:00
|
|
|
[['last_login_ip'], 'string', 'max' => 45],
|
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-03-03 14:25:14 +08:00
|
|
|
['password', 'string', 'min' => 6, 'max' => 24, 'on' => 'register'],
|
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-03-03 15:32:49 +08:00
|
|
|
[['oldPassword', 'newPassword', 'newPasswordRepeat'], 'required', 'on' => 'changePassword'],
|
|
|
|
['oldPassword', 'validatePassword2', 'on' => 'changePassword'],
|
|
|
|
['newPassword', 'string', 'min' => 6, 'max' => 24, 'on' => 'changePassword'],
|
|
|
|
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword', 'on' => 'changePassword'],
|
|
|
|
['newPassword', 'compare', 'compareAttribute' => 'oldPassword', 'operator' => '!=', 'message' => '新密码不能与旧密码相同', 'on' => 'changePassword'],
|
2024-02-09 12:15:09 +08:00
|
|
|
];
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2024-03-03 15:32:49 +08:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $params
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function validatePassword2($attribute, $params): void
|
|
|
|
{
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
if (!Yii::$app->security->validatePassword($this->$attribute, $this->password)) {
|
|
|
|
$this->addError($attribute, '原密码不匹配');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function attributeLabels(): array
|
2024-02-09 12:15:09 +08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
|
|
|
'username' => 'Username',
|
2024-03-03 14:25:14 +08:00
|
|
|
'name' => 'Name',
|
2024-02-09 12:15:09 +08:00
|
|
|
'password' => 'Password',
|
|
|
|
'auth_key' => 'Auth Key',
|
|
|
|
'email' => 'Email',
|
|
|
|
'status' => 'Status',
|
2024-03-01 18:04:04 +08:00
|
|
|
'created_at' => 'Created At',
|
|
|
|
'last_login' => 'Last Login',
|
|
|
|
'last_login_ip' => 'Last Login Ip',
|
|
|
|
'bio' => 'Bio',
|
|
|
|
'role' => 'Role',
|
|
|
|
'encryption_key' => 'Encryption Key',
|
|
|
|
'otp_secret' => 'Otp Secret',
|
|
|
|
'is_encryption_enabled' => 'Is Encryption Enabled',
|
|
|
|
'is_otp_enabled' => 'Is Otp Enabled',
|
2024-03-02 19:14:39 +08:00
|
|
|
'storage_limit' => 'Storage Limit',
|
2024-03-06 20:14:47 +08:00
|
|
|
'recovery_codes' => 'Recovery Codes',
|
|
|
|
|
2024-02-09 12:15:09 +08:00
|
|
|
];
|
|
|
|
}
|
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 12:46:16 +08: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 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public static function findIdentity($id): ?IdentityInterface
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return static::findOne($id);
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08: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 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public static function findIdentityByAccessToken($token, $type = null): ?IdentityInterface
|
2014-03-16 12:46:16 +08: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 12:46:16 +08:00
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Returns the ID of the user.
|
|
|
|
*
|
2024-03-01 18:04:04 +08:00
|
|
|
* @return int the ID of the user
|
2014-03-16 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function getId(): int
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->id;
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Returns an auth key used to authenticate cookie-based login.
|
|
|
|
*
|
2024-03-01 18:04:04 +08:00
|
|
|
* @return string|null the auth key
|
2014-03-16 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function getAuthKey(): ?string
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->auth_key;
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-09 12:15:09 +08:00
|
|
|
* Validates the given auth key.
|
2014-03-16 12:46:16 +08: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 12:46:16 +08:00
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function validateAuthKey($authKey): bool
|
2014-03-16 12:46:16 +08:00
|
|
|
{
|
2024-02-09 12:15:09 +08:00
|
|
|
return $this->getAuthKey() === $authKey;
|
2014-03-16 12:46:16 +08: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 返回用户名密码验证状态
|
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function login(): bool
|
2024-02-09 12:47:48 +08:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2024-03-01 18:04:04 +08:00
|
|
|
public function validatePassword($password): bool
|
2024-02-09 12:47:48 +08:00
|
|
|
{
|
|
|
|
return Yii::$app->security->validatePassword($password, $this->password);
|
|
|
|
}
|
2024-03-01 18:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets query for [[CollectionTasks]].
|
|
|
|
*
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getCollectionTasks(): ActiveQuery
|
|
|
|
{
|
|
|
|
return $this->hasMany(CollectionTasks::class, ['user_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets query for [[Shares]].
|
|
|
|
*
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getShares(): ActiveQuery
|
|
|
|
{
|
|
|
|
return $this->hasMany(Share::class, ['sharer_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address.
|
|
|
|
* 获取Gravatar头像url或完整的img标签
|
|
|
|
*
|
|
|
|
* @param string $email The email address
|
|
|
|
* @param int|string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
|
|
|
|
* @param string $d Default imageset to use [ 404 | mp | identicon | monsterid | wavatar ]
|
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
|
|
|
|
* @param boolean $img True to return a complete IMG tag False for just the URL
|
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
|
|
|
|
* @return String containing either just a URL or a complete image tag
|
|
|
|
* @source https://gravatar.com/site/implement/images/php/
|
|
|
|
*/
|
2024-03-02 19:14:39 +08:00
|
|
|
public function getGravatar(string $email, int|string $s = 80, string $d = 'mp', string $r = 'x', bool $img = false, array $atts = array()): string
|
2024-03-01 18:04:04 +08:00
|
|
|
{
|
|
|
|
$url = 'https://www.gravatar.com/avatar/';
|
2024-03-02 19:14:39 +08:00
|
|
|
$url .= md5(strtolower(trim($email)));
|
2024-03-01 18:04:04 +08:00
|
|
|
$url .= "?s=$s&d=$d&r=$r";
|
2024-03-02 19:14:39 +08:00
|
|
|
if ($img) {
|
2024-03-01 18:04:04 +08:00
|
|
|
$url = '<img src="' . $url . '"';
|
2024-03-02 19:14:39 +08:00
|
|
|
foreach ($atts as $key => $val)
|
2024-03-01 18:04:04 +08:00
|
|
|
$url .= ' ' . $key . '="' . $val . '"';
|
|
|
|
$url .= ' />';
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:51:19 +08:00
|
|
|
public function deleteAccount(): false|int
|
|
|
|
{
|
|
|
|
// 设置用户状态为禁用
|
|
|
|
$this->status = 0;
|
|
|
|
|
|
|
|
// 保存用户模型
|
|
|
|
if (!$this->save()) {
|
|
|
|
return false; // something wrong
|
|
|
|
}
|
|
|
|
// 更新与用户相关的所有 CollectionTasks 和 Share 记录的状态为禁用
|
|
|
|
CollectionTasks::updateAll(['status' => 0], ['user_id' => $this->id]);
|
|
|
|
Share::updateAll(['status' => 0], ['sharer_id' => $this->id]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-05 13:32:12 +08:00
|
|
|
public function afterSave($insert, $changedAttributes): void
|
|
|
|
{
|
|
|
|
parent::afterSave($insert, $changedAttributes);
|
|
|
|
|
|
|
|
$auth = Yii::$app->authManager;
|
|
|
|
$role = $auth->getRole($this->role);
|
|
|
|
if ($role) {
|
|
|
|
if (!$insert) {
|
|
|
|
$auth->revokeAll($this->id);
|
|
|
|
}
|
|
|
|
$auth->assign($role, $this->id);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
}
|