From 39287634181ff214dae3fbf294213c93e8abc98b Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Fri, 22 Mar 2024 14:48:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD(1/5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 110 +++++++++++++++++++++++++++++++- models/UserSearch.php | 21 +++++- views/admin/_user_add_form.php | 42 ++++++++++++ views/admin/_user_form.php | 27 ++++++++ views/admin/user.php | 81 +++++++++++++++++++++-- views/admin/user_create.php | 20 ++++++ views/admin/user_update.php | 21 ++++++ views/admin/user_view.php | 57 +++++++++++++++++ 8 files changed, 367 insertions(+), 12 deletions(-) create mode 100644 views/admin/_user_add_form.php create mode 100644 views/admin/_user_form.php create mode 100644 views/admin/user_create.php create mode 100644 views/admin/user_update.php create mode 100644 views/admin/user_view.php diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 2a9f2e9..05d993b 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -2,10 +2,15 @@ namespace app\controllers; -use Yii; +use app\models\User; +use app\models\UserSearch; +use Throwable; +use yii\db\StaleObjectException; use yii\filters\AccessControl; use yii\filters\VerbFilter; use yii\web\Controller; +use yii\web\NotFoundHttpException; +use yii\web\Response; class AdminController extends Controller { @@ -22,7 +27,7 @@ class AdminController extends Controller 'rules' => [ [ 'allow' => true, - 'actions' => ['index', 'system', 'user', 'info'], + 'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete'], 'roles' => ['admin'], // only admin can do these ] ], @@ -33,6 +38,10 @@ class AdminController extends Controller 'index' => ['GET'], 'system' => ['GET'], 'user' => ['GET'], + 'user-view' => ['GET'], + 'user-create' => ['GET', 'POST'], + 'user-update' => ['GET', 'POST'], + 'user-delete' => ['POST'], 'info' => ['GET'], ], ], @@ -63,11 +72,106 @@ class AdminController extends Controller } /** + * Lists all User. + * * @return string */ public function actionUser(): string { - return $this->render('user'); + $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.'); } /** diff --git a/models/UserSearch.php b/models/UserSearch.php index 129282f..e8bfcd1 100644 --- a/models/UserSearch.php +++ b/models/UserSearch.php @@ -16,8 +16,8 @@ class UserSearch extends User public function rules(): array { return [ - [['id', 'status'], 'integer'], - [['username', 'password', 'auth_key', 'email'], 'safe'], + [['id', 'status', 'is_encryption_enabled', 'is_otp_enabled', 'storage_limit', 'dark_mode'], 'integer'], + [['username', 'name', 'password', 'auth_key', 'email', 'created_at', 'last_login', 'last_login_ip', 'bio', 'role', 'encryption_key', 'otp_secret', 'recovery_codes', 'vault_secret', 'vault_salt'], 'safe'], ]; } @@ -59,12 +59,27 @@ class UserSearch extends User $query->andFilterWhere([ 'id' => $this->id, 'status' => $this->status, + 'created_at' => $this->created_at, + 'last_login' => $this->last_login, + 'is_encryption_enabled' => $this->is_encryption_enabled, + 'is_otp_enabled' => $this->is_otp_enabled, + 'storage_limit' => $this->storage_limit, + 'dark_mode' => $this->dark_mode, ]); $query->andFilterWhere(['like', 'username', $this->username]) + ->andFilterWhere(['like', 'name', $this->name]) ->andFilterWhere(['like', 'password', $this->password]) ->andFilterWhere(['like', 'auth_key', $this->auth_key]) - ->andFilterWhere(['like', 'email', $this->email]); + ->andFilterWhere(['like', 'email', $this->email]) + ->andFilterWhere(['like', 'last_login_ip', $this->last_login_ip]) + ->andFilterWhere(['like', 'bio', $this->bio]) + ->andFilterWhere(['like', 'role', $this->role]) + ->andFilterWhere(['like', 'encryption_key', $this->encryption_key]) + ->andFilterWhere(['like', 'otp_secret', $this->otp_secret]) + ->andFilterWhere(['like', 'recovery_codes', $this->recovery_codes]) + ->andFilterWhere(['like', 'vault_secret', $this->vault_secret]) + ->andFilterWhere(['like', 'vault_salt', $this->vault_salt]); return $dataProvider; } diff --git a/views/admin/_user_add_form.php b/views/admin/_user_add_form.php new file mode 100644 index 0000000..e2b7178 --- /dev/null +++ b/views/admin/_user_add_form.php @@ -0,0 +1,42 @@ + + +
+ + + + field($model, 'username')->textInput(['maxlength' => true])->label('用户名') ?> + + field($model, 'email')->input('email')->label('电子邮箱地址') ?> + + field($model, 'password')->passwordInput(['maxlength' => true])->label('密码') ?> + +
+
+ + +
+
+ + +
+
+ +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/views/admin/_user_form.php b/views/admin/_user_form.php new file mode 100644 index 0000000..0b6747f --- /dev/null +++ b/views/admin/_user_form.php @@ -0,0 +1,27 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true])->label('昵称') ?> + + field($model, 'email')->input('email')->label('电子邮箱地址') ?> + + field($model, 'password')->passwordInput(['maxlength' => true])->label('密码') ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/views/admin/user.php b/views/admin/user.php index ffa14ce..a4f24e6 100644 --- a/views/admin/user.php +++ b/views/admin/user.php @@ -1,8 +1,77 @@ -

admin/index

-

- 这里是管理员页面.建设中 -

+use app\models\User; +use app\utils\FileSizeHelper; +use app\utils\IPLocation; +use yii\grid\ActionColumn; +use yii\grid\GridView; +use yii\helpers\Html; +use yii\helpers\Url; +use yii\widgets\Pjax; + +/** @var yii\web\View $this */ +/** @var app\models\UserSearch $searchModel */ +/** @var yii\data\ActiveDataProvider $dataProvider */ +$IPLocation = new IPLocation(); +$this->title = '用户管理'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ 'btn btn-success']) ?> +

