You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.2 KiB
161 lines
4.2 KiB
7 years ago
|
<?php
|
||
|
|
||
|
namespace backend\controllers;
|
||
|
|
||
|
use core\forms\user\UserForm;
|
||
|
use core\services\user\UserManageService;
|
||
|
use Yii;
|
||
|
use core\entities\user\User;
|
||
|
use backend\forms\UserSearch;
|
||
|
use yii\web\Controller;
|
||
|
use yii\web\NotFoundHttpException;
|
||
|
use yii\filters\VerbFilter;
|
||
|
use yii\filters\AccessControl;
|
||
|
|
||
|
/**
|
||
|
* UserController implements the CRUD actions for User model.
|
||
|
*/
|
||
|
class UserController extends Controller
|
||
|
{
|
||
|
private $service;
|
||
|
|
||
|
public function __construct($id, $module, UserManageService $service, $config = [])
|
||
|
{
|
||
|
parent::__construct($id, $module, $config);
|
||
|
$this->service = $service;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @inheritdoc
|
||
|
*/
|
||
|
public function behaviors()
|
||
|
{
|
||
|
return [
|
||
|
'access' => [
|
||
|
'class' => AccessControl::className(),
|
||
|
'rules' => [
|
||
|
[
|
||
|
'actions' => ['create','view','index', 'update', 'delete'],
|
||
|
'allow' => true,
|
||
|
'roles' => ['UserManagement'],
|
||
|
],
|
||
|
[ // all the action are accessible to admin
|
||
|
'allow' => true,
|
||
|
'roles' => ['admin'],
|
||
|
],
|
||
|
],
|
||
|
],
|
||
|
'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->render('index', [
|
||
|
'searchModel' => $searchModel,
|
||
|
'dataProvider' => $dataProvider,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $id
|
||
|
*
|
||
|
* @return string
|
||
|
* @throws NotFoundHttpException
|
||
|
*/
|
||
|
public function actionView($id)
|
||
|
{
|
||
|
return $this->render('view', [
|
||
|
'model' => $this->findModel($id),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Creates a new User model.
|
||
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function actionCreate()
|
||
|
{
|
||
|
$form = new UserForm();
|
||
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
|
||
|
try {
|
||
|
$user = $this->service->create($form);
|
||
|
return $this->redirect(['view', 'id' => $user->id]);
|
||
|
} catch (\DomainException $e) {
|
||
|
Yii::$app->errorHandler->logException($e);
|
||
|
Yii::$app->session->setFlash('error', $e->getMessage());
|
||
|
}
|
||
|
}
|
||
|
return $this->render('create', [
|
||
|
'model' => $form,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $id
|
||
|
*
|
||
|
* @return string|\yii\web\Response
|
||
|
* @throws NotFoundHttpException
|
||
|
*/
|
||
|
public function actionUpdate($id)
|
||
|
{
|
||
|
$user = $this->findModel($id);
|
||
|
|
||
|
$form = new UserForm($user);
|
||
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
|
||
|
try {
|
||
|
$this->service->edit($user->id, $form);
|
||
|
return $this->redirect(['view', 'id' => $user->id]);
|
||
|
} catch (\DomainException $e) {
|
||
|
Yii::$app->errorHandler->logException($e);
|
||
|
Yii::$app->session->setFlash('error', $e->getMessage());
|
||
|
}
|
||
|
}
|
||
|
return $this->render('update', [
|
||
|
'model' => $form,
|
||
|
'user' => $user,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Deletes an existing User model.
|
||
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
||
|
* @param integer $id
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function actionDelete($id)
|
||
|
{
|
||
|
$this->service->remove($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;
|
||
|
} else {
|
||
|
throw new NotFoundHttpException('The requested page does not exist.');
|
||
|
}
|
||
|
}
|
||
|
}
|