AuthItemService.php 2.5 KB

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