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 @@ + + +
- 这里是管理员页面.建设中 -
+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; +?> ++ = Html::a('创建用户', ['user-create'], ['class' => 'btn btn-success']) ?> +
+ + ++ = Html::a('Update', ['user-update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['user-delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $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', + ], + ]) ?> + +