221 lines
6.1 KiB
PHP
Executable File
221 lines
6.1 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* @Author: fm453
|
|
* @Date: 2021-09-10 17:33:23
|
|
* @Last Modified by: fm453
|
|
* @Last Modified time: 2021-09-10 17:34:36
|
|
* @Email: fm453@lukegzs.com
|
|
*/
|
|
|
|
namespace backend\models;
|
|
|
|
use Yii;
|
|
use yii\db\ActiveRecord;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\base\NotSupportedException;
|
|
use backend\models\AuthAssignment;
|
|
use yii\web\IdentityInterface;
|
|
|
|
class Adminer extends ActiveRecord implements IdentityInterface
|
|
{
|
|
|
|
public $password;
|
|
|
|
const STATUS_DELETE = 0;
|
|
const STATUS_ACTIVE = 10;
|
|
|
|
public static $statusTexts = [
|
|
self::STATUS_DELETE => '禁用',
|
|
self::STATUS_ACTIVE => '启用',
|
|
];
|
|
|
|
public static $statusStyles = [
|
|
self::STATUS_DELETE => 'label-warning',
|
|
self::STATUS_ACTIVE => 'label-info',
|
|
];
|
|
|
|
public static function tableName() {
|
|
return '{{%c_adminer}}';
|
|
}
|
|
|
|
public function behaviors() {
|
|
return [
|
|
TimestampBehavior::className(),
|
|
];
|
|
}
|
|
|
|
public function rules() {
|
|
return [
|
|
[['username', 'email'], 'required'],
|
|
[['username'], 'string', 'length' => [5, 32]],
|
|
[['nickname'],'string','max'=>32],
|
|
[['password'], 'required', 'on' => ['create']],
|
|
[['auth_key'], 'string', 'length' => 32],
|
|
[['username', 'email','mobile'], 'unique'],
|
|
[['email'], 'email'],
|
|
['status', 'default', 'value' => self::STATUS_ACTIVE],
|
|
['status', 'in', 'range' => [
|
|
self::STATUS_DELETE,self::STATUS_ACTIVE,1
|
|
]], //状态值1是软删除
|
|
[['status', 'created_at', 'updated_at'], 'integer'],
|
|
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
|
|
//去除首尾空白
|
|
[['username', 'nickname', 'mobile', 'email'], 'filter', 'filter' => 'trim'],
|
|
|
|
];
|
|
}
|
|
|
|
public function attributeLabels() {
|
|
return [
|
|
'id' => 'ID',
|
|
'username' => Yii::t('common' ,'Username'),
|
|
'nickname' => '姓名',
|
|
'auth_key' => Yii::t('common' ,'Password'),
|
|
'password_hash' => 'Password Hash',
|
|
'email' => Yii::t('common' ,'Email'),
|
|
'mobile' => Yii::t('common' ,'Mobile'),
|
|
'status' => '状态',
|
|
'created_at' => 'Created At',
|
|
'updated_at' => 'Updated At',
|
|
'user_role' => '所属角色',
|
|
'role' => Yii::t('common' ,'Role'),
|
|
];
|
|
}
|
|
|
|
public function scenarios() {
|
|
$scenarios = parent::scenarios();
|
|
$scenarios['create'] = ['username', 'email', 'password', 'status'];
|
|
$scenarios['update'] = ['username', 'email', 'password', 'status'];
|
|
return $scenarios;
|
|
}
|
|
|
|
/*用户组 S*/
|
|
public function getUsergrouplist()
|
|
{
|
|
/**
|
|
* 第一个参数为要关联的字表模型类名称,
|
|
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
|
|
*/
|
|
return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
|
|
}
|
|
public function getUsergroup()
|
|
{
|
|
/**
|
|
* 第一个参数为要关联的字表模型类名称,
|
|
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
|
|
*/
|
|
return $this->hasOne(AuthAssignment::className(), ['user_id' => 'id']);
|
|
}
|
|
/*用户组 E*/
|
|
|
|
/**
|
|
* Generates "remember me" authentication key
|
|
*/
|
|
public function generateAuthKey()
|
|
{
|
|
$this->auth_key = Yii::$app->security->generateRandomString();
|
|
}
|
|
|
|
public function beforeSave($insert) {
|
|
if($this->isNewRecord) {
|
|
$this->generateAuthKey();
|
|
}
|
|
if(!empty($this->password)) $this->setPassword($this->password);
|
|
return parent::beforeSave($insert);
|
|
}
|
|
|
|
/**
|
|
* 生成加密后的密码
|
|
* @param [string] $password [用户密码]
|
|
*/
|
|
public function setPassword($password) {
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
|
|
}
|
|
|
|
/*获取所有用户*/
|
|
public function get_all_user(){
|
|
// $user = Yii::$app->db->createCommand('select * from '. tableName())->queryAll();
|
|
$user = static::findAll();
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* 获取账号信息
|
|
* @param [int] $id [后台用户id]
|
|
*/
|
|
public static function findIdentity($id) {
|
|
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
|
|
}
|
|
|
|
public static function findIdentityByAccessToken($token, $type = null) {
|
|
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
|
|
}
|
|
|
|
/**
|
|
* 获取账号信息
|
|
* @param [string] $username [用户名]
|
|
*/
|
|
public static function findByUsername($username) {
|
|
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
|
|
}
|
|
|
|
//获取用户所在的用户组
|
|
public function getGroup() {
|
|
return implode(',', array_keys(Yii::$app->authManager->getRolesByUser($this->id)));
|
|
}
|
|
|
|
/**
|
|
* 获取账号主键值(id)
|
|
*/
|
|
public function getId() {
|
|
return $this->getPrimaryKey();
|
|
}
|
|
|
|
/**
|
|
* 获取账号auth_key值
|
|
*/
|
|
public function getAuthKey() {
|
|
return $this->auth_key;
|
|
}
|
|
|
|
/**
|
|
* 验证auth_key
|
|
* @param [string] $authKey [auth key]
|
|
*/
|
|
public function validateAuthKey($authKey) {
|
|
return $this->getAuthKey() === $authKey;
|
|
}
|
|
|
|
/**
|
|
* 验证用户密码
|
|
* @param [string] $password [用户密码]
|
|
*/
|
|
public function validatePassword($password) {
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取账号状态
|
|
*/
|
|
public function getStatus() {
|
|
return self::$statusTexts[$this->status];
|
|
}
|
|
|
|
/**
|
|
* 获取账号状态样式
|
|
*/
|
|
public function getStatusStyle() {
|
|
return self::$statusStyles[$this->status];
|
|
}
|
|
|
|
/**
|
|
* 获取状态
|
|
*/
|
|
public function getStatusTexts() {
|
|
return self::$statusTexts;
|
|
}
|
|
|
|
}
|