mvccore / ext-auth-basic
MvcCore - 扩展 - 认证 - 基础 - 简单认证扩展,仅用于用户登录/登出。扩展可以使用系统 `config.ini` 中或数据库中定义的凭据。可以扩展用户、登录/登出表单、提交控制器以及扩展类本身。
v5.2.7
2024-03-14 14:30 UTC
Requires
- php: >=5.4.0
- mvccore/ext-form: ^5.2.20
- mvccore/ext-form-field-button: ^5.2.2
- mvccore/ext-form-field-text: ^5.2.2
- mvccore/mvccore: ^5.2.33
README
简单认证扩展,仅用于用户登录/登出。扩展可以使用系统 config.ini
中或数据库中定义的凭据。可以扩展用户、登录/登出表单、提交控制器以及扩展类本身。
安装
composer require mvccore/ext-auth-basic
使用方法
将此添加到 Bootstrap.php
或任何应用开始处,在应用路由之前。
\MvcCore\Ext\Auths\Basic::GetInstance() ->SetPasswordHashSalt('s9E56/QH6!a69sJML9aS$6s+') ->SetUserClass('\\MvcCore\\Ext\\Auths\\Users\\SystemConfig'); /* // or you can use database user: ->SetUserClass('\\MvcCore\\Ext\\Auths\\Users\\Database') ->SetTableStructureForDbUsers('users', array( 'id' => 'id', 'active' => 'active', 'userName' => 'user_name', 'passwordHash' => 'password_hash', 'fullName' => 'full_name', )); */
对于系统配置用户,您需要在 system.ini
中指定用户,如下所示
[users] 0.userName = admin 0.fullName = Administrator 0.passwordHash = $2y$10$czlFNTYvUUg2IWE2OXNKTO8PB5xPGXz9i8IH7Fa7M0YsPlSLriJZu ; admin password is `demo`
要在您的应用程序控制器中获取登录表单显示到视图中
... public function IndexAction () { if ($this->user !== NULL) self::Redirect($this->Url('administration_index_page')); $this->view->SignInForm = \MvcCore\Ext\Auths\Basic::GetInstance() ->GetSignInForm() ->SetValues(array(// set signed in url to administration index page by default: 'successUrl' => $this->Url('administration_index_page'), )); } ...
要在您的应用程序控制器中获取登出表单显示到视图中
... public function PreDispatch () { parent::PreDispatch(); if ($this->viewEnabled && $this->user) { $this->view->SignOutForm =\MvcCore\Ext\Auths\Basic::GetInstance() ->GetSignOutForm() ->SetValues(array( 'successUrl' => $this->Url('login_page') )); } } ...
对于任何表单的 CSRF 错误 - 您可以在基本控制器的 Init()
动作中调用
... public function Init() { parent::Init(); // when any CSRF token is outdated or not the same - sign out user by default \MvcCore\Ext\Form::AddCsrfErrorHandler(function (\MvcCore\Ext\Form & $form, $errorMsg) { \MvcCore\Ext\Auths\Basics\User::LogOut(); self::Redirect($this->Url( 'Index:Index', array('absolute' => TRUE, 'sourceUrl' => rawurlencode($form->ErrorUrl)) )); }); } ...
要翻译您的登录和登出表单的可视元素,请使用
\MvcCore\Ext\Auths\Basic::GetInstance()->SetTranslator(function ($key, $lang = NULL) { // your custom translator model/service: return \App\Models\Translator::GetInstance()->Translate($key, $lang); });