mvccore/ext-auth

MvcCore - 扩展 - 身份验证 - 通过加载的类自动检测身份验证模块类型的身份验证模块。

v5.2.0 2022-11-17 08:04 UTC

This package is auto-updated.

Last update: 2024-09-17 18:43:24 UTC


README

Latest Stable Version License PHP Version

具有通过加载的类自动检测身份验证模块类型的身份验证模块。

安装

composer require mvccore/ext-auth

用法

将此代码添加到 Bootstrap.php 或在应用路由之前的应用程序开始部分。

\MvcCore\Ext\Auth::GetInstance()
	->SetPasswordHashSalt('s9E56/QH6!a69sJML9aS$6s+')
	->SetUserClass('\\MvcCore\\Ext\\Auths\\Users\\SystemConfig');

对于系统配置用户,您需要在 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\Auth::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\Auth::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\Auth\User::LogOut();
			self::Redirect($this->Url(
				'Index:Index',
				array('absolute' => TRUE, 'sourceUrl'	=> rawurlencode($form->ErrorUrl))
			));
		});
	}
...

要翻译您的登录和注销表单的可视元素,请使用

\MvcCore\Ext\Auth::GetInstance()->SetTranslator(function ($key, $lang = NULL) {
	// your custom translator model/service:
	return \App\Models\Translator::GetInstance()->Translate($key, $lang);
});