+ + +
+ $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\CheckboxColumn'], + ['attribute' => 'id', 'label' => 'ID'], + ['attribute' => 'username', 'label' => '用户名'], + ['attribute' => 'name', 'label' => '昵称'], + ['attribute' => 'email', 'format' => 'email', 'label' => '电子邮箱'], + ['attribute' => 'status', 'label' => '账户启用', 'value' => function ($model) { + return $model->status == 0 ? '禁用' : '启用'; + }, 'filter' => ['0' => '禁用', '1' => '启用']], + ['attribute' => 'created_at', 'label' => '账户创建时间', 'filter' => false], + ['attribute' => 'last_login', 'label' => '上次登陆时间', 'filter' => false], + ['attribute' => 'last_login_ip', 'label' => '上次登录IP', 'value' => function ($model) use ($IPLocation) { + if (Yii::$app->params['enableIpInfo']) { + return $IPLocation->getFormatDetails($model->last_login_ip); + } else { + return $model->last_login_ip; + } + }, 'filter' => false],// 给这个加位置显示也许会更好,但ipinfo那边就不好了 + ['attribute' => 'role', 'label' => '用户身份', 'value' => function ($model) { + return $model->role == 'user' ? '用户' : '管理员'; + }, 'filter' => ['user' => '用户', 'admin' => '管理员']], + ['attribute' => 'is_otp_enabled', 'label' => '多因素登录', 'value' => function ($model) { + return $model->is_otp_enabled == 0 ? '禁用' : '启用'; + }, 'filter' => ['0' => '禁用', '1' => '启用']], + ['attribute' => 'storage_limit', 'label' => '空间使用情况', 'value' => function ($model) { + if ($model->role == 'user') { + return FileSizeHelper::getFormatUserAllDirSize($model->id) . ' / ' . FileSizeHelper::formatMegaBytes($model->storage_limit); + } else { + return '不可用'; + } + }, 'filter' => false], + [ + 'class' => ActionColumn::class, + 'header' => '操作', + 'template' => '{view} {update}', + 'urlCreator' => function ($action, User $model, $key, $index, $column) { + return Url::toRoute(['user-' . $action, 'id' => $model->id]); + } + ], + ], + ]); ?> +
+ + + +
diff --git a/views/admin/user_create.php b/views/admin/user_create.php new file mode 100644 index 0000000..fe4049a --- /dev/null +++ b/views/admin/user_create.php @@ -0,0 +1,20 @@ +title = '创建用户'; +$this->params['breadcrumbs'][] = ['label' => '用户管理', 'url' => ['user']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_user_add_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/views/admin/user_update.php b/views/admin/user_update.php new file mode 100644 index 0000000..afa3eba --- /dev/null +++ b/views/admin/user_update.php @@ -0,0 +1,21 @@ +title = 'Update User: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['user']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['user-view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_user_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/views/admin/user_view.php b/views/admin/user_view.php new file mode 100644 index 0000000..9752979 --- /dev/null +++ b/views/admin/user_view.php @@ -0,0 +1,57 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['user']]; +$this->params['breadcrumbs'][] = $this->title; +YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'username', + 'name', + 'password', + 'auth_key', + 'email:email', + 'status', + 'created_at', + 'last_login', + 'last_login_ip', + 'bio:ntext', + 'role', + 'encryption_key', + 'otp_secret', + 'is_encryption_enabled', + 'is_otp_enabled', + 'storage_limit', + 'recovery_codes', + 'dark_mode', + 'vault_secret', + 'vault_salt', + ], + ]) ?> + +