digitaledgeit/zf2-authentication-module

此包最新版本(0.1.0)没有可用的许可证信息。

为Zend Framework v2提供的Digital Edge IT身份验证模块

0.1.0 2014-06-01 11:35 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:46:06 UTC


README

DeitAuthenticationModule是Zend Framework v2的一个预构建身份验证模块。它提供灵活的登录和注销功能。要限制对路由、控制器操作或其他资源的访问,还可以查看我们的DeitAuthorisationModule模块。

要获取功能齐全的身份验证模块,请参阅ZfcUser

安装

使用Composer

"digitaledgeit/zf2-authentication-module": "master-dev"添加到您的composer.json文件的require部分,然后运行composer update

不使用Composer

下载存储库存档,创建一个名为%app%/module/DeitAuthenticationModule的新目录,并将存档的内容提取到该目录中。

配置

配置application.config.php以加载模块

'modules' => array(
	'DeitAuthenticationModule',
	'Application',
),

配置一个实现\Zend\Authentication\Adapter\AdapterInterface的认证适配器。

'service_manager' => array(
	'factories' => array(
		'deit_authentication_adapter' => function($sm) {

			//create an adapter
			//for example: http://framework.zend.com/manual/2.0/en/modules/zend.authentication.adapter.dbtable.html
			//for example: https://github.com/doctrine/DoctrineModule/blob/master/docs/authentication.md

		},
	),
),

获取认证身份

该模块提供用于获取身份的控制器插件和视图辅助器。

$this->identity();

当类似\Zend\Authentication\Adapter\DbTable\DoctrineModule\Authentication\Adapter\ObjectRepository的适配器检索数据库实体并将其存储为会话中的身份时,通常在数据库中更新身份后(例如,如果他们已更新其名称,您使用identity视图辅助器在布局中获取用户的名称,则更新的名称将不会显示,直到用户下次登录时)会遇到会话中存储的身份数据过时的问题。

为了绕过这个问题,您可以在每次页面请求时指定一个回调来获取实体,使其始终保持最新。

'deit_authentication' => array(
	'fetch_entity_from_identity_callback' => function($identity, \Zend\ServiceManager\ServiceLocatorInterface $sm) {

		//fetch and return your entity

		//for example: fetch a user based on the user's Facebook ID returned by the adapter
		return $em->findOneById($identity['facebook_id');

	}
),

使用自定义视图

默认视图非常基础,仅显示标题“登录”并使用表单视图辅助器渲染表单。将视图更改为您自己的视图非常简单。只需像这样覆盖模板映射即可

'view_manager' => array(
	'template_map' => array(
		'deit-authentication-module/log-in' => __DIR__.'/../view/your-view-path/log-in.phtml',
	),
),

The view is passed two variables, a `$form` and a feedback `$message`. When an error occurs the `$message` may contain a message similar to "invalid credentials, please try again".

使用自定义表单

覆盖服务管理器中的deit_authentication_form键以提供您自己的表单。表单必须实现\DeitAuthenticationModule\Form\AuthenticationInterface或扩展\DeitAuthenticationModule\Form\AbstractAuthentication

'service_manager' => array(
    'factories' => array(
        'deit_authentication_form' => function($sm) {
            return new \Application\Form\MyAuthenticationForm();
        },
    },
},

默认情况下,表单在POST时进行验证,但可以通过实现isSubmitted($request)方法来覆盖此行为。

class MyForm extends \DeitAuthenticationModule\Form\AbstractAuthentication {
	public function isSubmitted(\Zend\Http\Request $request) {
		return $request->isGet() && $request->getQuery('identity') && $request->getQuery('credential');
	}
}

如果您的表单没有提供身份或凭据字段,您可以为映射用户提交的数据到适配器指定一个回调。

'deit_authentication' => array(
    'map_auth_data_to_adapter_callback' => function(array $data, \Zend\Authentication\Adapter\AdapterInterface $adapter) {

		//in this example the adapter has a single parameter (not identity and credential like the ones provided by Zend).
		//this will be useful for alternate log-in schemes like Facebook OAuth
        if (isset($data['code'])) {
            $adapter->setCode($data['code']);
        }

    },
),

监听事件

认证控制器触发两个事件,即“登录”和“注销”。该模块提供自己的\Zend\EventManager\EventManager,可以通过服务管理器使用deit_authentication_events键检索。

class Module {

	public function onBootstrap(MvcEvent $event) {

		$sm = $event->getApplication()->getServiceManager();
		$em = $sm->get('deit_authentication_events');

		$em->attach('log-in', function($event /** @var \DeitAuthenticationModule\Event\Authenticate $event */) {

			if ($event->getResult()->isValid()) {
				//handle the event here
			}

		});

        $em->attach('log-out', function($event) {

			/**
			 * @var mixed $oldIdentity
			 */
			$oldIdentity = $event->getParam('identity');

            //handle the event here

        });

	}

}