PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Acl/View/Helper/AclHelper.php

https://github.com/kareypowell/croogo
PHP | 169 lines | 95 code | 15 blank | 59 comment | 19 complexity | cc601e7d562cd2333997b54556d770d5 MD5 | raw file
  1. <?php
  2. App::uses('Helper', 'View');
  3. /**
  4. * Acl Helper
  5. *
  6. * @category Helper
  7. * @package Croogo.Acl
  8. * @version 1.4
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class AclHelper extends Helper {
  14. /**
  15. * Cached actions per Role
  16. *
  17. * @var array
  18. * @access public
  19. */
  20. public $allowedActions = array();
  21. /**
  22. * Path Whitelist
  23. */
  24. protected $_pathWhitelist = array('/', '#');
  25. /**
  26. * Constructor
  27. */
  28. public function __construct(View $View, $settings = array()) {
  29. $settings = Hash::merge(array(
  30. 'pathWhitelist' => $this->_pathWhitelist
  31. ), $settings);
  32. parent::__construct($View, $settings);
  33. $plugin = Configure::read('Site.acl_plugin');
  34. App::uses('AclPermission', $plugin . '.Model');
  35. $this->AclPermission = ClassRegistry::init($plugin . '.AclPermission');
  36. }
  37. /**
  38. * Checks whether path is in whitelist
  39. *
  40. * @param string $path Path
  41. * @return bool True if path is in the whitelist
  42. */
  43. protected function _isWhitelist($url) {
  44. return in_array($url, $this->settings['pathWhitelist']);
  45. }
  46. /**
  47. * beforeRender
  48. *
  49. */
  50. public function beforeRender($viewFile) {
  51. // display upgrade link when required
  52. $key = AuthComponent::$sessionKey . '.aclUpgrade';
  53. if ($this->_View->Session->read($key)) {
  54. $link = $this->_View->Croogo->adminAction(
  55. __d('croogo', 'Upgrade Acl database'),
  56. array('controller' => 'acl_permissions', 'action' => 'upgrade'),
  57. array('button' => 'primary')
  58. );
  59. $this->_View->Blocks->append('actions', sprintf('<li>%s</li>', $link));
  60. }
  61. }
  62. /**
  63. * Returns an array of allowed actions for current logged in Role
  64. *
  65. * @param integer $roleId Role id
  66. * @return array
  67. */
  68. public function getAllowedActionsByRoleId($roleId) {
  69. if (!empty($this->allowedActions[$roleId])) {
  70. return $this->allowedActions[$roleId];
  71. }
  72. $this->allowedActions[$roleId] = $this->AclPermission->getAllowedActionsByRoleId($roleId);
  73. return $this->allowedActions[$roleId];
  74. }
  75. /**
  76. * Check if url is allowed for the Role
  77. *
  78. * @param integer $roleId Role id
  79. * @param $url array
  80. * @return boolean
  81. */
  82. public function linkIsAllowedByRoleId($roleId, $url) {
  83. if (is_string($url)) {
  84. return $this->_isWhitelist($url);
  85. }
  86. if (isset($url['admin']) && $url['admin'] == true) {
  87. $url['action'] = 'admin_' . $url['action'];
  88. }
  89. $plugin = empty($url['plugin']) ? null : Inflector::camelize($url['plugin']) . '/';
  90. $path = '/:plugin/:controller/:action';
  91. $path = str_replace(
  92. array(':controller', ':action', ':plugin/'),
  93. array(Inflector::camelize($url['controller']), $url['action'], $plugin),
  94. 'controllers/' . $path
  95. );
  96. $linkAction = str_replace('//', '/', $path);
  97. if (in_array($linkAction, $this->getAllowedActionsByRoleId($roleId))) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Returns an array of allowed actions for current logged in User
  104. *
  105. * @param integer $userId Role id
  106. * @return array
  107. */
  108. public function getAllowedActionsByUserId($userId) {
  109. if (!empty($this->allowedActions[$userId])) {
  110. return $this->allowedActions[$userId];
  111. }
  112. $this->allowedActions[$userId] = $this->AclPermission->getAllowedActionsByUserId($userId);
  113. return $this->allowedActions[$userId];
  114. }
  115. /**
  116. * Check if url is allowed for the User
  117. *
  118. * @param integer $userId User Id
  119. * @param array|string $url link/url to check
  120. * @return boolean
  121. */
  122. public function linkIsAllowedByUserId($userId, $url) {
  123. if (is_array($url)) {
  124. if (isset($url['admin']) && $url['admin'] == true && strpos($url['action'], 'admin_') === false) {
  125. $url['action'] = 'admin_' . $url['action'];
  126. }
  127. $plugin = empty($url['plugin']) ? null : Inflector::camelize($url['plugin']) . '/';
  128. $path = '/:plugin/:controller/:action';
  129. $path = str_replace(
  130. array(':controller', ':action', ':plugin/'),
  131. array(Inflector::camelize($url['controller']), $url['action'], $plugin),
  132. 'controllers/' . $path
  133. );
  134. } else {
  135. if ($this->_isWhitelist($url)) {
  136. return true;
  137. }
  138. $path = $url;
  139. }
  140. $linkAction = str_replace('//', '/', $path);
  141. if (in_array($linkAction, $this->getAllowedActionsByUserId($userId))) {
  142. return true;
  143. } else {
  144. $userAro = array('model' => 'User', 'foreign_key' => $userId);
  145. $nodes = $this->AclPermission->Aro->node($userAro);
  146. if (isset($nodes[0]['Aro'])) {
  147. if ($this->AclPermission->check($nodes[0]['Aro'], $linkAction)) {
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. }