接收并处理接收到的可编辑表单数据

前端改进
This commit is contained in:
Chenx221 2024-03-25 15:38:47 +08:00
parent e29389e5d7
commit 2dec8a60da
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
3 changed files with 141 additions and 47 deletions

View File

@ -7,6 +7,7 @@ use app\models\UserSearch;
use app\utils\AdminSword;
use Throwable;
use Yii;
use yii\base\Exception;
use yii\db\StaleObjectException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
@ -40,7 +41,7 @@ class AdminController extends Controller
'index' => ['GET'],
'system' => ['GET'],
'user' => ['GET'],
'user-view' => ['GET'],
'user-view' => ['GET','POST'],
'user-create' => ['GET', 'POST'],
'user-update' => ['GET', 'POST'],
'user-delete' => ['POST'],
@ -92,13 +93,36 @@ class AdminController extends Controller
/**
* 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
public function actionUserView(int $id): array|string
{
$model = $this->findModel($id);
if (isset($_POST['hasEditable'])) {
Yii::$app->response->format = Response::FORMAT_JSON;
$oldValue = $model->name;
if ($model->load($_POST)) {
// read or convert your posted information
$value = $model->name;
// validate if any errors
if ($model->save(true,['name'])) {
// return JSON encoded output in the below format on success with an empty `message`
return ['output' => $value, 'message' => ''];
} else {
// alternatively you can return a validation error (by entering an error message in `message` key)
return ['output' => $oldValue, 'message' => 'Incorrect Value! Please reenter.'];
}
} // else if nothing to do always return an empty JSON encoded output
else {
return ['output' => '', 'message' => ''];
}
}
return $this->render('user_view', [
'model' => $this->findModel($id),
'model' => $model,
]);
}
@ -106,6 +130,7 @@ class AdminController extends Controller
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|Response
* @throws Exception
*/
public function actionUserCreate(): Response|string
{

View File

@ -13,8 +13,6 @@ use yii\widgets\Pjax;
/** @var yii\web\View $this */
/** @var app\models\UserSearch $searchModel */
/** @var yii\data\ActiveDataProvider $dataProvider */
$IPLocation = new IPLocation();
$PKCSR = new PublicKeyCredentialSourceRepository();
$this->title = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
@ -42,20 +40,15 @@ $this->params['breadcrumbs'][] = $this->title;
}, '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' => 'last_login_ip', 'label' => '上次登录IP'],
['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' => '启用']],
['label' => 'Passkey', 'value' => function ($Model) use ($PKCSR) {
['label' => 'Passkey', 'value' => function ($Model) {
$PKCSR = new PublicKeyCredentialSourceRepository();
$UserEntitys = $PKCSR->findAllForUserEntity($Model);
if (empty($UserEntitys)) {
return '禁用';

View File

@ -1,5 +1,10 @@
<?php
use app\assets\FontAwesomeAsset;
use app\models\PublicKeyCredentialSourceRepository;
use app\utils\FileSizeHelper;
use app\utils\IPLocation;
use kartik\editable\Editable;
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
@ -11,51 +16,122 @@ $this->title = '用户ID: '.$model->id;
$this->params['breadcrumbs'][] = ['label' => '用户管理', 'url' => ['user']];
$this->params['breadcrumbs'][] = $this->title;
$alreadyDisabled = $model->status == 0;
$isCurrentUser = Yii::$app->user->id == $model->id;
$isCurrentUser = Yii::$app->user->id == $model->id ? 'disabled' : '';
$str = $alreadyDisabled ? '启用' : '禁用';
$IPLocation = new IPLocation();
YiiAsset::register($this);
FontAwesomeAsset::register($this);
?>
<div class="user-view">
<h1>用户详情</h1>
<p>
<?= Html::a('修改信息', ['user-update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<!-- --><?php //= Html::a('修改信息', ['user-update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a($str . '用户', ['user-delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'class' => 'btn btn-danger ' . $isCurrentUser,
'data' => [
'confirm' => '你确定要' . $str . '这个用户吗?',
'method' => 'post',
],
'disabled' => $isCurrentUser,
'title'=> $isCurrentUser ? '不能'.$str.'自己的账户' : '点击'.$str.'用户',
'title' => '点击' . $str . '用户',
]) ?>
</p>
<?= 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',
['attribute' => 'id', 'label' => '用户ID'],
['attribute' => 'username', 'label' => '用户名'],
['attribute' => 'name', 'label' => '昵称', 'format' => 'raw', 'value' => function ($model) {
return Editable::widget([
'name' => 'name',
'asPopover' => false,
'value' => $model->name,
'header' => '昵称',
'size' => 'md',
'options' => ['class' => 'form-control', 'placeholder' => '在这里输入新的昵称...'],
]);
}],
['attribute' => 'email', 'label' => '电子邮件'],
['label' => '头像', 'format' => 'html', 'value' => function ($model) {
return $model->getGravatar(email: $model->email, s: 100, img: true);
}],
['attribute' => 'status', 'label' => '账户状态', 'format' => 'raw', 'value' => function ($model) {
// return $model->status == 0 ? '禁用' : '启用';
//TODO 未完成
return Editable::widget([
'name' => 'status',
'asPopover' => true,
'header' => '账户状态',
'format' => Editable::FORMAT_BUTTON,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data' => [1,2,3], // any list of values
'options' => ['class' => 'form-control'],
'editableValueOptions' => ['class' => 'text-danger']
]);
}],
['attribute' => 'created_at', 'label' => '创建时间', 'value' => function ($model) {
// 日期时间 (xx天前)
return $model->created_at . ' (' . Yii::$app->formatter->asRelativeTime($model->created_at) . ')';
}],
['attribute' => 'last_login', 'label' => '最后登录时间', 'value' => function ($model) {
// 日期时间 (xx天前)
return $model->last_login . ' (' . Yii::$app->formatter->asRelativeTime($model->last_login) . ')';
}],
['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;
}
}],
['attribute' => 'bio', 'label' => '用户简介'],
['attribute' => 'role', 'label' => '用户身份', 'value' => function ($model) {
return $model->role == 'user' ? '用户' : '管理员';
}],
['attribute' => 'is_otp_enabled', 'label' => '多因素登录', 'value' => function ($model) {
return $model->is_otp_enabled == 0 ? '禁用' : '启用';
}],
['label' => 'Passkey', 'value' => function ($Model) {
$PKCSR = new PublicKeyCredentialSourceRepository();
$UserEntitys = $PKCSR->findAllForUserEntity($Model);
if (empty($UserEntitys)) {
return '禁用';
} else {
return '启用';
}
}],
['label' => '保险箱状态', 'value' => function ($model) {
if ($model->role == 'admin') {
return '不可用';
}
return empty($model->vault_secret) ? '未初始化' : '已启用';
}],
['label' => '网盘已用空间', 'value' => function ($model) {
if ($model->role == 'admin') {
return '不可用';
}
return FileSizeHelper::formatBytes(FileSizeHelper::getUserHomeDirSize($model->id));
}],
['label' => '保险箱已用空间', 'value' => function ($model) {
if ($model->role == 'admin') {
return '不可用';
}
return FileSizeHelper::formatBytes(FileSizeHelper::getUserVaultDirSize($model->id));
}],
['attribute' => 'storage_limit', 'label' => '存储容量限制', 'value' => function ($model) {
if ($model->role == 'admin') {
return '不可用';
}
return FileSizeHelper::formatMegaBytes($model->storage_limit);
}],
['attribute' => 'storage_limit', 'format' => 'html', 'label' => '存储空间使用状态', 'value' => function ($model) {
if ($model->role == 'admin') {
return '不可用';
}
return FileSizeHelper::getUsedPercent($model->id) . '<br>' . FileSizeHelper::getFormatUserAllDirSize($model->id) . ' / ' . FileSizeHelper::formatMegaBytes($model->storage_limit);
}],
],
]) ?>