user->isGuest) {
return $this->redirect(['/site/login']);
}
if (!Yii::$app->user->can("administrator")) {
return $this->redirect(["/dashboard"]);
}
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all User models.
* @return mixed
*/
public function actionIndex() {
$this->view->title = "Người dùng";
$this->view->params['breadcrumbs'][] = "Hệ thống";
$this->view->params['breadcrumbs'][] = $this->view->title;
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('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" => " " . 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() {
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 = 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
];
} else {
Yii::$app->response->format = "json";
return [
"title" => " " . 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) {
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->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->quota = $post['Quota'];
$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);
}
}
return true;
} else {
Yii::$app->response->format = "json";
return [
"title" => " " . 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->user->can("administrator")) {
throw new \yii\web\ForbiddenHttpException(Yii::t("app", "Bạn không có quyền truy cập!"));
}
$this->findModel($id)->delete();
AuthAssignment::deleteAll(['user_id' => $id]);
return $this->redirect(['index']);
}
/**
* 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() {
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
$model = $this->findModel(Yii::$app->user->id);
$this->view->title = Yii::t("app", "Thông tin cá nhân");
$this->view->params['breadcrumbs'][] = $this->view->title;
return $this->render('profiles', [
"model" => $model
]);
}
public function actionInfo($id) {
if (Yii::$app->request->post()) {
$model = $this->findModel($id);
$post = Yii::$app->request->post();
$model->first_name = $post['Name'];
$model->phone_number = $post['PhoneNumber'];
$model->email = $post['Email'];
return $model->save();
}
}
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 {
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
$this->view->title = Yii::t("app", "Đổi mật khẩu");
$this->view->params['breadcrumbs'][] = $this->view->title;
return $this->render('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;
}
}
}