12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace common\modules\rbac\models\searchs;
- use Yii;
- use yii\base\Model;
- use yii\data\ArrayDataProvider;
- use yii\rbac\Item;
- /**
- * AuthItemSearch represents the model behind the search form about AuthItem.
- *
- * @author Misbahul D Munir <misbahuldmunir@gmail.com>
- *
- * @since 1.0
- */
- class AuthItem extends Model
- {
- const TYPE_ROUTE = 101;
- public $name;
- public $type;
- public $description;
- public $rule;
- public $data;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['name', 'description'], 'safe'],
- [['type'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'name' => Yii::t('rbac', 'Name'),
- 'item_name' => Yii::t('rbac', 'Name'),
- 'type' => Yii::t('rbac', 'Type'),
- 'description' => Yii::t('rbac', 'Description'),
- 'ruleName' => Yii::t('rbac', 'Rule Name'),
- 'data' => Yii::t('rbac', 'Data'),
- ];
- }
- /**
- * Search authitem.
- *
- * @param array $params
- *
- * @return \yii\data\ActiveDataProvider|\yii\data\ArrayDataProvider
- */
- public function search($params)
- {
- /* @var \yii\rbac\Manager $authManager */
- $authManager = Yii::$app->authManager;
- if ($this->type == Item::TYPE_ROLE) {
- $items = $authManager->getRoles();
- } else {
- $items = [];
- if ($this->type == Item::TYPE_PERMISSION) {
- foreach ($authManager->getPermissions() as $name => $item) {
- if ($name[0] !== '/') {
- $items[$name] = $item;
- }
- }
- } else {
- foreach ($authManager->getPermissions() as $name => $item) {
- if ($name[0] === '/') {
- $items[$name] = $item;
- }
- }
- }
- }
- if ($this->load($params) && $this->validate() && (trim($this->name) !== '' || trim($this->description) !== '')) {
- $search = strtolower(trim($this->name));
- $desc = strtolower(trim($this->description));
- $items = array_filter($items, function ($item) use ($search, $desc) {
- return (empty($search) || strpos(strtolower($item->name), $search) !== false) && (empty($desc) || strpos(strtolower($item->description), $desc) !== false);
- });
- }
- return new ArrayDataProvider([
- 'allModels' => $items,
- ]);
- }
- }
|