Acl_plugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class Zend_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
  3. {
  4. /**
  5. * @var Zend_Acl
  6. **/
  7. protected $_acl;
  8. /**
  9. * @var string
  10. **/
  11. protected $_roleName;
  12. /**
  13. * @var array
  14. **/
  15. protected $_errorPage;
  16. /**
  17. * Constructor
  18. *
  19. * @param mixed $aclData
  20. * @param $roleName
  21. * @return void
  22. **/
  23. public function __construct(Zend_Acl $aclData, $roleName = 'G')
  24. {
  25. $this->_errorPage = array('module' => 'default',
  26. 'controller' => 'error',
  27. 'action' => 'denied');
  28. $this->_roleName = $roleName;
  29. if (null !== $aclData) {
  30. $this->setAcl($aclData);
  31. }
  32. }
  33. /**
  34. * Sets the ACL object
  35. *
  36. * @param mixed $aclData
  37. * @return void
  38. **/
  39. public function setAcl(Zend_Acl $aclData)
  40. {
  41. $this->_acl = $aclData;
  42. }
  43. /**
  44. * Returns the ACL object
  45. *
  46. * @return Zend_Acl
  47. **/
  48. public function getAcl()
  49. {
  50. return $this->_acl;
  51. }
  52. /**
  53. * Returns the ACL role used
  54. *
  55. * @return string
  56. * @author
  57. **/
  58. public function getRoleName()
  59. {
  60. return $this->_roleName;
  61. }
  62. public function setRoleName($type)
  63. {
  64. $this->_roleName = $type;
  65. }
  66. /**
  67. * Sets the error page
  68. *
  69. * @param string $action
  70. * @param string $controller
  71. * @param string $module
  72. * @return void
  73. **/
  74. public function setErrorPage($action, $controller = 'error', $module = null)
  75. {
  76. $this->_errorPage = array('module' => $module,
  77. 'controller' => $controller,
  78. 'action' => $action);
  79. }
  80. /**
  81. * Returns the error page
  82. *
  83. * @return array
  84. **/
  85. public function getErrorPage()
  86. {
  87. return $this->_errorPage;
  88. }
  89. /**
  90. * Predispatch
  91. * Checks if the current user identified by roleName has rights to the requested url (module/controller/action)
  92. * If not, it will call denyAccess to be redirected to errorPage
  93. *
  94. * @return void
  95. **/
  96. public function preDispatch(Zend_Controller_Request_Abstract $request)
  97. {
  98. $controller = strtolower($request->getControllerName());
  99. Application_Model_Auth::pinSessionToClient(Zend_Auth::getInstance());
  100. if (in_array($controller, array("api", "auth", "locale"))) {
  101. $this->setRoleName("G");
  102. } elseif (!Zend_Auth::getInstance()->hasIdentity()) {
  103. if ($controller !== 'login') {
  104. if ($request->isXmlHttpRequest()) {
  105. $url = 'http://'.$request->getHttpHost().'/login';
  106. $json = Zend_Json::encode(array('auth' => false, 'url' => $url));
  107. // Prepare response
  108. $this->getResponse()
  109. ->setHttpResponseCode(401)
  110. ->setBody($json)
  111. ->sendResponse();
  112. //redirectAndExit() cleans up, sends the headers and stops the script
  113. Zend_Controller_Action_HelperBroker::getStaticHelper('redirector')->redirectAndExit();
  114. } else {
  115. $r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
  116. $r->gotoSimpleAndExit('index', 'login', $request->getModuleName());
  117. }
  118. }
  119. } else {
  120. $userInfo = Zend_Auth::getInstance()->getStorage()->read();
  121. $this->setRoleName($userInfo->type);
  122. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($this->_acl);
  123. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($this->_roleName);
  124. $resourceName = '';
  125. if ($request->getModuleName() != 'default') {
  126. $resourceName .= strtolower($request->getModuleName()) . ':';
  127. }
  128. $resourceName .= $controller;
  129. /** Check if the controller/action can be accessed by the current user */
  130. if (!$this->getAcl()->has($resourceName)
  131. || !$this->getAcl()->isAllowed($this->_roleName,
  132. $resourceName,
  133. $request->getActionName())) {
  134. /** Redirect to access denied page */
  135. $this->denyAccess();
  136. }
  137. }
  138. }
  139. /**
  140. * Deny Access Function
  141. * Redirects to errorPage, this can be called from an action using the action helper
  142. *
  143. * @return void
  144. **/
  145. public function denyAccess()
  146. {
  147. $this->_request->setModuleName($this->_errorPage['module']);
  148. $this->_request->setControllerName($this->_errorPage['controller']);
  149. $this->_request->setActionName($this->_errorPage['action']);
  150. }
  151. }