Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZendFramework] Zend_Acl, Jak odczytać uprawnienia w widoku?
nexis
post
Post #1





Grupa: Zarejestrowani
Postów: 1 012
Pomógł: 109
Dołączył: 26.09.2003
Skąd: nexis.pl

Ostrzeżenie: (0%)
-----


W jaki sposób mogę w widoku (np. views/scripts/index/index.phtml) uzyskać informację czy zalogowany użytkownik ma dostęp do jakiegoś działu? Chciałbym to uzyskać za pomocą funkcji typu isAllowed('controller', 'action') abym mógł ukryć linki w menu, do których użytkownik tak czy inaczej nie ma dostępu. Zend_Acl skonfigurowałem w następujący sposób:

bootstrap.php
  1. <?php
  2. defined('APPLICATION_PATH')
  3.   or define('APPLICATION_PATH', dirname(__FILE__));
  4.  
  5. defined('APPLICATION_ENVIRONMENT')
  6.    or define('APPLICATION_ENVIRONMENT', 'development');
  7.  
  8. $frontController = Zend_Controller_Front::getInstance();
  9. $frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
  10. $frontController->setParam('env', APPLICATION_ENVIRONMENT);
  11.  
  12. Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');
  13. $view = Zend_Layout::getMvcInstance()->getView();
  14. $view->doctype('XHTML1_STRICT');
  15.  
  16. $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', APPLICATION_ENVIRONMENT);
  17.  
  18. $dbAdapter = Zend_Db::factory($configuration->database);
  19. $dbAdapter->query('SET NAMES utf8');
  20. Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
  21.  
  22. $registry = Zend_Registry::getInstance();
  23. $registry->configuration = $configuration;
  24. $registry->dbAdapter     = $dbAdapter;
  25.  
  26. require_once APPLICATION_PATH . '/models/Acl.php';
  27. require_once APPLICATION_PATH . '/models/Auth.php';
  28. $auth = Zend_Auth::getInstance();
  29. $acl  = new Acl($auth);
  30. $frontController->registerPlugin(new Auth($auth, $acl))
  31.                ->setParam('auth', $auth);
  32.  
  33.  
  34. unset($frontController, $view, $configuration, $dbAdapter, $registry);
  35. ?>


models/Auth.php
  1. <?php
  2. class Auth extends Zend_Controller_Plugin_Abstract
  3. {
  4.   public $_auth;
  5.   public $_acl;
  6.  
  7.   private $_noauth = array('module' => 'default', 'controller' => 'user', 'action' => 'login');
  8.   private $_noacl  = array('module' => 'default', 'controller' => 'user', 'action' => 'noAccess');
  9.  
  10.   public function __construct($auth, $acl)
  11.   {
  12.      $this->_auth = $auth;
  13.      $this->_acl  = $acl;
  14.   }
  15.   public function preDispatch(Zend_Controller_Request_Abstract $request)
  16.   {
  17.      $role = ($this->_auth->hasIdentity()) ? $this->_auth->getStorage()->read()->role : 'guest';
  18.  
  19.      $controller = $request->controller;
  20.      $action     = $request->action;
  21.      $module     = $request->module;
  22.      $resource   = $controller;
  23.  
  24.      if (!$this->_acl->has($resource)) {
  25.         $resource = null;
  26.      }
  27.  
  28.      if (!$this->_acl->isAllowed($role, $resource, $action)) {
  29.         if (!$this->_auth->hasIdentity()) {
  30.            $module     = $this->_noauth['module'];
  31.            $controller = $this->_noauth['controller'];
  32.            $action     = $this->_noauth['action'];
  33.         } else {
  34.            $module     = $this->_noacl['module'];
  35.            $controller = $this->_noacl['controller'];
  36.            $action     = $this->_noacl['action'];
  37.         }
  38.      }
  39.        
  40.      $request->setModuleName($module);
  41.      $request->setControllerName($controller);
  42.      $request->setActionName($action);
  43.   }
  44. }
  45. ?>


models/Acl.php
  1. <?php
  2. class Acl extends Zend_Acl
  3. {
  4.   public function __construct(Zend_Auth $auth)
  5.   {      
  6.      $this->add(new Zend_Acl_Resource('category'));
  7.      $this->add(new Zend_Acl_Resource('error'));
  8.      $this->add(new Zend_Acl_Resource('index'));
  9.      $this->add(new Zend_Acl_Resource('localization'));
  10.      $this->add(new Zend_Acl_Resource('object'));
  11.      $this->add(new Zend_Acl_Resource('objectcategory'));
  12.      $this->add(new Zend_Acl_Resource('objectelement'));
  13.      $this->add(new Zend_Acl_Resource('objectphoto'));
  14.      $this->add(new Zend_Acl_Resource('pattern'));
  15.      $this->add(new Zend_Acl_Resource('patternelement'));
  16.      $this->add(new Zend_Acl_Resource('postcode'));
  17.      $this->add(new Zend_Acl_Resource('user'));
  18.      
  19.      # Guest
  20.      $this->addRole(new Zend_Acl_Role('guest'));
  21.      $this->allow('guest', 'index');
  22.      $this->allow('guest', 'error');
  23.      $this->allow('guest', 'user');
  24.      
  25.      # Editor
  26.      $this->addRole(new Zend_Acl_Role('editor'), 'guest');
  27.      $this->allow('editor', 'localization');
  28.      $this->allow('editor', 'object');
  29.      $this->allow('editor', 'objectcategory');
  30.      $this->allow('editor', 'objectelement');
  31.      $this->allow('editor', 'objectphoto');
  32.      $this->allow('editor', 'postcode');
  33.      
  34.      # Manager
  35.      $this->addRole(new Zend_Acl_Role('manager'), 'editor');
  36.      $this->allow('manager', 'category');
  37.      $this->deny('manager', 'object', 'activate');
  38.      $this->allow('manager', 'pattern');
  39.      $this->allow('manager', 'patternelement');
  40.      
  41.      # Administrator
  42.      $this->addRole(new Zend_Acl_Role('administrator'));
  43.      $this->allow('administrator');
  44.   }
  45. }
  46. ?>


Ten post edytował nexis 15.01.2009, 23:06:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
qba_rox
post
Post #2





Grupa: Zarejestrowani
Postów: 29
Pomógł: 1
Dołączył: 12.01.2009
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


to prawda raz, ale nie chodzilo mi tu akurat o same obiekty helperow, tylko obiekty wewnatrz helpera, np obiekt modelu, konfiguracji (oczywiscie moze nie byc zadnych obiektow), poza tym odwolywanie sie dziesiatki razy do helpera nie jest wydajne, po to ludzie robia cos takiego.

Ten post edytował qba_rox 16.01.2009, 12:53:44
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 05:18