armenio/armenio-zf2-restrictaccess-module

Zend Framework 2 的数据库(|LDAP)访问限制模块

1.0.17 2016-06-21 17:21 UTC

This package is not auto-updated.

Last update: 2024-09-26 02:16:02 UTC


README

Zend Framework 2 的访问限制模块

如何安装

  1. 通过 composer 安装。不知道如何操作? 查看这里

  2. cd my/project/directory

  3. 编辑 composer.json

    {
    	"require": {
    		"armenio/armenio-zf2-restrictaccess-module": "1.*"
    	}
    }
  4. 编辑 config/application.config.php

    'modules' => array(
    	 'Application',
    	 'RestrictAccess', //<==============================
    )
  5. 编辑 module/config/module.config.php

    	'service_manager' => array(
            'factories' => array(
                'AuthenticationService' => function(\Zend\ServiceManager\ServiceManager $serviceManager) {
                    $service = new \RestrictAccess\Service\Authentication\DbTableService();
                    // $service = new \RestrictAccess\Service\Authentication\LdapService();
                    
                    $service->setServiceManager($serviceManager);
    
                    return $service;
                }
            ),
        ),
  6. 在控制器中使用的说明

    6.1 使用 Zend\Db

    $username = $data['username'];
    $password = $data['password'];
    
    $authService = $this->getServiceLocator()->get('AuthenticationService');
    
    $authService->setNamespace('Default');
    $authService->setTableName('users');
    $authService->setIdentityColumn('username');
    $authService->setCredentialColumn('password');
    
    $authenticationResult = $authService->authenticate($username, $password);
    
    if( ! $authenticationResult->isValid() ){
    	var_dump($authenticationResult->getMessages());
    }
    // else var_dump($authService->getIdentity());

    6.2 使用 Zend\Ldap

    $username = $post['username'];
    $password = $post['password'];
    
    $ldapOptions = array(
    	'server1' => array(
    		'host' => 'dc1.w.net',
    		'useStartTls' => 'false',
    		'useSsl' => 'false',
    		'baseDn' => 'CN=Users,DC=w,DC=net',
    		'accountCanonicalForm' => 3,
    		'accountDomainName' => 'w.net',
    		'accountDomainNameShort' => 'W',
    	),
    );
    
    $authService = $this->getServiceLocator()->get('AuthenticationService');
    
    $authService->setNamespace('Default');
    $authService->setOptions($ldapOptions);
    
    $authenticationResult = $authService->authenticate($username, $password);
    
    if( ! $authenticationResult->isValid() ){
    	var_dump($authenticationResult->getMessages());
    }
    // else var_dump($authService->getIdentity());
  7. 获取用户身份

    if( $authService->hasIdentity() ){
    	var_dump($authService->getIdentity());
    }