@@ -2,10 +2,6 @@ |
||
| 2 | 2 | namespace UserPermissions\Controller\Component; |
| 3 | 3 | |
| 4 | 4 | use Cake\Controller\Component; |
| 5 | -use Cake\Controller\ComponentRegistry; |
|
| 6 | -use Cake\Datasource\ConnectionManager; |
|
| 7 | -use Cake\ORM\TableRegistry; |
|
| 8 | -use Cake\Controller\Component\FlashComponent; |
|
| 9 | 5 | |
| 10 | 6 | class UserPermissionsComponent extends Component { |
| 11 | 7 | |
@@ -47,7 +47,6 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * Initialization to get controller variable |
| 49 | 49 | * |
| 50 | - * @param string $event The event to use. |
|
| 51 | 50 | */ |
| 52 | 51 | public function initialize(array $config) |
| 53 | 52 | { |
@@ -69,7 +68,7 @@ discard block |
||
| 69 | 68 | * Initialization to get controller variable |
| 70 | 69 | * |
| 71 | 70 | * @param array $rules Array of rules for permissions. |
| 72 | - * @return string '0' if user / group doesn't have permission, 1 if has permission |
|
| 71 | + * @return boolean '0' if user / group doesn't have permission, 1 if has permission |
|
| 73 | 72 | */ |
| 74 | 73 | public function allow ($rules) { |
| 75 | 74 | $this->setUserValues(); |
@@ -9,107 +9,107 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class UserPermissionsComponent extends Component { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Controller name |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 12 | + /** |
|
| 13 | + * Controller name |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | 17 | public $controller = null; |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Session |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 19 | + /** |
|
| 20 | + * Session |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | 24 | public $session = null; |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Components array |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 26 | + /** |
|
| 27 | + * Components array |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | 31 | public $components = ['Flash']; |
| 32 | 32 | |
| 33 | - private $actions; |
|
| 33 | + private $actions; |
|
| 34 | 34 | |
| 35 | - private $allow; |
|
| 35 | + private $allow; |
|
| 36 | 36 | |
| 37 | - private $redirect; |
|
| 37 | + private $redirect; |
|
| 38 | 38 | |
| 39 | - private $params; |
|
| 39 | + private $params; |
|
| 40 | 40 | |
| 41 | - private $message; |
|
| 41 | + private $message; |
|
| 42 | 42 | |
| 43 | - private $userType; |
|
| 43 | + private $userType; |
|
| 44 | 44 | |
| 45 | - private $action; |
|
| 45 | + private $action; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Initialization to get controller variable |
|
| 49 | - * |
|
| 50 | - * @param string $event The event to use. |
|
| 51 | - */ |
|
| 52 | - public function initialize(array $config) |
|
| 53 | - { |
|
| 54 | - parent::initialize($config); |
|
| 47 | + /** |
|
| 48 | + * Initialization to get controller variable |
|
| 49 | + * |
|
| 50 | + * @param string $event The event to use. |
|
| 51 | + */ |
|
| 52 | + public function initialize(array $config) |
|
| 53 | + { |
|
| 54 | + parent::initialize($config); |
|
| 55 | 55 | |
| 56 | - $this->controller = $this->_registry->getController(); |
|
| 57 | - $this->session = $this->controller->request->session(); |
|
| 56 | + $this->controller = $this->_registry->getController(); |
|
| 57 | + $this->session = $this->controller->request->session(); |
|
| 58 | 58 | |
| 59 | - $this->actions = array(); |
|
| 59 | + $this->actions = array(); |
|
| 60 | 60 | $this->allow = true; |
| 61 | 61 | $this->redirect = ''; |
| 62 | 62 | $this->params = ''; |
| 63 | 63 | $this->message = ''; |
| 64 | 64 | $this->userType = ''; |
| 65 | 65 | $this->action = null; |
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Initialization to get controller variable |
|
| 70 | - * |
|
| 71 | - * @param array $rules Array of rules for permissions. |
|
| 72 | - * @return string '0' if user / group doesn't have permission, 1 if has permission |
|
| 73 | - */ |
|
| 74 | - public function allow ($rules) { |
|
| 75 | - $this->setUserValues(); |
|
| 76 | - $this->bindConfiguration($rules); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Initialization to get controller variable |
|
| 70 | + * |
|
| 71 | + * @param array $rules Array of rules for permissions. |
|
| 72 | + * @return string '0' if user / group doesn't have permission, 1 if has permission |
|
| 73 | + */ |
|
| 74 | + public function allow ($rules) { |
|
| 75 | + $this->setUserValues(); |
|
| 76 | + $this->bindConfiguration($rules); |
|
| 77 | 77 | |
| 78 | 78 | if (!$this->applyGroupsRules($rules)) { |
| 79 | 79 | $this->applyViewsRules($rules); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | return $this->allow; |
| 83 | - } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - private function setUserValues() |
|
| 86 | - { |
|
| 87 | - $userId = $this->session->read('Auth.User.id'); |
|
| 85 | + private function setUserValues() |
|
| 86 | + { |
|
| 87 | + $userId = $this->session->read('Auth.User.id'); |
|
| 88 | 88 | |
| 89 | - if (!isset($userId)) { |
|
| 89 | + if (!isset($userId)) { |
|
| 90 | 90 | $this->userType = 'guest'; |
| 91 | 91 | } |
| 92 | - } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - private function bindConfiguration(array $rules) |
|
| 95 | - { |
|
| 96 | - foreach($rules as $key => $value){ |
|
| 94 | + private function bindConfiguration(array $rules) |
|
| 95 | + { |
|
| 96 | + foreach($rules as $key => $value){ |
|
| 97 | 97 | switch($key){ |
| 98 | 98 | case "user_type": |
| 99 | - $this->userType = $value; |
|
| 100 | - break; |
|
| 101 | - case "redirect": |
|
| 102 | - $this->redirect = $value; |
|
| 103 | - break; |
|
| 104 | - case "action": |
|
| 105 | - $this->action = $value; |
|
| 106 | - break; |
|
| 107 | - case "controller": |
|
| 108 | - $this->controller = $value; |
|
| 109 | - break; |
|
| 110 | - case "message": |
|
| 111 | - $this->message = $value; |
|
| 112 | - break; |
|
| 99 | + $this->userType = $value; |
|
| 100 | + break; |
|
| 101 | + case "redirect": |
|
| 102 | + $this->redirect = $value; |
|
| 103 | + break; |
|
| 104 | + case "action": |
|
| 105 | + $this->action = $value; |
|
| 106 | + break; |
|
| 107 | + case "controller": |
|
| 108 | + $this->controller = $value; |
|
| 109 | + break; |
|
| 110 | + case "message": |
|
| 111 | + $this->message = $value; |
|
| 112 | + break; |
|
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | |
@@ -120,60 +120,60 @@ discard block |
||
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | - } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - private function applyGroupsRules(array $rules) |
|
| 126 | - { |
|
| 127 | - $existRulesForGroups = false; |
|
| 125 | + private function applyGroupsRules(array $rules) |
|
| 126 | + { |
|
| 127 | + $existRulesForGroups = false; |
|
| 128 | 128 | |
| 129 | - if(isset($rules['groups'])){ |
|
| 129 | + if(isset($rules['groups'])){ |
|
| 130 | 130 | foreach($rules['groups'] as $key => $value){ |
| 131 | 131 | $this->searchForApplyGroupRules($key, $value); |
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | return $existRulesForGroups; |
| 136 | - } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - private function searchForApplyGroupRules($key) |
|
| 139 | - { |
|
| 140 | - if($key == $this->userType){ |
|
| 141 | - if ($this->notInArrayAction()) { |
|
| 138 | + private function searchForApplyGroupRules($key) |
|
| 139 | + { |
|
| 140 | + if($key == $this->userType){ |
|
| 141 | + if ($this->notInArrayAction()) { |
|
| 142 | 142 | $this->redirectIfIsSet(); |
| 143 | 143 | |
| 144 | 144 | $this->allow = false; |
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | - } |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - private function notInArrayAction() |
|
| 150 | - { |
|
| 151 | - return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
| 152 | - } |
|
| 149 | + private function notInArrayAction() |
|
| 150 | + { |
|
| 151 | + return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - private function applyViewsRules(array $rules) |
|
| 155 | - { |
|
| 156 | - if(isset($rules['views'])){ |
|
| 154 | + private function applyViewsRules(array $rules) |
|
| 155 | + { |
|
| 156 | + if(isset($rules['views'])){ |
|
| 157 | 157 | foreach($rules['views'] as $key => $value){ |
| 158 | 158 | $this->searchForApplyViewRules($key, $value); |
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | - } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - private function searchForApplyViewRules($key, $value) |
|
| 164 | - { |
|
| 165 | - if($key == $this->action){ |
|
| 163 | + private function searchForApplyViewRules($key, $value) |
|
| 164 | + { |
|
| 165 | + if($key == $this->action){ |
|
| 166 | 166 | if(!$this->controller->$value()){ |
| 167 | 167 | $this->redirectIfIsSet(); |
| 168 | 168 | |
| 169 | 169 | $this->allow = false; |
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - private function redirectIfIsSet() |
|
| 175 | - { |
|
| 176 | - if($this->redirect != ''){ |
|
| 174 | + private function redirectIfIsSet() |
|
| 175 | + { |
|
| 176 | + if($this->redirect != ''){ |
|
| 177 | 177 | if($this->message != ''){ |
| 178 | 178 | $this->Flash->set($this->message); |
| 179 | 179 | } |
@@ -56,10 +56,10 @@ discard block |
||
| 56 | 56 | $this->controller = $this->_registry->getController(); |
| 57 | 57 | $this->session = $this->controller->request->session(); |
| 58 | 58 | |
| 59 | - $this->actions = array(); |
|
| 60 | - $this->allow = true; |
|
| 59 | + $this->actions = array(); |
|
| 60 | + $this->allow = true; |
|
| 61 | 61 | $this->redirect = ''; |
| 62 | - $this->params = ''; |
|
| 62 | + $this->params = ''; |
|
| 63 | 63 | $this->message = ''; |
| 64 | 64 | $this->userType = ''; |
| 65 | 65 | $this->action = null; |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * @param array $rules Array of rules for permissions. |
| 72 | 72 | * @return string '0' if user / group doesn't have permission, 1 if has permission |
| 73 | 73 | */ |
| 74 | - public function allow ($rules) { |
|
| 74 | + public function allow($rules) { |
|
| 75 | 75 | $this->setUserValues(); |
| 76 | 76 | $this->bindConfiguration($rules); |
| 77 | 77 | |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | private function bindConfiguration(array $rules) |
| 95 | 95 | { |
| 96 | - foreach($rules as $key => $value){ |
|
| 97 | - switch($key){ |
|
| 96 | + foreach ($rules as $key => $value) { |
|
| 97 | + switch ($key) { |
|
| 98 | 98 | case "user_type": |
| 99 | 99 | $this->userType = $value; |
| 100 | 100 | break; |
@@ -113,9 +113,9 @@ discard block |
||
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - foreach($rules['groups'] as $key => $value){ |
|
| 117 | - if($key == $this->userType){ |
|
| 118 | - foreach($value as $v){ |
|
| 116 | + foreach ($rules['groups'] as $key => $value) { |
|
| 117 | + if ($key == $this->userType) { |
|
| 118 | + foreach ($value as $v) { |
|
| 119 | 119 | array_push($this->actions, $v); |
| 120 | 120 | } |
| 121 | 121 | } |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | { |
| 127 | 127 | $existRulesForGroups = false; |
| 128 | 128 | |
| 129 | - if(isset($rules['groups'])){ |
|
| 130 | - foreach($rules['groups'] as $key => $value){ |
|
| 129 | + if (isset($rules['groups'])) { |
|
| 130 | + foreach ($rules['groups'] as $key => $value) { |
|
| 131 | 131 | $this->searchForApplyGroupRules($key, $value); |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | |
| 138 | 138 | private function searchForApplyGroupRules($key) |
| 139 | 139 | { |
| 140 | - if($key == $this->userType){ |
|
| 140 | + if ($key == $this->userType) { |
|
| 141 | 141 | if ($this->notInArrayAction()) { |
| 142 | 142 | $this->redirectIfIsSet(); |
| 143 | 143 | |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | private function applyViewsRules(array $rules) |
| 155 | 155 | { |
| 156 | - if(isset($rules['views'])){ |
|
| 157 | - foreach($rules['views'] as $key => $value){ |
|
| 156 | + if (isset($rules['views'])) { |
|
| 157 | + foreach ($rules['views'] as $key => $value) { |
|
| 158 | 158 | $this->searchForApplyViewRules($key, $value); |
| 159 | 159 | } |
| 160 | 160 | } |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | |
| 163 | 163 | private function searchForApplyViewRules($key, $value) |
| 164 | 164 | { |
| 165 | - if($key == $this->action){ |
|
| 166 | - if(!$this->controller->$value()){ |
|
| 165 | + if ($key == $this->action) { |
|
| 166 | + if (!$this->controller->$value()) { |
|
| 167 | 167 | $this->redirectIfIsSet(); |
| 168 | 168 | |
| 169 | 169 | $this->allow = false; |
@@ -173,8 +173,8 @@ discard block |
||
| 173 | 173 | |
| 174 | 174 | private function redirectIfIsSet() |
| 175 | 175 | { |
| 176 | - if($this->redirect != ''){ |
|
| 177 | - if($this->message != ''){ |
|
| 176 | + if ($this->redirect != '') { |
|
| 177 | + if ($this->message != '') { |
|
| 178 | 178 | $this->Flash->set($this->message); |
| 179 | 179 | } |
| 180 | 180 | |