1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\rbac\models\search; |
4
|
|
|
|
5
|
|
|
use dosamigos\arrayquery\ArrayQuery; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\Model; |
8
|
|
|
use yii\data\ArrayDataProvider; |
9
|
|
|
use yii\rbac\Item; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AuthItemSearch |
13
|
|
|
* |
14
|
|
|
* @package yii2mod\rbac\models\search |
15
|
|
|
*/ |
16
|
|
|
class AuthItemSearch extends Model |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string auth item name |
20
|
|
|
*/ |
21
|
|
|
public $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int auth item type |
25
|
|
|
*/ |
26
|
|
|
public $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string auth item description |
30
|
|
|
*/ |
31
|
|
|
public $description; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string auth item rule name |
35
|
|
|
*/ |
36
|
|
|
public $ruleName; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int the default page size |
40
|
|
|
*/ |
41
|
|
|
public $pageSize = 25; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function rules(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
[['name', 'ruleName', 'description'], 'trim'], |
50
|
|
|
[['type'], 'integer'], |
51
|
|
|
[['name', 'ruleName', 'description'], 'safe'], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function attributeLabels(): array |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'name' => Yii::t('yii2mod.rbac', 'Name'), |
62
|
|
|
'type' => Yii::t('yii2mod.rbac', 'Type'), |
63
|
|
|
'description' => Yii::t('yii2mod.rbac', 'Description'), |
64
|
|
|
'rule' => Yii::t('yii2mod.rbac', 'Rule'), |
65
|
|
|
'data' => Yii::t('yii2mod.rbac', 'Data'), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates data provider instance with search query applied |
71
|
|
|
* |
72
|
|
|
* @param array $params |
73
|
|
|
* |
74
|
|
|
* @return \yii\data\ArrayDataProvider |
75
|
|
|
*/ |
76
|
|
|
public function search(array $params): ArrayDataProvider |
77
|
|
|
{ |
78
|
|
|
$authManager = Yii::$app->getAuthManager(); |
79
|
|
|
|
80
|
|
|
if ($this->type == Item::TYPE_ROLE) { |
81
|
|
|
$items = $authManager->getRoles(); |
82
|
|
|
} else { |
83
|
|
|
$items = array_filter($authManager->getPermissions(), function ($item) { |
84
|
|
|
return strpos($item->name, '/') !== 0; |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$query = new ArrayQuery($items); |
89
|
|
|
|
90
|
|
|
$this->load($params); |
91
|
|
|
|
92
|
|
|
if ($this->validate()) { |
93
|
|
|
$query->addCondition('name', $this->name ? "~{$this->name}" : null) |
94
|
|
|
->addCondition('ruleName', $this->ruleName ? "~{$this->ruleName}" : null) |
95
|
|
|
->addCondition('description', $this->description ? "~{$this->description}" : null); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new ArrayDataProvider([ |
99
|
|
|
'allModels' => $query->find(), |
100
|
|
|
'sort' => [ |
101
|
|
|
'attributes' => ['name'], |
102
|
|
|
], |
103
|
|
|
'pagination' => [ |
104
|
|
|
'pageSize' => $this->pageSize, |
105
|
|
|
], |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.