This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

48
models/AuthAssignment.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "auth_assignment".
*
* @property string $item_name
* @property string $user_id
* @property int $created_at
*/
class AuthAssignment extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'auth_assignment';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['item_name', 'user_id'], 'required'],
[['created_at'], 'integer'],
[['item_name', 'user_id'], 'string', 'max' => 64],
[['item_name', 'user_id'], 'unique', 'targetAttribute' => ['item_name', 'user_id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'item_name' => 'Item Name',
'user_id' => 'User ID',
'created_at' => 'Created At',
];
}
}

97
models/AuthItem.php Normal file
View File

@@ -0,0 +1,97 @@
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "auth_item".
*
* @property string $name
* @property int $type
* @property string $description
* @property string $rule_name
* @property resource $data
* @property int $created_at
* @property int $updated_at
*
* @property AuthItemChild[] $authItemChildren
* @property AuthItemChild[] $authItemChildren0
* @property AuthItem[] $children
* @property AuthItem[] $parents
*/
class AuthItem extends \yii\db\ActiveRecord {
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'auth_item';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['name', 'type'], 'required'],
[['type', 'created_at', 'updated_at'], 'integer'],
[['description', 'data'], 'string'],
[['name', 'rule_name'], 'string', 'max' => 64],
[['name'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels() {
return [
'name' => 'Name',
'type' => 'Type',
'description' => 'Description',
'rule_name' => 'Rule Name',
'data' => 'Data',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren() {
return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren0() {
return $this->hasMany(AuthItemChild::className(), ['child' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChildren() {
return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable('auth_item_child', ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParents() {
return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']);
}
public static function roleArray() {
$ls = self::find()->all();
$re = [];
foreach ($ls as $key => $value) {
$re[$value->name] = Yii::t("app", $value->description);
}
return $re;
}
}

75
models/LoginForm.php Normal file
View File

@@ -0,0 +1,75 @@
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model {
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules() {
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params) {
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login() {
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser() {
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}

204
models/User.php Normal file
View File

@@ -0,0 +1,204 @@
<?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' => "Họ tên",
'last_name' => 'Last Name',
'username' => "Tên đăng nhập",
'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;
}
}

70
models/UserSearch.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\User;
/**
* UserSearch represents the model behind the search form of `app\models\User`.
*/
class UserSearch extends User {
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['id'], 'integer'],
[['first_name', 'phone_number', 'username', 'email'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params) {
$query = User::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'first_name', $this->first_name])
->andFilterWhere(['like', 'phone_number', $this->phone_number])
->andFilterWhere(['like', 'username', $this->username])
->andFilterWhere(['like', 'email', $this->email]);
return $dataProvider;
}
}

174
models/common.php Normal file
View File

@@ -0,0 +1,174 @@
<?php
namespace app\models;
use Yii;
class common extends \yii\db\ActiveRecord {
public function formatName($name) {
return preg_replace('/[^a-zA-Z0-9 _-]/', '', $this->vn2latin($name, true));
}
public function vn2latin($cs, $tolower = false) {
/* Mảng chứa tất cả ký tự có dấu trong Tiếng Việt */
$marTViet = array("à", "á", "", "", "ã", "â", "", "", "", "", "", "ă",
"", "", "", "", "", "è", "é", "", "", "", "ê", "",
"ế", "", "", "",
"ì", "í", "", "", "ĩ",
"ò", "ó", "", "", "õ", "ô", "", "", "", "", "", "ơ",
"", "", "", "", "",
"ù", "ú", "", "", "ũ", "ư", "", "", "", "", "",
"", "ý", "", "", "",
"đ",
"À", "Á", "", "", "Ã", "Â", "", "", "", "", "", "Ă",
"", "", "", "", "",
"È", "É", "", "", "", "Ê", "", "", "", "", "",
"Ì", "Í", "", "", "Ĩ",
"Ò", "Ó", "", "", "Õ", "Ô", "", "", "", "", "", "Ơ", "", "", "", "", "",
"Ù", "Ú", "", "", "Ũ", "Ư", "", "", "", "", "",
"", "Ý", "", "", "",
"Đ", " ");
/* Mảng chứa tất cả ký tự không dấu tương ứng với mảng $marTViet bên trên */
$marKoDau = array("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a",
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e",
"i", "i", "i", "i", "i",
"o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o",
"o", "o", "o", "o", "o",
"u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u",
"y", "y", "y", "y", "y",
"d",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A",
"E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E",
"I", "I", "I", "I", "I",
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O",
"U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U",
"Y", "Y", "Y", "Y", "Y",
"D", "-");
if ($tolower) {
return strtolower(str_replace($marTViet, $marKoDau, $cs));
}
return str_replace($marTViet, $marKoDau, $cs);
}
public function formatTime($time, $format = "d/m/Y") {
// $now = time();
// $range = $now - $time;
// if ($range > 60 * 60 * 12) {
return date($format, $time);
// } else {
// return Yii::$app->formatter->asRelativeTime($time);
// }
}
//Upload
public function UploadFile($file, $fileTypes, $dir) {
$LocalPath = "data/" . $dir;
$RootFolder = Yii::getAlias('@webroot') . "/" . $LocalPath;
$name = $_FILES[$file]["name"];
$array = explode(".", $name);
$nr = count($array);
$ext = $array[$nr - 1];
$fileName = preg_replace('/[^a-zA-Z0-9 _-]/', '', $this->vn2latin($array[0], true));
$destfile = time() . "_" . $fileName . "." . $ext;
if (!empty($_FILES)) {
$tempFile = $_FILES[$file]['tmp_name'];
$targetPath = $RootFolder;
if (!file_exists($targetPath)) {
@mkdir($targetPath, 0777, true);
}
$targetFile = $targetPath . '/' . $destfile;
$targetFileLocal = $LocalPath . '/' . $destfile;
// Validate the file type
$fileParts = pathinfo($_FILES[$file]['name']);
if (in_array(strtoupper($fileParts['extension']), $fileTypes)) {
move_uploaded_file($tempFile, $targetFile);
if (file_exists($targetFile)) {
return $targetFileLocal;
} else {
return false;
}
} else {
return 2;
}
}
}
//Move file
public function MoveFile($file, $folder) {
$exp = explode("/", $file);
$t = [];
foreach ($exp as $key => $value) {
if ($key < count($exp) - 1) {
$t[] = $value;
}
}
$folderOld = implode("/", $t);
$root = \Yii::getAlias('@app') . '/web/data/uploads' . trim($folderOld);
chdir($root);
$nameArr = explode(".", basename($file));
$name = $this->formatName($nameArr[0]);
$ext = $nameArr[count($nameArr) - 1];
rename(basename($file), $name . "." . $ext);
$tempdir = $root . "/" . $name . "." . $ext;
if (file_exists($tempdir)) {
$destfile = basename($tempdir);
$dir = Yii::getAlias('@webroot') . "/data/uploads" . $folder;
$file_dir = $dir . '/' . $destfile;
if (!file_exists($dir)) {
@mkdir($dir, 0777, true);
}
$output = shell_exec("mv {$tempdir} {$file_dir}");
$link = $folder . "/" . $destfile;
} else {
$link = $file;
}
return $link;
}
//File size
public function filesize_formatted($path) {
$size = filesize($path);
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . " " . $units[$power];
}
public function getExtension($file) {
$f = basename($file);
$ls = explode(".", $file);
return strtolower($ls[count($ls) - 1]);
}
public function convertIntToTime($seconds) {
$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);
if ($hours > 0) {
return sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
} else {
return sprintf('%02d:%02d', $mins, $secs);
}
}
public function visiblePharse($stt, $tab, $pharse) {
$visible = false;
if ($stt != 0) {
if ($stt == -1) {
if ($tab == $pharse) {
$visible = true;
}
} else {
$visible = true;
}
} else {
$visible = true;
}
return $visible;
}
}