2024-03-21 14:01:44 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
|
2024-03-22 14:48:35 +08:00
|
|
|
use app\models\User;
|
|
|
|
use app\models\UserSearch;
|
|
|
|
use Throwable;
|
|
|
|
use yii\db\StaleObjectException;
|
2024-03-21 14:01:44 +08:00
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\filters\VerbFilter;
|
|
|
|
use yii\web\Controller;
|
2024-03-22 14:48:35 +08:00
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
use yii\web\Response;
|
2024-03-21 14:01:44 +08:00
|
|
|
|
|
|
|
class AdminController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function behaviors(): array
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
parent::behaviors(),
|
|
|
|
[
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
|
|
|
'allow' => true,
|
2024-03-22 14:48:35 +08:00
|
|
|
'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete'],
|
2024-03-21 14:01:44 +08:00
|
|
|
'roles' => ['admin'], // only admin can do these
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'verbs' => [
|
|
|
|
'class' => VerbFilter::class,
|
|
|
|
'actions' => [
|
|
|
|
'index' => ['GET'],
|
|
|
|
'system' => ['GET'],
|
|
|
|
'user' => ['GET'],
|
2024-03-22 14:48:35 +08:00
|
|
|
'user-view' => ['GET'],
|
|
|
|
'user-create' => ['GET', 'POST'],
|
|
|
|
'user-update' => ['GET', 'POST'],
|
|
|
|
'user-delete' => ['POST'],
|
2024-03-21 14:01:44 +08:00
|
|
|
'info' => ['GET'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function init(): void
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
$this->layout = 'admin_main';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function actionIndex(): string
|
|
|
|
{
|
|
|
|
return $this->render('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function actionSystem(): string
|
|
|
|
{
|
|
|
|
return $this->render('system');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-22 14:48:35 +08:00
|
|
|
* Lists all User.
|
|
|
|
*
|
2024-03-21 14:01:44 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function actionUser(): string
|
|
|
|
{
|
2024-03-22 14:48:35 +08:00
|
|
|
$searchModel = new UserSearch();
|
|
|
|
$dataProvider = $searchModel->search($this->request->queryParams);
|
|
|
|
|
|
|
|
return $this->render('user', [
|
|
|
|
'searchModel' => $searchModel,
|
|
|
|
'dataProvider' => $dataProvider,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a single User model.
|
|
|
|
* @param int $id ID
|
|
|
|
* @return string
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
*/
|
|
|
|
public function actionUserView(int $id): string
|
|
|
|
{
|
|
|
|
return $this->render('user_view', [
|
|
|
|
'model' => $this->findModel($id),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new User model.
|
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
|
|
|
* @return string|Response
|
|
|
|
*/
|
|
|
|
public function actionUserCreate(): Response|string
|
|
|
|
{
|
|
|
|
$model = new User();
|
|
|
|
|
|
|
|
if ($this->request->isPost) {
|
|
|
|
if ($model->load($this->request->post()) && $model->save()) {
|
|
|
|
return $this->redirect(['user_view', 'id' => $model->id]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$model->loadDefaultValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('user_create', [
|
|
|
|
'model' => $model,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing User model.
|
|
|
|
* If update is successful, the browser will be redirected to the 'view' page.
|
|
|
|
* @param int $id ID
|
|
|
|
* @return string|Response
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
*/
|
|
|
|
public function actionUserUpdate(int $id): Response|string
|
|
|
|
{
|
|
|
|
$model = $this->findModel($id);
|
|
|
|
|
|
|
|
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
|
|
|
|
return $this->redirect(['user_view', 'id' => $model->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('user_update', [
|
|
|
|
'model' => $model,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an existing User model.
|
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
|
|
|
* @param int $id ID
|
|
|
|
* @return Response
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
* @throws Throwable
|
|
|
|
* @throws StaleObjectException
|
|
|
|
*/
|
|
|
|
public function actionUserDelete(int $id): Response
|
|
|
|
{
|
|
|
|
$this->findModel($id)->delete();
|
|
|
|
|
|
|
|
return $this->redirect(['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 int $id ID
|
|
|
|
* @return User the loaded model
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
*/
|
|
|
|
protected function findModel(int $id): User
|
|
|
|
{
|
|
|
|
if (($model = User::findOne(['id' => $id])) !== null) {
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
2024-03-21 14:01:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function actionInfo(): string
|
|
|
|
{
|
|
|
|
return $this->render('info');
|
|
|
|
}
|
|
|
|
}
|