273 lines
8.7 KiB
PHP
273 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace app\controllers;
|
|
|
|
use Yii;
|
|
use app\models\User;
|
|
use app\models\UserSearch;
|
|
use yii\web\Controller;
|
|
use yii\web\NotFoundHttpException;
|
|
use yii\filters\VerbFilter;
|
|
use yii\helpers\Url;
|
|
use app\models\AuthItem;
|
|
use app\models\AuthAssignment;
|
|
|
|
/**
|
|
* UserController implements the CRUD actions for User model.
|
|
*/
|
|
class UserController extends Controller {
|
|
|
|
public function init() {
|
|
parent::init();
|
|
if (Yii::$app->user->isGuest) {
|
|
return $this->redirect(['/site/login']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function behaviors() {
|
|
return [
|
|
'verbs' => [
|
|
'class' => VerbFilter::className(),
|
|
'actions' => [
|
|
'delete' => ['POST'],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Lists all User models.
|
|
* @return mixed
|
|
*/
|
|
public function actionIndex() {
|
|
$searchModel = new UserSearch();
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
|
return $this->renderAjax("index", [
|
|
'searchModel' => $searchModel,
|
|
'dataProvider' => $dataProvider
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Displays a single User model.
|
|
* @param integer $id
|
|
* @return mixed
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
*/
|
|
public function actionView($id) {
|
|
if (!Yii::$app->user->can("administrator")) {
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => "403",
|
|
"form" => Yii::t("app", "Bạn không có quyền truy cập!")
|
|
];
|
|
}
|
|
|
|
$model = $this->findModel($id);
|
|
if (Yii::$app->request->isAjax) {
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => "<i class='fa fa-user'></i> " . Yii::t("app", "Thông tin người dùng"),
|
|
"form" => $this->renderPartial("view", [
|
|
"model" => $model
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a new User model.
|
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
|
* @return mixed
|
|
*/
|
|
public function actionCreate() {
|
|
$model = new User();
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->post();
|
|
Yii::$app->response->format = "json";
|
|
$check = User::findOne(["username" => $post['Username']]);
|
|
if ($check) {
|
|
return [
|
|
'stt' => false,
|
|
"reason" => 'username'
|
|
];
|
|
}
|
|
$check2 = User::findOne(['email' => $post['Email']]);
|
|
if ($check2) {
|
|
return [
|
|
'stt' => false,
|
|
"reason" => 'email'
|
|
];
|
|
}
|
|
$user_id = $model->create($post);
|
|
$auth = Yii::$app->authManager;
|
|
foreach ($post['Role'] as $key => $value) {
|
|
$role = $auth->getRole($value);
|
|
if ($role != null) {
|
|
$auth->assign($role, $user_id);
|
|
}
|
|
}
|
|
return [
|
|
'stt' => true,
|
|
'url' => Url::to(['/user'])
|
|
];
|
|
} else {
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => "<i class='fa fa-plus-circle'></i> " . Yii::t("app", "Tạo người dùng mới"),
|
|
"form" => $this->renderPartial("form", [
|
|
"model" => $model,
|
|
"roles" => AuthItem::roleArray(),
|
|
"url" => Url::to(['/user/create'])
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates an existing User model.
|
|
* If update is successful, the browser will be redirected to the 'view' page.
|
|
* @param integer $id
|
|
* @return mixed
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
*/
|
|
public function actionUpdate($id) {
|
|
$model = $this->findModel($id);
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->post();
|
|
if ($post['Username'] !== $model->username) {
|
|
$check = User::findOne(["username" => $post['Username']]);
|
|
if ($check)
|
|
return false;
|
|
}
|
|
if ($post['Password'] !== "") {
|
|
$model->password = md5($post['Password']);
|
|
}
|
|
$model->first_name = $post['Name'];
|
|
$model->username = $post['Username'];
|
|
$model->phone_number = $post['PhoneNumber'];
|
|
$model->email = $post['Email'];
|
|
$model->save();
|
|
AuthAssignment::deleteAll(['user_id' => $id]);
|
|
$auth = Yii::$app->authManager;
|
|
foreach ($post['Role'] as $key => $value) {
|
|
$role = $auth->getRole($value);
|
|
if ($role != null) {
|
|
$auth->assign($role, $id);
|
|
}
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
'stt' => true,
|
|
'url' => Url::to(['/user'])
|
|
];
|
|
} else {
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => "<i class='fa fa-edit'></i> " . Yii::t("app", "Sửa thông tin người dùng"),
|
|
"form" => $this->renderPartial("form", [
|
|
"model" => $model,
|
|
"roles" => AuthItem::roleArray(),
|
|
"url" => Url::to(['/user/update', 'id' => $id])
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes an existing User model.
|
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
|
* @param integer $id
|
|
* @return mixed
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
*/
|
|
public function actionDelete($id) {
|
|
if (Yii::$app->request->isAjax) {
|
|
$this->findModel($id)->delete();
|
|
AuthAssignment::deleteAll(['user_id' => $id]);
|
|
Yii::$app->response->format = "json";
|
|
return ["url" => Url::to(['/user'])];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Finds the User model based on its primary key value.
|
|
* If the model is not found, a 404 HTTP exception will be thrown.
|
|
* @param integer $id
|
|
* @return User the loaded model
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
*/
|
|
protected function findModel($id) {
|
|
if (($model = User::findOne($id)) !== null) {
|
|
return $model;
|
|
}
|
|
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
|
}
|
|
|
|
public function actionProfiles() {
|
|
$model = $this->findModel(Yii::$app->user->id);
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->post();
|
|
$model->first_name = $post['Name'];
|
|
$model->phone_number = $post['PhoneNumber'];
|
|
$model->email = $post['Email'];
|
|
return $model->save();
|
|
} else {
|
|
Yii::$app->response->format = "json";
|
|
if (Yii::$app->user->isGuest) {
|
|
return [
|
|
"title" => "Lỗi",
|
|
"form" => "Bạn chưa đăng nhập hệ thống"
|
|
];
|
|
}
|
|
return [
|
|
"title" => "Thông tin cá nhân",
|
|
"form" => $this->renderPartial('profiles', [
|
|
"model" => $model
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionChangePassword() {
|
|
$model = $this->findModel(Yii::$app->user->id);
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->post();
|
|
$model->password = md5($post['NewPassword']);
|
|
$model->save();
|
|
return true;
|
|
} else {
|
|
Yii::$app->response->format = "json";
|
|
if (Yii::$app->user->isGuest) {
|
|
return [
|
|
"title" => "Lỗi",
|
|
"form" => "Bạn chưa đăng nhập hệ thống"
|
|
];
|
|
}
|
|
return [
|
|
"title" => "Đổi mật khẩu",
|
|
"form" => $this->renderPartial('password', [
|
|
"model" => $model
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionAvatar() {
|
|
if (Yii::$app->request->post()) {
|
|
$model = new \app\models\UploadForm();
|
|
$path = "avatar/" . Yii::$app->user->id;
|
|
$url = $model->UploadGlobal("image", ["PNG", "JPG", "JPEG", "GIF"], $path);
|
|
$UserInfo = User::findOne(Yii::$app->user->id);
|
|
$UserInfo->user_image = $url;
|
|
$UserInfo->save();
|
|
return $url;
|
|
}
|
|
}
|
|
|
|
}
|