language = Yii::$app->session->get("language") ? Yii::$app->session->get("language")["name"] : Yii::$app->language; if (Yii::$app->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 ]) ]; } } 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']) ]) ]; } } 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->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]) ]) ]; } } 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']); } protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException(Yii::t("app", "KHONG_TIM_THAY_THONG_TIN")); } 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", "THONG_TIN_CA_NHAN"); $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", "DOI_MAT_KHAU"); $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; } } }