205 lines
5.4 KiB
PHP
205 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use Yii;
|
|
use yii\db\ActiveRecord;
|
|
|
|
class User extends ActiveRecord implements \yii\web\IdentityInterface {
|
|
|
|
public static function tableName() {
|
|
return 'user';
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules() {
|
|
return [
|
|
[['username', 'password'], 'required'],
|
|
[['email'], 'email'],
|
|
[['username', 'email'], 'unique'],
|
|
[['phone_number'], 'string', 'max' => 30],
|
|
[['username', 'password', 'password_reset_token', 'first_name', 'last_name'], 'string', 'max' => 250],
|
|
[['user_image', 'email'], 'string', 'max' => 500],
|
|
[['quota'], 'integer']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function attributeLabels() {
|
|
return [
|
|
'first_name' => "Tên đăng nhập",
|
|
'last_name' => 'Last Name',
|
|
'username' => "Họ tên",
|
|
'phone_number' => "Điện thoại",
|
|
'email' => "Email",
|
|
'roleName' => "Phân quyền"
|
|
];
|
|
}
|
|
|
|
public static function findIdentity($id) {
|
|
$user = self::find()
|
|
->where([
|
|
"id" => $id
|
|
])
|
|
->one();
|
|
if ($user == null) {
|
|
return null;
|
|
}
|
|
return new static($user);
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public static function findIdentityByAccessToken($token, $userType = null) {
|
|
|
|
$user = self::find()
|
|
->where(["accessToken" => $token])
|
|
->one();
|
|
if (!count($user)) {
|
|
return null;
|
|
}
|
|
return new static($user);
|
|
}
|
|
|
|
/**
|
|
* Finds user by username
|
|
*
|
|
* @param string $username
|
|
* @return static|null
|
|
*/
|
|
public static function findByUsername($username) {
|
|
$user = self::find()
|
|
->where([
|
|
"username" => $username
|
|
])
|
|
->one();
|
|
if ($user == null) {
|
|
return null;
|
|
}
|
|
return new static($user);
|
|
}
|
|
|
|
public static function findByUser($username) {
|
|
$user = self::find()
|
|
->where([
|
|
"username" => $username
|
|
])
|
|
->one();
|
|
if ($user == null) {
|
|
return null;
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function getId() {
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function getAuthKey() {
|
|
return $this->authKey;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function validateAuthKey($authKey) {
|
|
return $this->authKey === $authKey;
|
|
}
|
|
|
|
/**
|
|
* Validates password
|
|
*
|
|
* @param string $password password to validate
|
|
* @return boolean if password provided is valid for current user
|
|
*/
|
|
public function validatePassword($password) {
|
|
return $this->password === md5($password);
|
|
}
|
|
|
|
public function create($data) {
|
|
$r = $this->load([
|
|
'first_name' => $data['Name'],
|
|
'username' => $data['Username'],
|
|
'password' => $data['Password'] !== "" ? md5($data['Password']) : md5('123456a@'),
|
|
'phone_number' => $data['PhoneNumber'],
|
|
'email' => $data['Email']
|
|
], '');
|
|
if ($r) {
|
|
try {
|
|
$this->save();
|
|
return $this->id;
|
|
} catch (\Exception $ex) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getRole() {
|
|
return AuthAssignment::findOne(['user_id' => $this->id])->item_name;
|
|
}
|
|
|
|
public function getRoleName() {
|
|
$roles = $this->roles;
|
|
$lists = "";
|
|
foreach ($roles as $key => $value) {
|
|
$lists .= \yii\helpers\Html::label(Yii::t("app", AuthItem::findOne(['name' => $value])->description), "", ["class" => "label label-info"]) . " ";
|
|
}
|
|
return $lists;
|
|
}
|
|
|
|
public function getRoleNameLists() {
|
|
$roles = $this->roles;
|
|
$lists = [];
|
|
foreach ($roles as $key => $value) {
|
|
$lists[] = AuthItem::findOne(['name' => $value])->description;
|
|
}
|
|
return implode(",", $lists);
|
|
}
|
|
|
|
public function getRoles() {
|
|
$roleArray = [];
|
|
$roles = AuthAssignment::find()->andWhere(['user_id' => $this->id])->all();
|
|
foreach ($roles as $key => $value) {
|
|
$roleArray[] = $value->item_name;
|
|
}
|
|
return $roleArray;
|
|
}
|
|
|
|
public static function securityArray($all = false) {
|
|
$ls = AuthAssignment::find()->andWhere(['IN', 'item_name', ["security"]])->all();
|
|
$temp = [];
|
|
foreach ($ls as $key => $value) {
|
|
$temp[] = $value->user_id;
|
|
}
|
|
$lsID = array_unique($temp);
|
|
$re = [];
|
|
foreach ($lsID as $key => $value) {
|
|
if ($all) {
|
|
$re[$value] = self::findOne($value)->first_name;
|
|
} else {
|
|
$check = Gate::findOne(['user_id' => $value]);
|
|
if (!$check)
|
|
$re[$value] = self::findOne($value)->first_name;
|
|
}
|
|
}
|
|
return $re;
|
|
}
|
|
|
|
public function getAvatar() {
|
|
$directoryAsset = Yii::$app->assetManager->getPublishedUrl('@vendor/almasaeed2010/adminlte/dist');
|
|
return $this->user_image == null ? $directoryAsset . "/img/user2-160x160.jpg" : Yii::getAlias("@images_folder") . $this->user_image;
|
|
}
|
|
|
|
}
|