216 lines
6.4 KiB
PHP
216 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
use yii\web\IdentityInterface;
|
||
|
||
/**
|
||
* This is the model class for table "{{%sys_admin}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $user_id 用户ID
|
||
* @property int $is_delete 是否删除,0=否,1=是
|
||
* @property int $created_at 添加时间
|
||
* @property int $creator_user_id 创建者用户ID
|
||
*/
|
||
class Admin extends \yii\db\ActiveRecord implements IdentityInterface
|
||
{
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%sys_admin}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['cx_mch_id', 'user_id', 'is_delete', 'created_at', 'creator_user_id'], 'integer'],
|
||
[['user_id'], 'required'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'user_id' => '用户ID',
|
||
'is_delete' => '是否删除,0=否,1=是',
|
||
'created_at' => '添加时间',
|
||
'creator_user_id' => '创建者用户ID',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Validates password
|
||
*
|
||
* @param string $password password to validate
|
||
* @return boolean if password provided is valid for current user
|
||
*/
|
||
public function validatePassword($password)
|
||
{
|
||
$user = User::findOne([
|
||
"id" => $this->user_id,
|
||
"is_delete" => 0,
|
||
"status" => User::STATUS_NORMAL
|
||
]);
|
||
if($user == null){
|
||
return false;
|
||
} else {
|
||
return Yii::$app->security->validatePassword($password, $user->password);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Generates password hash from password and sets it to the model
|
||
*
|
||
* @param string $password
|
||
*/
|
||
public function setPassword($password)
|
||
{
|
||
$user = User::findOne([
|
||
"id" => $this->user_id,
|
||
"is_delete" => 0,
|
||
"status" => User::STATUS_NORMAL
|
||
]);
|
||
if($user != null){
|
||
$user->password = Yii::$app->security->generatePasswordHash($password);
|
||
}
|
||
return $user->save();
|
||
}
|
||
|
||
/**
|
||
* Finds an identity by the given ID.
|
||
* @param string|int $id the ID to be looked for
|
||
* @return IdentityInterface the identity object that matches the given ID.
|
||
* Null should be returned if such an identity cannot be found
|
||
* or the identity is not in an active state (disabled, deleted, etc.)
|
||
*/
|
||
public static function findIdentity($id)
|
||
{
|
||
$admin = Admin::findOne([
|
||
"id" => $id,
|
||
"is_delete" => 0
|
||
]);
|
||
return $admin;
|
||
}
|
||
/**
|
||
* 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 the identity object that matches the given token.
|
||
* Null should be returned if such an identity cannot be found
|
||
* or the identity is not in an active state (disabled, deleted, etc.)
|
||
*/
|
||
public static function findIdentityByAccessToken($token, $type = null)
|
||
{
|
||
$user_token = UserToken::find()
|
||
->where([
|
||
'is_delete' => 0,
|
||
'token' => $token,
|
||
'cx_mch_id' => Model::getCxMchId()
|
||
])
|
||
->andFilterWhere([
|
||
'type' => $type,
|
||
])->one();
|
||
if($user_token && $user_token->expire_time > time()){
|
||
return static::findOne([
|
||
'id' => $user_token->user_id,
|
||
'is_delete' => 0,
|
||
'status' => self::STATUS_NORMAL,
|
||
]);
|
||
}
|
||
return static::findOne([
|
||
'id' => 0,
|
||
'is_delete' => 0,
|
||
'status' => self::STATUS_NORMAL,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Returns an ID that can uniquely identify a user identity.
|
||
* @return string|int an ID that uniquely identifies a user identity.
|
||
*/
|
||
public function getId()
|
||
{
|
||
return $this->id;
|
||
}
|
||
|
||
/**
|
||
* Returns a key that can be used to check the validity of a given identity ID.
|
||
*
|
||
* The key should be unique for each individual user, and should be persistent
|
||
* so that it can be used to check the validity of the user identity.
|
||
*
|
||
* The space of such keys should be big enough to defeat potential identity attacks.
|
||
*
|
||
* This is required if [[User::enableAutoLogin]] is enabled.
|
||
* @return string a key that is used to check the validity of a given identity ID.
|
||
* @see validateAuthKey()
|
||
*/
|
||
public function getAuthKey()
|
||
{
|
||
$user = User::findOne([
|
||
"id" => $this->user_id,
|
||
"is_delete" => 0,
|
||
"status" => User::STATUS_NORMAL
|
||
]);
|
||
if($user != null){
|
||
return $user->auth_key;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* Validates the given auth key.
|
||
*
|
||
* This is required if [[User::enableAutoLogin]] is enabled.
|
||
* @param string $authKey the given auth key
|
||
* @return bool whether the given auth key is valid.
|
||
* @see getAuthKey()
|
||
*/
|
||
public function validateAuthKey($authKey)
|
||
{
|
||
return $this->getAuthKey() === $authKey;
|
||
}
|
||
|
||
|
||
public function getUser()
|
||
{
|
||
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
||
}
|
||
|
||
|
||
public function getCreator()
|
||
{
|
||
return $this->hasOne(User::className(), ['id' => 'creator_user_id']);
|
||
}
|
||
|
||
public static function lastLogin($user_id, $is_pass = 1, $err_msg = null, $cx_mch_id = 0)
|
||
{
|
||
return log\UserLoginLog::logger($user_id, $is_pass, $err_msg, $cx_mch_id);
|
||
}
|
||
}
|