armenio/armenio-zf2-restrictaccess-module

Restrict Access (Database|Ldap) Module for Zend Framework 2

1.0.17 2016-06-21 17:21 UTC

This package is not auto-updated.

Last update: 2024-04-25 00:08:46 UTC


README

The Restrict Access Module for Zend Framework 2

How to install

  1. Install via composer. Don't know how? Look here

  2. cd my/project/directory

  3. Edit composer.json :

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

    'modules' => array(
    	 'Application',
    	 'RestrictAccess', //<==============================
    )
  5. Edit 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. Usage inside Controllers

    6.1 Use with 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 Use with 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. Getting user identity

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