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()); |
|
|
|
|
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')) { |
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$params += ['_base' => false]; |
67
|
|
|
|
68
|
|
|
$url = Router::url($params); |
69
|
|
|
$params = Router::parse($url); |
|
|
|
|
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)) { |
|
|
|
|
94
|
|
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->Html->link($title, $url, $options); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.