适配分离后的layout

适配管理员登录后事件
This commit is contained in:
Chenx221 2024-03-21 14:00:10 +08:00
parent 246b9c5dde
commit a7b28b8afd
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
4 changed files with 72 additions and 6 deletions

View File

@ -60,7 +60,18 @@ class CollectionController extends Controller
] ]
); );
} }
public function init(): void
{
parent::init();
if (Yii::$app->user->can('admin')) {
$this->layout = 'admin_main';
}elseif (Yii::$app->user->isGuest) {
$this->layout = 'guest_main';
} else {
$this->layout = 'main';
}
}
/** /**
* Lists all CollectionTasks models. * Lists all CollectionTasks models.
* *

View File

@ -60,7 +60,18 @@ class ShareController extends Controller
] ]
); );
} }
public function init(): void
{
parent::init();
if (Yii::$app->user->can('admin')) {
$this->layout = 'admin_main';
}elseif (Yii::$app->user->isGuest) {
$this->layout = 'guest_main';
} else {
$this->layout = 'main';
}
}
/** /**
* Lists all Share models. * Lists all Share models.
* *

View File

@ -35,7 +35,18 @@ class SiteController extends Controller
], ],
]; ];
} }
public function init(): void
{
parent::init();
if (Yii::$app->user->can('admin')) {
$this->layout = 'admin_main';
}elseif (Yii::$app->user->isGuest) {
$this->layout = 'guest_main';
} else {
$this->layout = 'main';
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -99,6 +99,19 @@ class UserController extends Controller
); );
} }
public function init(): void
{
parent::init();
if (Yii::$app->user->can('admin')) {
$this->layout = 'admin_main';
} elseif (Yii::$app->user->isGuest) {
$this->layout = 'guest_main';
} else {
$this->layout = 'main';
}
}
/** /**
* 删除账户(仅自身) * 删除账户(仅自身)
* @return Response * @return Response
@ -239,8 +252,14 @@ class UserController extends Controller
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
} }
Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0);
// user to home page, admin to admin/index
if (Yii::$app->user->can('admin')) {
return $this->redirect(['admin/index']);
} else {
return $this->goHome(); return $this->goHome();
} }
}
} else { } else {
Yii::$app->session->setFlash('error', '用户名密码错误或账户已禁用'); Yii::$app->session->setFlash('error', '用户名密码错误或账户已禁用');
} }
@ -283,7 +302,11 @@ class UserController extends Controller
} }
Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0);
Yii::$app->session->remove('login_verification'); Yii::$app->session->remove('login_verification');
if (Yii::$app->user->can('admin')) {
return $this->redirect(['admin/index']);
} else {
return $this->goHome(); return $this->goHome();
}
} else { } else {
Yii::$app->session->setFlash('error', '二步验证代码错误'); Yii::$app->session->setFlash('error', '二步验证代码错误');
} }
@ -301,7 +324,11 @@ class UserController extends Controller
Yii::$app->session->setFlash('success', '登陆成功,但请注意已经使用的恢复代码已失效'); Yii::$app->session->setFlash('success', '登陆成功,但请注意已经使用的恢复代码已失效');
Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0);
Yii::$app->session->remove('login_verification'); Yii::$app->session->remove('login_verification');
if (Yii::$app->user->can('admin')) {
return $this->redirect(['admin/index']);
} else {
return $this->goHome(); return $this->goHome();
}
} else { } else {
Yii::$app->session->setFlash('error', '恢复代码错误'); Yii::$app->session->setFlash('error', '恢复代码错误');
} }
@ -366,6 +393,7 @@ class UserController extends Controller
public function actionLogout(): Response public function actionLogout(): Response
{ {
Yii::$app->user->logout(); Yii::$app->user->logout();
Yii::$app->session->setFlash('success', '已登出');
return $this->goHome(); return $this->goHome();
} }
@ -392,7 +420,7 @@ class UserController extends Controller
$model->password = Yii::$app->security->generatePasswordHash($raw_password); $model->password = Yii::$app->security->generatePasswordHash($raw_password);
$model->auth_key = Yii::$app->security->generateRandomString(); $model->auth_key = Yii::$app->security->generateRandomString();
$model->created_at = date('Y-m-d H:i:s'); $model->created_at = date('Y-m-d H:i:s');
$model->role = 'user'; $model->role = 'user'; // 管理员只能通过现有管理员操作添加
$model->name = $model->username; //用户默认昵称为用户名,后期可以修改 $model->name = $model->username; //用户默认昵称为用户名,后期可以修改
if ($model->save(false)) { // save without validation if ($model->save(false)) { // save without validation
$userFolder = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->id; $userFolder = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->id;
@ -843,6 +871,11 @@ class UserController extends Controller
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
} }
Yii::$app->user->login($user, $remember === 1 ? 3600 * 24 * 30 : 0); Yii::$app->user->login($user, $remember === 1 ? 3600 * 24 * 30 : 0);
$publicKeyCredentialSourceRepository1->saveCredential($publicKeyCredentialSource, '', false);
if(Yii::$app->user->can('admin')){
return $this->asJson(['verified' => true,'redirectTo' => 'index.php?r=admin%2Findex']);
}
return $this->asJson(['verified' => true,'redirectTo' => 'index.php']);
} }
// Optional, but highly recommended, you can save the credential source as it may be modified // Optional, but highly recommended, you can save the credential source as it may be modified
// during the verification process (counter may be higher). // during the verification process (counter may be higher).