From 7a3bf1c1352dc491dbfbd4a48a4b12d2a9c03441 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 23 Mar 2024 13:42:15 +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/5)=20=E7=94=A8=E6=88=B7=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 34 +++++++++++++++++++++++++++------ models/User.php | 7 ++++--- views/admin/user.php | 14 ++++++++++++-- views/admin/user_create.php | 2 +- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 05d993b..7ce0bef 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -5,6 +5,7 @@ namespace app\controllers; use app\models\User; use app\models\UserSearch; use Throwable; +use Yii; use yii\db\StaleObjectException; use yii\filters\AccessControl; use yii\filters\VerbFilter; @@ -107,16 +108,37 @@ class AdminController extends Controller */ public function actionUserCreate(): Response|string { - $model = new User(); + $model = new User(['scenario' => 'addUser']); if ($this->request->isPost) { - if ($model->load($this->request->post()) && $model->save()) { - return $this->redirect(['user_view', 'id' => $model->id]); + if ($model->load($this->request->post()) && $model->validate()) { + $raw_password = $model->password; + $model->password = Yii::$app->security->generatePasswordHash($raw_password); + $model->auth_key = Yii::$app->security->generateRandomString(); + $model->role = $this->request->post('User')['role']; + $model->created_at = date('Y-m-d H:i:s'); + $model->name = $model->username; //用户默认昵称为用户名,后期可以修改 + if ($model->save(false)) { // save without validation + if($model->role == 'user'){ + $userFolder = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->id; + if (!is_dir($userFolder)) { + mkdir($userFolder); + } + $secretFolder = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->id . '.secret'; + if (!is_dir($secretFolder)) { + mkdir($secretFolder); + } + } + Yii::$app->session->setFlash('success', '用户添加成功'); + return $this->redirect(['user-view', 'id' => $model->id]); + } else { + $model->loadDefaultValues(); + $model->password = $raw_password; + Yii::$app->session->setFlash('error', '用户添加失败'); + } } - } else { - $model->loadDefaultValues(); } - + $model->loadDefaultValues(true); return $this->render('user_create', [ 'model' => $model, ]); diff --git a/models/User.php b/models/User.php index 238bcae..714ea91 100644 --- a/models/User.php +++ b/models/User.php @@ -71,12 +71,13 @@ class User extends ActiveRecord implements IdentityInterface [['last_login_ip'], 'string', 'max' => 45], [['username'], 'required', 'on' => 'login'], [['username', 'password', 'email', 'password2'], 'required', 'on' => 'register'], + [['username', 'password', 'email'], 'required', 'on' => 'addUser'], ['username', 'string', 'min' => 3, 'max' => 12], ['password', 'string', 'min' => 6, 'max' => 24], ['password2', 'compare', 'compareAttribute' => 'password', 'on' => 'register'], - ['email', 'email', 'on' => 'register'], - ['username', 'unique', 'on' => 'register'], - ['email', 'unique', 'on' => 'register'], + ['email', 'email', 'on' => ['register', 'addUser']], + ['username', 'unique', 'on' => ['register', 'addUser']], + ['email', 'unique', 'on' => ['register', 'addUser']], [['oldPassword', 'newPassword', 'newPasswordRepeat'], 'required', 'on' => 'changePassword'], ['oldPassword', 'validatePassword2', 'on' => 'changePassword'], ['newPassword', 'string', 'min' => 6, 'max' => 24, 'on' => 'changePassword'], diff --git a/views/admin/user.php b/views/admin/user.php index a4f24e6..8965bfb 100644 --- a/views/admin/user.php +++ b/views/admin/user.php @@ -1,5 +1,6 @@ title = '用户管理'; $this->params['breadcrumbs'][] = $this->title; ?> @@ -21,7 +23,7 @@ $this->params['breadcrumbs'][] = $this->title;

title) ?>

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

@@ -30,7 +32,7 @@ $this->params['breadcrumbs'][] = $this->title; 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ - ['class' => 'yii\grid\CheckboxColumn'], +// ['class' => 'yii\grid\CheckboxColumn'], ['attribute' => 'id', 'label' => 'ID'], ['attribute' => 'username', 'label' => '用户名'], ['attribute' => 'name', 'label' => '昵称'], @@ -53,6 +55,14 @@ $this->params['breadcrumbs'][] = $this->title; ['attribute' => 'is_otp_enabled', 'label' => '多因素登录', 'value' => function ($model) { return $model->is_otp_enabled == 0 ? '禁用' : '启用'; }, 'filter' => ['0' => '禁用', '1' => '启用']], + ['label' => 'Passkey', 'value' => function ($Model) use ($PKCSR) { + $UserEntitys = $PKCSR->findAllForUserEntity($Model); + if (empty($UserEntitys)) { + return '禁用'; + }else{ + return '启用'; + } + }], ['attribute' => 'storage_limit', 'label' => '空间使用情况', 'value' => function ($model) { if ($model->role == 'user') { return FileSizeHelper::getFormatUserAllDirSize($model->id) . ' / ' . FileSizeHelper::formatMegaBytes($model->storage_limit); diff --git a/views/admin/user_create.php b/views/admin/user_create.php index fe4049a..4ad97cb 100644 --- a/views/admin/user_create.php +++ b/views/admin/user_create.php @@ -5,7 +5,7 @@ use yii\helpers\Html; /** @var yii\web\View $this */ /** @var app\models\User $model */ -$this->title = '创建用户'; +$this->title = '添加用户'; $this->params['breadcrumbs'][] = ['label' => '用户管理', 'url' => ['user']]; $this->params['breadcrumbs'][] = $this->title; ?>