-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hierarchy.php
102 lines (87 loc) · 2.42 KB
/
Hierarchy.php
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
/**
* Author: Abramov A. aka MUTOgen
*
* Widget helps you to see whole rbac hierarchy by each role and see hierarchy for current user
* Place it wherever you want
*
*/
namespace mutogen\rbacw;
class Hierarchy extends \yii\base\Widget
{
/**
* User's id to display permission list
* @var null
*/
public $userId = null;
/**
* Full class name of User model
* @var string
*/
public $class = 'app\models\User';
/**
* Run this widget
* @return string|void
*/
public function run()
{
//if no userId specified, let's see permissions of the current user
if ($this->userId === null) $this->userId = \Yii::$app->user->identity->getId();
\Yii::trace('Run Rbac widget rendering');
return $this->render('hierarchy',array(
'auth' => \Yii::$app->authManager,
));
}
/**
* Format output string for recursive list
*
* @param $item
* @return string
*/
protected function _formatItem($item){
/**
* @var \yii\rbac\Item $item
*/
switch ($item->type) {
case 2:
$tag = 'u';
$name = \Yii::t('app/rbacw','Permission');
break;
case 1:
$tag = 'b';
$name = \Yii::t('app/rbacw','Role');
break;
default:
$tag = 'span';
$name = '?';
break;
}
return sprintf('<%s class="popoverHandler" data-content="%s" data-title="%s: %s">%s: %s</%s>',
$tag,$item->description,$name,$item->name,$name,$item->name,$tag);
}
/**
* Generate recursive list or roles and permissions
*
* @param $items
* @param $level
* @return string
*/
public function _recursiveList($items,$level)
{
$out = '';
$auth = \Yii::$app->authManager;
if ($level > 100) die('Too Deep Recursion');
if(is_array($items) && count($items) > 0){
foreach($items as $i){
$out .= str_repeat(' ', $level);
$out .= $this->_formatItem($i);
$out .= '<br/>';
$childs = $auth->getChildren($i->name);
$out.= $this->_recursiveList($childs, $level+1);
}
} else {
return '';
}
return $out;
}
}