AuthItemChildService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace services\rbac;
  3. use Yii;
  4. use yii\db\ActiveQuery;
  5. use common\components\Service;
  6. use common\enums\WhetherEnum;
  7. use common\helpers\ArrayHelper;
  8. use common\helpers\StringHelper;
  9. use common\helpers\TreeHelper;
  10. use common\models\rbac\AuthItem;
  11. use common\models\rbac\AuthItemChild;
  12. use yii\web\UnprocessableEntityHttpException;
  13. /**
  14. * Class AuthItemChildService
  15. * @package services\rbac
  16. */
  17. class AuthItemChildService extends Service
  18. {
  19. /**
  20. * 当前的角色所有权限
  21. *
  22. * @var array
  23. */
  24. protected $allAuthNames = [];
  25. /**
  26. * 获取用户所有的权限 - 包含插件
  27. *
  28. * @param $roles
  29. * @return array
  30. */
  31. public function getAuthByRole($roles)
  32. {
  33. if (!empty($this->allAuthNames)) {
  34. return $this->allAuthNames;
  35. }
  36. // 获取当前角色的权限
  37. $allAuth = AuthItemChild::find()
  38. ->select(['addon_name', 'name'])
  39. ->where(['in', 'role_id', ArrayHelper::getColumn($roles, 'id')])
  40. ->asArray()
  41. ->all();
  42. $addonsName = [];
  43. foreach ($allAuth as $item) {
  44. !isset($addonsName[$item['addon_name']]) && $this->allAuthNames[] = $item['addon_name'];
  45. $this->allAuthNames[] = $item['name'];
  46. $addonsName[$item['addon_name']] = true;
  47. }
  48. return $this->allAuthNames;
  49. }
  50. /**
  51. * 授权
  52. *
  53. * @param int $role_id 角色ID
  54. * @param array $data 数据
  55. * @param string $is_addon 是否插件
  56. * @param string $app_id 应用ID
  57. * @throws \yii\db\Exception
  58. */
  59. public function accredit(int $role_id, array $data, int $is_addon, string $app_id)
  60. {
  61. // 删除原先所有权限
  62. AuthItemChild::deleteAll(['role_id' => $role_id, 'is_addon' => $is_addon]);
  63. if (empty($data)) {
  64. return;
  65. }
  66. $rows = [];
  67. $items = Yii::$app->services->rbacAuthItem->findByAppId($app_id, $data);
  68. foreach ($items as $value) {
  69. $rows[] = [
  70. $role_id,
  71. $value['id'],
  72. $value['name'],
  73. $value['app_id'],
  74. $value['is_addon'],
  75. $value['addon_name'],
  76. ];
  77. }
  78. $field = ['role_id', 'item_id', 'name', 'app_id', 'is_addon', 'addon_name'];
  79. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(AuthItemChild::tableName(), $field, $rows)->execute();
  80. }
  81. /**
  82. * @param $allAuthItem
  83. * @param $name
  84. * @throws UnprocessableEntityHttpException
  85. * @throws \yii\db\Exception
  86. */
  87. public function accreditByAddon($allAuthItem, $name, $delAuthItemChild = false)
  88. {
  89. // 卸载权限
  90. Yii::$app->services->rbacAuthItem->delByAddonName($name, $delAuthItemChild);
  91. // 重组
  92. foreach ($allAuthItem as &$val) {
  93. $val = ArrayHelper::regroupMapToArr($val);
  94. }
  95. $defaultAuth = [];
  96. // 重组路由
  97. $allAuth = [];
  98. foreach ($allAuthItem as $key => $item) {
  99. $allAuth = ArrayHelper::merge($allAuth, $this->regroupByAddonsData($item, $name, $key));
  100. }
  101. // 创建权限
  102. $rows = $this->createByAddonData(ArrayHelper::merge($defaultAuth, $allAuth));
  103. // 批量写入数据
  104. $field = ['title', 'name', 'app_id', 'is_addon', 'addon_name', 'pid', 'level', 'sort', 'tree', 'created_at', 'updated_at'];
  105. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(AuthItem::tableName(), $field, $rows)->execute();
  106. unset($data, $allAuth, $installData, $defaultAuth);
  107. }
  108. /**
  109. * @param $item
  110. * @param $name
  111. * @param $app_id
  112. * @return mixed
  113. */
  114. protected function regroupByAddonsData($item, $name, $app_id)
  115. {
  116. foreach ($item as &$value) {
  117. $value['app_id'] = $app_id;
  118. $value['is_addon'] = WhetherEnum::ENABLED;
  119. $value['addon_name'] = $name;
  120. // 组合子级
  121. if (isset($value['child']) && !empty($value['child'])) {
  122. $value['child'] = $this->regroupByAddonsData($value['child'], $name, $app_id);
  123. }
  124. }
  125. return $item;
  126. }
  127. /**
  128. * @param array $data
  129. * @param int $pid
  130. * @param int $level
  131. * @param AuthItem $parent
  132. * @throws UnprocessableEntityHttpException
  133. * @throws \yii\db\Exception
  134. */
  135. protected function createByAddonData(array $data, $pid = 0, $level = 1, $parent = '')
  136. {
  137. $rows = [];
  138. foreach ($data as $datum) {
  139. /** @var AuthItem $model */
  140. $model = new AuthItem();
  141. $model = $model->loadDefaultValues();
  142. $model->attributes = $datum;
  143. // 增加父级
  144. !empty($parent) && $model->setParent($parent);
  145. $model->pid = $pid;
  146. $model->level = $level;
  147. $model->name = '/' . StringHelper::toUnderScore($model->addon_name) . '/' . $model->name;
  148. !$model->validate() && $this->error($model);
  149. // 创建子权限
  150. if (isset($datum['child']) && !empty($datum['child'])) {
  151. // 有子权限的直接写入
  152. !$model->save() && $this->error($model);
  153. $rows = array_merge($rows, $this->createByAddonData($datum['child'], $model->id, $level++, $model));
  154. } else {
  155. $model->tree = !empty($parent) ? $parent->tree . TreeHelper::prefixTreeKey($parent->id) : TreeHelper::defaultTreeKey();
  156. $rows[] = [
  157. $model->title,
  158. $model->name,
  159. $model->app_id,
  160. $model->is_addon,
  161. $model->addon_name,
  162. $pid,
  163. $level,
  164. $model->sort ?? 9999,
  165. $model->tree,
  166. time(),
  167. time(),
  168. ];
  169. unset($model);
  170. }
  171. }
  172. return $rows;
  173. }
  174. /**
  175. * 获取某角色的所有权限
  176. *
  177. * @param $role_id
  178. * @return array
  179. */
  180. public function findItemByRoleId($role_id)
  181. {
  182. $role = Yii::$app->services->rbacAuthRole->findById($role_id);
  183. $auth = AuthItemChild::find()
  184. ->where(['role_id' => $role_id])
  185. ->with(['item' => function (ActiveQuery $activeQuery) use ($role) {
  186. return $activeQuery->andWhere(['app_id' => $role['app_id']]);
  187. }])
  188. ->asArray()
  189. ->all();
  190. return array_column($auth, 'item');
  191. }
  192. }
粤ICP备19079148号