Passed
Push — master ( bd0cf6...698b78 )
by Alessandro
06:15
created

searchForApplyViewRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
namespace UserPermissions\Controller\Component;
3
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
10
class UserPermissionsComponent extends Component {
11
12
    /**
13
     * Controller name
14
     *
15
     * @var string
16
     */
17
	public $controller = null;
18
19
    /**
20
     * Session
21
     *
22
     * @var string
23
     */
24
	public $session = null;
25
26
    /**
27
     * Components array
28
     *
29
     * @var array
30
     */
31
   	public $components = ['Flash'];
32
33
    private $actions;
34
35
    private $allow;
36
37
    private $redirect;
38
39
    private $params;
40
41
    private $message;
42
43
    private $userType;
44
45
    private $action;
46
47
    /**
48
    * Initialization to get controller variable
49
    *
50
    * @param string $event The event to use.
0 ignored issues
show
Bug introduced by
There is no parameter named $event. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
    */
52 5
    public function initialize(array $config)
53
    {
54 5
        parent::initialize($config);
55
        
56 5
        $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...
57 5
        $this->session = $this->controller->request->session();
58
59 5
        $this->actions 		= array();
60 5
		$this->allow 		= true;
61 5
		$this->redirect 	= '';
62 5
		$this->params 		= '';
63 5
		$this->message 		= '';
64 5
		$this->userType 	= '';
65 5
		$this->action   	= null;
66 5
    }
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 5
    public function allow ($rules) {
75 5
    	$this->setUserValues();
76 5
    	$this->bindConfiguration($rules);
77
78 5
		if (!$this->applyGroupsRules($rules)) {
79 5
			$this->applyViewsRules($rules);
80
		}
81
82 5
		return $this->allow;
83
    }
84
85 5
    private function setUserValues()
86
    {
87 5
    	$userId = $this->session->read('Auth.User.id');
0 ignored issues
show
Bug introduced by
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...
88
89 5
    	if (!isset($userId)) {
90 5
			$this->userType = 'guest';
91
		}
92 5
    }
93
94 5
    private function bindConfiguration(array $rules) 
95
    {
96 5
    	foreach($rules as $key => $value){
97
			switch($key){
98 5
				case "user_type":
99 5
			        $this->userType = $value;
100 5
			        break;
101 5
			    case "redirect":
102 5
			        $this->redirect = $value;
103 5
			        break;
104 5
			    case "action":
105 5
			        $this->action = $value;
106 5
			        break;
107 5
			    case "controller":
108 5
			        $this->controller = $value;
109 5
			        break;
110 5
			    case "message":
111 5
			        $this->message = $value;
112 5
			        break;
113
			}
114
		}
115
116 5
		foreach($rules['groups']  as $key => $value){
117 5
			if($key == $this->userType){
118 5
				foreach($value as $v){
119 5
					array_push($this->actions, $v);
120
				}
121
			}
122
		}
123 5
    }
124
125 5
    private function applyGroupsRules(array $rules)
126
    {
127 5
    	$existRulesForGroups = false;
128
129 5
    	if(isset($rules['groups'])){
130 5
			foreach($rules['groups'] as $key => $value){
131 5
				$this->searchForApplyGroupRules($key, $value);
0 ignored issues
show
Unused Code introduced by
The call to UserPermissionsComponent...rchForApplyGroupRules() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
			}
133
		}
134
135 5
		return $existRulesForGroups;
136
    }
137
138 5
    private function searchForApplyGroupRules($key)
139
    {
140 5
    	if($key == $this->userType){
141 5
    		if ($this->notInArrayAction()) {
142 2
				$this->redirectIfIsSet();
143
				
144 2
				$this->allow = false;
145
			}
146
		}
147 5
    }
148
149 5
    private function notInArrayAction()
150
    {
151 5
    	return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions)));
152
    }
153
154 5
    private function applyViewsRules(array $rules)
155
    {
156 5
    	if(isset($rules['views'])){
157 2
			foreach($rules['views'] as $key => $value){
158 2
				$this->searchForApplyViewRules($key, $value);
159
			}
160
		}
161 5
    }
162
163 2
    private function searchForApplyViewRules($key, $value)
164
    {
165 2
    	if($key == $this->action){
166 2
			if(!$this->controller->$value()){
0 ignored issues
show
Bug introduced by
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...
167 1
				$this->redirectIfIsSet();
168
				
169 1
				$this->allow = false;
170
			}
171
		}
172 2
    }
173
174 3
    private function redirectIfIsSet()
175
    {
176 3
    	if($this->redirect != ''){
177
			if($this->message != ''){
178
				$this->Flash->set($this->message);
0 ignored issues
show
Documentation introduced by
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...
179
			}
180
			
181
			header("Location: " . $this->redirect);
182
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirectIfIsSet() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
183
		}
184
	}
185
}