tahmina8765/zf2auth

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

一个Zend Framework 2身份验证模块

dev-master 2015-02-23 09:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:17:48 UTC


README

这是一个由Tahmina Khatoon创建的Zend Framework 2用户身份验证和基于角色的授权模块。

此包仍不稳定。请等待beta版本发布后再使用。

安装

使用composer

  1. 在您的composer.json中添加此项目

    "require": {
        "tahmina8765/zf2auth": "dev-master"
    }
  2. 现在运行以下命令,让composer下载ZfcUser:

    $ php composer.phar update

安装后

  1. 在您的application.config.php文件中启用它。

    <?php
    return array(
        'modules' => array(
            // ...
            'Zf2auth'
        ),
        // ...
    );
  2. 然后导入位于./vendor/tahmina8765/zf2auth/data/schema.sql的SQL架构。

  3. 在Application/Module.php(用于引导应用的主体模块)中添加以下内容:

    use Zend\Authentication\AuthenticationService;
    use Zend\Http\Response;
    use Zend\Session\Container;
    use Zend\Session\Config\SessionConfig;
    use Zend\Session\SessionManager;
    
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    
        $this->initAcl($e);
        $eventManager->attach('route', array($this, 'checkAcl'));
        $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'));
        $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'));
    }
    
    public function initSession($config)
    {
        $sessionConfig = new SessionConfig();
        $sessionConfig->setOptions($config);
        $sessionManager = new SessionManager($sessionConfig);
        $sessionManager->start();
        Container::setDefaultManager($sessionManager);
    }
    
    public function initAcl(MvcEvent $e)
    {
    
        $acl = new \Zend\Permissions\Acl\Acl();
        $application = $e->getApplication();
        $services = $application->getServiceManager();
    
        $this->rolesTable = $services->get('Zf2auth\Table\RolesTable');
        $this->resourcesTable = $services->get('Zf2auth\Table\ResourcesTable');
        $this->roleResourcesTable = $services->get('Zf2auth\Table\RoleResourcesTable');
    
    
        $roles = $this->rolesTable->fetchAll();
        $resources = $this->resourcesTable->fetchAll();
    
        $allResources = array();
        foreach ($resources as $resource) {
            if (!empty($resource)) {
                $acl->addResource(new \Zend\Permissions\Acl\Resource\GenericResource($resource->name));
                $allResources[] = $resource->name;
            }
        }
        $allowed = array();
        foreach ($roles as $role) {
            $role_id = $role->id;
            $role_name = ($role->name);
    
            $role = new \Zend\Permissions\Acl\Role\GenericRole($role_name);
            $acl->addRole($role_name);
    
            $allowed[$role_name] = array();
            if ($role_name == 'Administrator') {
                $acl->allow($role_name);
                $allowed[$role_name] = $allResources;
            } else {
                $role_resources = $this->roleResourcesTable->getResourcesBasedOnRole($role_id);
                $allowd_resources = array();
                foreach ($role_resources as $row) {
                    if (!empty($row)) {
                        $allowd_resources[] = $row;
                        $acl->allow($role_name, $row->resource_name);
                        $allowed[$role_name][] = $row->resource_name;
                    }
                }
            }
        }
        // Set Allowed Resources In session
        $container = new Container('system_init');
        if (empty($container->allowed_resources)) {
            $container->allowed_resources = $allowed;
        }
        $e->getViewModel()->acl = $acl;
    }
    
    public function checkAcl(MvcEvent $e)
    {
    
        $route = $e->getRouteMatch()->getMatchedRouteName();
        $Zf2AuthStorage = new \Zf2auth\Model\Zf2AuthStorage;
        $userRole = $Zf2AuthStorage->getRole();
    
        if (!$e->getViewModel()->acl->hasResource($route) || !$e->getViewModel()->acl->isAllowed($userRole, $route)) {
    
            $response = $e->getResponse();
    
            if (!empty($_SESSION['zf2authSession'])) {
    
                $response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/404');
                $response->setStatusCode(403);
                $response->sendHeaders();
            } else {
                $url = $e->getRouter()->assemble(array('controller' => 'users', 'action' => 'login'), array('name' => 'users/login'));
                $response->getHeaders()->addHeaderLine('Location', $url);
                $response->setStatusCode(302);
                $response->sendHeaders();
            }
            exit;
        }
    }
    
    public function authPreDispatch(MvcEvent $e)
    {
    
        //- assemble your own URL - this is just an example
        $url = $e->getRouter()->assemble(array('action' => 'login'), array('name' => 'frontend'));
    
        $response = $e->getResponse();
        $response->getHeaders()->addHeaderLine('Location', $url);
        $response->setStatusCode(302);
        $response->sendHeaders();
        exit;
    }
    
    public function handleError(MvcEvent $e)
    {
        $exception = $e->getParam('exception');
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'ZF2AuthService' => function($sm) {
                    $authService = new AuthenticationService();
                    $authService->setStorage($sm->get('Zf2auth\Model\Zf2AuthStorage'));
                    return $authService;
                },                
            ),
        );
    }
    
    public function getSessionConfig()
    {
        $config = array();
        return $config;
    }
  4. 在public/index.php中设置管理员角色

    define('ADMIN_ROLE_ID', 1);