init
This commit is contained in:
183
controllers/UserController.php
Normal file
183
controllers/UserController.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?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'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionIndex() {
|
||||
if (!Yii::$app->user->can('administrator'))
|
||||
throw new \yii\web\HttpException(403, 'Bạn không có quyền truy cập nội dung này');
|
||||
|
||||
$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
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionCreate() {
|
||||
Yii::$app->response->format = 'json';
|
||||
if (!Yii::$app->user->can('administrator'))
|
||||
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();
|
||||
$check = User::findOne(['username' => $post['Username']]);
|
||||
if ($check)
|
||||
return ['stt' => false, 'reason' => 'username'];
|
||||
|
||||
$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 {
|
||||
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(['create'])
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionUpdate($id) {
|
||||
Yii::$app->response->format = 'json';
|
||||
if (!Yii::$app->user->can('administrator'))
|
||||
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 ['stt' => false, 'reason' => 'username'];
|
||||
}
|
||||
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 ['stt' => true];
|
||||
} else {
|
||||
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(['update', 'id' => $id])
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionDelete($id) {
|
||||
if (!Yii::$app->user->can('administrator'))
|
||||
throw new \yii\web\HttpException(403, 'Bạn không có quyền truy cập nội dung này');
|
||||
|
||||
$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('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 {
|
||||
$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 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 {
|
||||
$this->view->title = Yii::t('app', 'Đổi mật khẩu');
|
||||
$this->view->params['breadcrumbs'][] = $this->view->title;
|
||||
|
||||
return $this->render('password', [
|
||||
'model' => $model
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user