用户管理功能(1.5/5)
用户添加
This commit is contained in:
parent
3928763418
commit
7a3bf1c135
@ -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,
|
||||
]);
|
||||
|
@ -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'],
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use app\models\PublicKeyCredentialSourceRepository;
|
||||
use app\models\User;
|
||||
use app\utils\FileSizeHelper;
|
||||
use app\utils\IPLocation;
|
||||
@ -13,6 +14,7 @@ use yii\widgets\Pjax;
|
||||
/** @var app\models\UserSearch $searchModel */
|
||||
/** @var yii\data\ActiveDataProvider $dataProvider */
|
||||
$IPLocation = new IPLocation();
|
||||
$PKCSR = new PublicKeyCredentialSourceRepository();
|
||||
$this->title = '用户管理';
|
||||
$this->params['breadcrumbs'][] = $this->title;
|
||||
?>
|
||||
@ -21,7 +23,7 @@ $this->params['breadcrumbs'][] = $this->title;
|
||||
<h1><?= Html::encode($this->title) ?></h1>
|
||||
|
||||
<p>
|
||||
<?= Html::a('创建用户', ['user-create'], ['class' => 'btn btn-success']) ?>
|
||||
<?= Html::a('添加用户', ['user-create'], ['class' => 'btn btn-success']) ?>
|
||||
</p>
|
||||
|
||||
<?php Pjax::begin(); ?>
|
||||
@ -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);
|
||||
|
@ -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;
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user