AuthItemService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace services\rbac;
  3. use common\components\Service;
  4. use common\enums\AppEnum;
  5. use common\enums\StatusEnum;
  6. use common\enums\WhetherEnum;
  7. use common\helpers\ArrayHelper;
  8. use common\models\rbac\AuthItem;
  9. use common\models\rbac\AuthItemChild;
  10. /**
  11. *
  12. * 权限配置
  13. *
  14. * Class AuthItemService
  15. * @package services\rbac
  16. * @author qimall
  17. */
  18. class AuthItemService extends Service
  19. {
  20. /**
  21. * 卸载插件
  22. *
  23. * @param $name
  24. */
  25. public function delByAddonsName($name)
  26. {
  27. AuthItem::deleteAll(['is_addon' => WhetherEnum::ENABLED, 'addons_name' => $name]);
  28. // AuthItemChild::deleteAll(['is_addon' => WhetherEnum::ENABLED, 'addons_name' => $name]);
  29. }
  30. /**
  31. * 编辑下拉选择框数据
  32. *
  33. * @param $app_id
  34. * @param string $id
  35. * @return array
  36. */
  37. public function getDropDownForEdit($app_id, $id = '')
  38. {
  39. $list = AuthItem::find()
  40. ->where(['>=', 'status', StatusEnum::DISABLED])
  41. // ->andWhere(['app_id' => $app_id, 'is_addon' => WhetherEnum::DISABLED])
  42. ->andWhere(['app_id' => $app_id])
  43. ->andFilterWhere(['<>', 'id', $id])
  44. ->select(['id', 'title', 'pid', 'level'])
  45. ->orderBy('sort asc')
  46. ->asArray()
  47. ->all();
  48. $models = ArrayHelper::itemsMerge($list);
  49. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  50. return ArrayHelper::merge([0 => '顶级权限'], $data);
  51. }
  52. /**
  53. * @param array $ids
  54. * @param string $app_id
  55. * @return array|\yii\db\ActiveRecord[]
  56. */
  57. public function findByAppId($app_id = AppEnum::BACKEND, $ids = [])
  58. {
  59. return AuthItem::find()
  60. ->where(['status' => StatusEnum::ENABLED])
  61. ->andWhere(['app_id' => $app_id])
  62. ->andFilterWhere(['in', 'id', $ids])
  63. ->select(['id', 'title', 'name', 'pid', 'level', 'app_id', 'is_addon', 'addons_name'])
  64. ->orderBy('sort asc, id asc')
  65. ->asArray()
  66. ->all();
  67. }
  68. /**
  69. * 查询当前应用所有权限配置
  70. *
  71. * @param string $app_id 应用id
  72. * @param int $is_addons 是否插件
  73. * @param string $addons_name 插件名称
  74. */
  75. public function findAll($app_id = AppEnum::BACKEND)
  76. {
  77. return AuthItem::find()
  78. ->where(['app_id' => $app_id])
  79. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  80. ->orderBy('sort asc, created_at asc')
  81. ->asArray()
  82. ->all();
  83. }
  84. }