@@ -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 | |
@@ -37,13 +37,13 @@ discard block |
||
| 37 | 37 | * @param string $event The event to use. |
| 38 | 38 | */ |
| 39 | 39 | |
| 40 | - public function initialize(array $config) |
|
| 41 | - { |
|
| 42 | - parent::initialize($config); |
|
| 40 | + public function initialize(array $config) |
|
| 41 | + { |
|
| 42 | + parent::initialize($config); |
|
| 43 | 43 | |
| 44 | - $this->controller = $this->_registry->getController(); |
|
| 45 | - $this->session = $this->controller->request->session(); |
|
| 46 | - } |
|
| 44 | + $this->controller = $this->_registry->getController(); |
|
| 45 | + $this->session = $this->controller->request->session(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * Initialization to get controller variable |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | * @param array $rules Array of rules for permissions. |
| 52 | 52 | * @return string '0' if user / group doesn't have permission, 1 if has permission |
| 53 | 53 | */ |
| 54 | - public function allow ($rules) { |
|
| 55 | - $user_id = $this->session->read('Auth.User.id'); |
|
| 54 | + public function allow ($rules) { |
|
| 55 | + $user_id = $this->session->read('Auth.User.id'); |
|
| 56 | 56 | |
| 57 | 57 | $actions = array(); |
| 58 | 58 | $bool = '1'; |
@@ -67,20 +67,20 @@ discard block |
||
| 67 | 67 | foreach($rules as $key => $value){ |
| 68 | 68 | switch($key){ |
| 69 | 69 | case "user_type": |
| 70 | - $userType = $value; |
|
| 71 | - break; |
|
| 72 | - case "redirect": |
|
| 73 | - $redirect = $value; |
|
| 74 | - break; |
|
| 75 | - case "action": |
|
| 76 | - $action = $value; |
|
| 77 | - break; |
|
| 78 | - case "controller": |
|
| 79 | - $controller = $value; |
|
| 80 | - break; |
|
| 81 | - case "message": |
|
| 82 | - $message = $value; |
|
| 83 | - break; |
|
| 70 | + $userType = $value; |
|
| 71 | + break; |
|
| 72 | + case "redirect": |
|
| 73 | + $redirect = $value; |
|
| 74 | + break; |
|
| 75 | + case "action": |
|
| 76 | + $action = $value; |
|
| 77 | + break; |
|
| 78 | + case "controller": |
|
| 79 | + $controller = $value; |
|
| 80 | + break; |
|
| 81 | + case "message": |
|
| 82 | + $message = $value; |
|
| 83 | + break; |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
@@ -141,5 +141,5 @@ discard block |
||
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | return $bool; |
| 144 | - } |
|
| 144 | + } |
|
| 145 | 145 | } |
| 146 | 146 | \ No newline at end of file |
@@ -51,21 +51,21 @@ discard block |
||
| 51 | 51 | * @param array $rules Array of rules for permissions. |
| 52 | 52 | * @return string '0' if user / group doesn't have permission, 1 if has permission |
| 53 | 53 | */ |
| 54 | - public function allow ($rules) { |
|
| 54 | + public function allow($rules) { |
|
| 55 | 55 | $user_id = $this->session->read('Auth.User.id'); |
| 56 | 56 | |
| 57 | - $actions = array(); |
|
| 58 | - $bool = '1'; |
|
| 59 | - $redirect = ''; |
|
| 60 | - $params = ''; |
|
| 57 | + $actions = array(); |
|
| 58 | + $bool = '1'; |
|
| 59 | + $redirect = ''; |
|
| 60 | + $params = ''; |
|
| 61 | 61 | $controller = ''; |
| 62 | - $message = ''; |
|
| 63 | - $userType = ''; |
|
| 64 | - $find = 0; |
|
| 62 | + $message = ''; |
|
| 63 | + $userType = ''; |
|
| 64 | + $find = 0; |
|
| 65 | 65 | |
| 66 | 66 | //setting default options |
| 67 | - foreach($rules as $key => $value){ |
|
| 68 | - switch($key){ |
|
| 67 | + foreach ($rules as $key => $value) { |
|
| 68 | + switch ($key) { |
|
| 69 | 69 | case "user_type": |
| 70 | 70 | $userType = $value; |
| 71 | 71 | break; |
@@ -85,33 +85,33 @@ discard block |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | //push into array group actions |
| 88 | - foreach($rules['groups'] as $key => $value){ |
|
| 89 | - if($key == $userType){ |
|
| 90 | - foreach($value as $v){ |
|
| 88 | + foreach ($rules['groups'] as $key => $value) { |
|
| 89 | + if ($key == $userType) { |
|
| 90 | + foreach ($value as $v) { |
|
| 91 | 91 | array_push($actions, $v); |
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - if(!isset($userId)){ |
|
| 96 | + if (!isset($userId)) { |
|
| 97 | 97 | $userType = 'guest'; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - if(isset($rules['groups'])){ |
|
| 101 | - foreach($rules['groups'] as $key => $value){ |
|
| 102 | - if($key == $userType){ |
|
| 103 | - if(!in_array('*', $actions)){ |
|
| 104 | - if(!in_array($action, $actions)){ |
|
| 100 | + if (isset($rules['groups'])) { |
|
| 101 | + foreach ($rules['groups'] as $key => $value) { |
|
| 102 | + if ($key == $userType) { |
|
| 103 | + if (!in_array('*', $actions)) { |
|
| 104 | + if (!in_array($action, $actions)) { |
|
| 105 | 105 | $find = 1; |
| 106 | - if($redirect != ''){ |
|
| 107 | - if($message != ''){ |
|
| 106 | + if ($redirect != '') { |
|
| 107 | + if ($message != '') { |
|
| 108 | 108 | $this->Flash->set($message); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | header("Location: " . $redirect); |
| 112 | 112 | exit; |
| 113 | 113 | } |
| 114 | - else{ |
|
| 114 | + else { |
|
| 115 | 115 | $bool = '0'; |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -120,19 +120,19 @@ discard block |
||
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if(($find == 0) && (isset($rules['views']))){ |
|
| 124 | - foreach($rules['views'] as $key => $value){ |
|
| 125 | - if($key == $action){ |
|
| 126 | - if(!$this->controller->$value()){ |
|
| 127 | - if($redirect != ''){ |
|
| 128 | - if($message != ''){ |
|
| 123 | + if (($find == 0) && (isset($rules['views']))) { |
|
| 124 | + foreach ($rules['views'] as $key => $value) { |
|
| 125 | + if ($key == $action) { |
|
| 126 | + if (!$this->controller->$value()) { |
|
| 127 | + if ($redirect != '') { |
|
| 128 | + if ($message != '') { |
|
| 129 | 129 | $this->Flash->set($message); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | header("Location: " . $redirect); |
| 133 | 133 | exit; |
| 134 | 134 | } |
| 135 | - else{ |
|
| 135 | + else { |
|
| 136 | 136 | $bool = '0'; |
| 137 | 137 | } |
| 138 | 138 | } |
@@ -110,8 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | header("Location: " . $redirect); |
| 112 | 112 | exit; |
| 113 | - } |
|
| 114 | - else{ |
|
| 113 | + } else{ |
|
| 115 | 114 | $bool = '0'; |
| 116 | 115 | } |
| 117 | 116 | } |
@@ -131,8 +130,7 @@ discard block |
||
| 131 | 130 | |
| 132 | 131 | header("Location: " . $redirect); |
| 133 | 132 | exit; |
| 134 | - } |
|
| 135 | - else{ |
|
| 133 | + } else{ |
|
| 136 | 134 | $bool = '0'; |
| 137 | 135 | } |
| 138 | 136 | } |