@@ -11,40 +11,40 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | class UserPermissionsComponent extends Component { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Controller name |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 14 | + /** |
|
| 15 | + * Controller name |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | 19 | public $controller = null; |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Session |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 21 | + /** |
|
| 22 | + * Session |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | 26 | public $session = null; |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Components array |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 28 | + /** |
|
| 29 | + * Components array |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | 33 | public $components = ['Flash']; |
| 34 | 34 | |
| 35 | - private $actions; |
|
| 35 | + private $actions; |
|
| 36 | 36 | |
| 37 | - private $allow; |
|
| 37 | + private $allow; |
|
| 38 | 38 | |
| 39 | - private $redirect; |
|
| 39 | + private $redirect; |
|
| 40 | 40 | |
| 41 | - private $params; |
|
| 41 | + private $params; |
|
| 42 | 42 | |
| 43 | - private $message; |
|
| 43 | + private $message; |
|
| 44 | 44 | |
| 45 | - private $userType; |
|
| 45 | + private $userType; |
|
| 46 | 46 | |
| 47 | - private $action; |
|
| 47 | + private $action; |
|
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * Boolean value which holds the configuration for the behavior in case of |
@@ -52,23 +52,23 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | private $throwEx; |
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Initialization to get controller variable |
|
| 57 | - * |
|
| 58 | - * For this component available settings: |
|
| 59 | - * bool throwEx - default false - if set to true, an exception will be |
|
| 60 | - * thrown, if a handler is about to be called but does not exist. |
|
| 61 | - * |
|
| 62 | - * @param array $config Configuration array for the component. |
|
| 63 | - */ |
|
| 64 | - public function initialize(array $config) |
|
| 65 | - { |
|
| 66 | - parent::initialize($config); |
|
| 55 | + /** |
|
| 56 | + * Initialization to get controller variable |
|
| 57 | + * |
|
| 58 | + * For this component available settings: |
|
| 59 | + * bool throwEx - default false - if set to true, an exception will be |
|
| 60 | + * thrown, if a handler is about to be called but does not exist. |
|
| 61 | + * |
|
| 62 | + * @param array $config Configuration array for the component. |
|
| 63 | + */ |
|
| 64 | + public function initialize(array $config) |
|
| 65 | + { |
|
| 66 | + parent::initialize($config); |
|
| 67 | 67 | |
| 68 | - $this->controller = $this->_registry->getController(); |
|
| 69 | - $this->session = $this->controller->request->session(); |
|
| 68 | + $this->controller = $this->_registry->getController(); |
|
| 69 | + $this->session = $this->controller->request->session(); |
|
| 70 | 70 | |
| 71 | - $this->actions = array(); |
|
| 71 | + $this->actions = array(); |
|
| 72 | 72 | $this->allow = true; |
| 73 | 73 | $this->redirect = ''; |
| 74 | 74 | $this->params = ''; |
@@ -76,36 +76,36 @@ discard block |
||
| 76 | 76 | $this->userType = ''; |
| 77 | 77 | $this->action = null; |
| 78 | 78 | $this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Initialization to get controller variable |
|
| 83 | - * |
|
| 84 | - * @param array $rules Array of rules for permissions. |
|
| 85 | - * @return bool false if user / group doesn't have permission, true if has permission |
|
| 86 | - */ |
|
| 87 | - public function allow ($rules) { |
|
| 88 | - $this->setUserValues(); |
|
| 89 | - $this->bindConfiguration($rules); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Initialization to get controller variable |
|
| 83 | + * |
|
| 84 | + * @param array $rules Array of rules for permissions. |
|
| 85 | + * @return bool false if user / group doesn't have permission, true if has permission |
|
| 86 | + */ |
|
| 87 | + public function allow ($rules) { |
|
| 88 | + $this->setUserValues(); |
|
| 89 | + $this->bindConfiguration($rules); |
|
| 90 | 90 | |
| 91 | 91 | if (!$this->applyGroupsRules($rules)) { |
| 92 | 92 | $this->applyViewsRules($rules); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | return $this->allow; |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - private function setUserValues() |
|
| 99 | - { |
|
| 100 | - $userId = $this->session->read('Auth.User.id'); |
|
| 98 | + private function setUserValues() |
|
| 99 | + { |
|
| 100 | + $userId = $this->session->read('Auth.User.id'); |
|
| 101 | 101 | |
| 102 | - if (!isset($userId)) { |
|
| 102 | + if (!isset($userId)) { |
|
| 103 | 103 | $this->userType = 'guest'; |
| 104 | 104 | } |
| 105 | - } |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - private function bindConfiguration(array $rules) |
|
| 108 | - { |
|
| 107 | + private function bindConfiguration(array $rules) |
|
| 108 | + { |
|
| 109 | 109 | foreach(array("user_type", "redirect", "action", "controller", "message") as $key) { |
| 110 | 110 | if(isset($rules[$key])) { |
| 111 | 111 | $this->$key = $value; |
@@ -122,56 +122,56 @@ discard block |
||
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | - } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - private function applyGroupsRules(array $rules) |
|
| 128 | - { |
|
| 129 | - $existRulesForGroups = false; |
|
| 127 | + private function applyGroupsRules(array $rules) |
|
| 128 | + { |
|
| 129 | + $existRulesForGroups = false; |
|
| 130 | 130 | |
| 131 | - if(isset($rules['groups'])){ |
|
| 131 | + if(isset($rules['groups'])){ |
|
| 132 | 132 | foreach($rules['groups'] as $key => $value){ |
| 133 | 133 | $this->searchForApplyGroupRules($key, $value); |
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | return $existRulesForGroups; |
| 138 | - } |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - private function searchForApplyGroupRules($key) |
|
| 141 | - { |
|
| 142 | - if($key == $this->userType){ |
|
| 143 | - if ($this->notInArrayAction()) { |
|
| 140 | + private function searchForApplyGroupRules($key) |
|
| 141 | + { |
|
| 142 | + if($key == $this->userType){ |
|
| 143 | + if ($this->notInArrayAction()) { |
|
| 144 | 144 | $this->redirectIfIsSet(); |
| 145 | 145 | |
| 146 | 146 | $this->allow = false; |
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | - } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - private function notInArrayAction() |
|
| 152 | - { |
|
| 153 | - return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
| 154 | - } |
|
| 151 | + private function notInArrayAction() |
|
| 152 | + { |
|
| 153 | + return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - private function applyViewsRules(array $rules) |
|
| 157 | - { |
|
| 158 | - if(isset($rules['views'])){ |
|
| 156 | + private function applyViewsRules(array $rules) |
|
| 157 | + { |
|
| 158 | + if(isset($rules['views'])){ |
|
| 159 | 159 | foreach($rules['views'] as $key => $value){ |
| 160 | 160 | $this->searchForApplyViewRules($key, $value); |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | - } |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - private function searchForApplyViewRules($key, $value) |
|
| 166 | - { |
|
| 167 | - if($key == $this->action) { |
|
| 165 | + private function searchForApplyViewRules($key, $value) |
|
| 166 | + { |
|
| 167 | + if($key == $this->action) { |
|
| 168 | 168 | if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
| 169 | 169 | $this->redirectIfIsSet(); |
| 170 | 170 | |
| 171 | 171 | $this->allow = false; |
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | - } |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | private function checkForHandler($controller, $handler) |
| 177 | 177 | { |
@@ -192,9 +192,9 @@ discard block |
||
| 192 | 192 | return true; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - private function redirectIfIsSet() |
|
| 196 | - { |
|
| 197 | - if($this->redirect != ''){ |
|
| 195 | + private function redirectIfIsSet() |
|
| 196 | + { |
|
| 197 | + if($this->redirect != ''){ |
|
| 198 | 198 | if($this->message != ''){ |
| 199 | 199 | $this->Flash->set($this->message); |
| 200 | 200 | } |