-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAclHelper.php
More file actions
102 lines (93 loc) · 2.78 KB
/
Copy pathAclHelper.php
File metadata and controls
102 lines (93 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
App::uses('AclComponent', 'Controller/Component');
App::uses('AuthComponent', 'Controller/Component');
App::uses('HtmlHelper', 'View/Helper');
App::uses('AppHelper', 'View/Helper');
class AclHelper extends AppHelper {
/**
* Инициализация Acl-компонента в хелпере
* @var
*/
public $Acl;
/**
* Инициализация Auth-компонента в хелпере
* @var
*/
public $Auth;
/**
* Инициализация HTML-хелпера
* @var
*/
public $Html;
/**
* AclHelper constructor
*/
public function __construct() {
$collection = new ComponentCollection();
$this->Acl = new AclComponent($collection);
$this->Auth = new AuthComponent($collection);
$this->Html = new HtmlHelper(new View());
}
/**
* Проверка доступности путей или массива путей для пользователя
* @param $url
* @return bool
*/
public function check($url) {
$groupId = $this->Auth->user('group_id');
$access = true;
if(is_array($url)) {
foreach ($url as $item) {
$item = $this->_getAcoPath($item);
$access &= $this->Acl->check(
array(
'model' => 'Group',
'foreign_key' => $groupId
), $item
);
}
} elseif(is_string($url)) {
$url = $this->_getAcoPath($url);
$access = $this->Acl->check(
array(
'model' => 'Group',
'foreign_key' => $groupId
), $url
);
}
return $access;
}
/**
* Проверка доступности хотя бы одного пути в массиве путей (для вывода пункта родительского меню)
* @param $urls
* @return bool
*/
public function checkList($urls) {
$access = false;
foreach ($urls as $url) {
if($access = $this->check($url)) {
break;
}
}
return $access;
}
/**
* @param $title
* @param $url
* @param array $options
* @param array $confirm
* @return bool|string
*/
public function link($title, $url, $options = array(), $confirm = array()) {
return $this->check($url) ? $this->Html->link($title, $url, $options, $confirm) : false;
}
/**
* @param $url
* @return string
*/
protected function _getAcoPath($url) {
$route = Router::parse($url);
$url = 'controllers/'.ucfirst($route['controller']).'/'.$route['action'];
return $url;
}
}