Issues (4)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Controller/Component/UserPermissionsComponent.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace UserPermissions\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Controller\ComponentRegistry;
6
use Cake\Controller\Component\FlashComponent;
7
use Cake\Datasource\ConnectionManager;
8
use Cake\Log\Log;
9
use Cake\ORM\TableRegistry;
10
use UserPermissions\Exception\MissingHandlerException;
11
12
class UserPermissionsComponent extends Component {
13
14
    /**
15
     * Controller name
16
     *
17
     * @var string
18
     */
19
	public $controller = null;
20
21
    /**
22
     * Session
23
     *
24
     * @var string
25
     */
26
	public $session = null;
27
28
    /**
29
     * Components array
30
     *
31
     * @var array
32
     */
33
   	public $components = ['Flash'];
34
35
    private $actions;
36
37
    private $allow;
38
39
    private $redirect;
40
41
    private $params;
42
43
    private $message;
44
45
    private $userType;
46
47
    private $action;
48
	
49
	/**
50
	 * Boolean value which holds the configuration for the behavior in case of
51
	 * missing handlers.
52
	 */
53
	private $throwEx;
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 7
    public function initialize(array $config)
65
    {
66 7
        parent::initialize($config);
67
        
68 7
        $this->controller = $this->_registry->getController();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_registry->getController() of type object<Cake\Controller\Controller> is incompatible with the declared type string of property $controller.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69 7
        $this->session = $this->controller->request->session();
70
71 7
        $this->actions 		= array();
72 7
		$this->allow 		= true;
73 7
		$this->redirect 	= '';
74 7
		$this->params 		= '';
75 7
		$this->message 		= '';
76 7
		$this->userType 	= '';
77 7
		$this->action   	= null;
78 7
		$this->throwEx      = isset($config["throwEx"]) && $config["throwEx"];
79 7
    }
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 7
    public function allow ($rules) {
88 7
    	$this->setUserValues();
89 7
    	$this->bindConfiguration($rules);
90
91 7
		if (!$this->applyGroupsRules($rules)) {
92 7
			$this->applyViewsRules($rules);
93
		}
94
95 6
		return $this->allow;
96
    }
97
98 7
    private function setUserValues()
99
    {
100 7
    	$userId = $this->session->read('Auth.User.id');
0 ignored issues
show
The method read cannot be called on $this->session (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
101
102 7
    	if (!isset($userId)) {
103 7
			$this->userType = 'guest';
104
		}
105 7
    }
106
107 7
    private function bindConfiguration(array $rules) 
108
    {
109 7
    	foreach($rules as $key => $value){
110
			switch($key){
111 7
				case "user_type":
112 7
			        $this->userType = $value;
113 7
			        break;
114 7
			    case "redirect":
115 7
			        $this->redirect = $value;
116 7
			        break;
117 7
			    case "action":
118 7
			        $this->action = $value;
119 7
			        break;
120 7
			    case "controller":
121 7
			        $this->controller = $value;
122 7
					if(!is_object($value)) {
123
						Log::write("warning", sprintf("controller is not an object (%s)", gettype($value)));
124
					}
125 7
			        break;
126 7
			    case "message":
127 7
			        $this->message = $value;
128 7
			        break;
129
			}
130
		}
131
132 7
		foreach($rules['groups']  as $key => $value){
133 7
			if($key == $this->userType){
134 7
				foreach($value as $v){
135 7
					array_push($this->actions, $v);
136
				}
137
			}
138
		}
139 7
    }
140
141 7
    private function applyGroupsRules(array $rules)
142
    {
143 7
    	$existRulesForGroups = false;
144
145 7
    	if(isset($rules['groups'])){
146 7
			foreach($rules['groups'] as $key => $value){
147 7
				$this->searchForApplyGroupRules($key);
148
			}
149
		}
150
151 7
		return $existRulesForGroups;
152
    }
153
154 7
    private function searchForApplyGroupRules($key)
155
    {
156 7
    	if($key == $this->userType){
157 7
    		if ($this->notInArrayAction()) {
158 2
				$this->redirectIfIsSet();
159
				
160 2
				$this->allow = false;
161
			}
162
		}
163 7
    }
164
165 7
    private function notInArrayAction()
166
    {
167 7
    	return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions)));
168
    }
169
170 7
    private function applyViewsRules(array $rules)
171
    {
172 7
    	if(isset($rules['views'])){
173 4
			foreach($rules['views'] as $key => $value){
174 4
				$this->searchForApplyViewRules($key, $value);
175
			}
176
		}
177 6
    }
178
179 4
    private function searchForApplyViewRules($key, $value)
180
    {
181 4
    	if($key == $this->action) {
182 4
			if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){
0 ignored issues
show
The method $value cannot be called on $this->controller (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
183 2
				$this->redirectIfIsSet();
184
				
185 2
				$this->allow = false;
186
			}
187
		}
188 3
    }
189
	
190 4
	private function checkForHandler($controller, $handler)
191
	{
192 4
		if(!method_exists($controller, $handler)) {
193 2
			$msg = sprintf(
194 2
				"Controller %s=%s has no method called '%s'",
195 2
				is_object($controller) ? "class" : "type",
196 2
				is_object($controller) ? get_class($controller) : gettype($controller),
197 2
				$handler
198
			);
199 2
			Log::write("debug", $msg);
200 2
			if($this->throwEx) {
201 1
				throw new MissingHandlerException($msg);
202
			}
203 1
			return false;
204
		}
205
		
206 2
		return true;
207
	}
208
209 4
    private function redirectIfIsSet()
210
    {
211 4
    	if($this->redirect != ''){
212
			if($this->message != ''){
213
				$this->Flash->set($this->message);
0 ignored issues
show
The property Flash does not exist on object<UserPermissions\C...erPermissionsComponent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
214
			}
215
			
216
			header("Location: " . $this->redirect);
217
			exit;
218
		}
219
	}
220
}