AclHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
namespace App\View\Helper;
3
4
use Acl\Auth\ActionsAuthorize;
5
use Acl\Controller\Component\AclComponent;
6
use Cake\Controller\ComponentRegistry;
7
use Cake\Network\Request;
8
use Cake\Routing\Router;
9
use Cake\View\Helper;
10
use Cake\View\View;
11
12
class AclHelper extends Helper
13
{
14
15
    /**
16
     * Helpers used.
17
     *
18
     * @var array
19
     */
20
    public $helpers = ['Html'];
21
22
    /**
23
     * Acl Instance.
24
     *
25
     * @var object
26
     */
27
    public $Acl;
28
29
    /**
30
     * ActionsAuthorize Instance.
31
     *
32
     * @var object
33
     */
34
    public $Authorize;
35
36
    /**
37
     * Construct method.
38
     *
39
     * @param \Cake\View\View $view The view that was fired.
40
     * @param array $config The config passed to the class.
41
     */
42
    public function __construct(View $view, $config = [])
43
    {
44
        parent::__construct($view, $config);
45
46
        $collection = new ComponentRegistry();
47
        $this->Acl = new AclComponent($collection);
48
49
        $this->Authorize = new ActionsAuthorize($collection);
50
        $this->Authorize->config($this->config());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Core\InstanceConfigTrait::config() has been deprecated with message: 3.4.0 use setConfig()/getConfig() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
    }
52
53
    /**
54
     * Check if the user can access to the given URL.
55
     *
56
     * @param array $params The params to check.
57
     *
58
     * @return bool
59
     */
60
    public function check(array $params = [])
61
    {
62
        if (!$this->request->session()->read('Auth.User')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->request->session()->read('Auth.User') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
            return false;
64
        }
65
66
        $params += ['_base' => false];
67
68
        $url = Router::url($params);
69
        $params = Router::parse($url);
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Routing\Router::parse() has been deprecated with message: 3.4.0 Use Router::parseRequest() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
70
71
        $user = [$this->Authorize->config('userModel') => $this->request->session()->read('Auth.User')];
72
73
        $request = new Request();
74
        $request->addParams($params);
75
76
        $action = $this->Authorize->action($request);
77
78
        return $this->Acl->check($user, $action);
79
    }
80
81
    /**
82
     * Generate the link only if the user has access to the given url.
83
     *
84
     * @param string $title The content to be wrapped by <a> tags.
85
     * @param string|array|null $url Cake-relative URL or array of URL parameters, or
86
     * external URL (starts with http://)
87
     * @param array $options Array of options and HTML attributes.
88
     *
89
     * @return string
90
     */
91
    public function link($title, $url = null, array $options = [])
92
    {
93
        if (!$this->check($url)) {
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 91 can also be of type null or string; however, App\View\Helper\AclHelper::check() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
            return '';
95
        }
96
97
        return $this->Html->link($title, $url, $options);
0 ignored issues
show
Documentation introduced by
The property Html does not exist on object<App\View\Helper\AclHelper>. 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...
98
    }
99
}
100