AuthItemChildService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace services\rbac;
  3. use common\models\rbac\AuthRole;
  4. use Yii;
  5. use yii\web\UnprocessableEntityHttpException;
  6. use common\components\Service;
  7. use common\models\rbac\AuthItemChild;
  8. use common\enums\AddonDefaultRouteEnum;
  9. use common\enums\AppEnum;
  10. use common\enums\AuthMenuEnum;
  11. use common\enums\WhetherEnum;
  12. use common\helpers\ArrayHelper;
  13. use common\helpers\StringHelper;
  14. use common\helpers\TreeHelper;
  15. use common\models\rbac\AuthItem;
  16. /**
  17. *
  18. * 已授权权限
  19. *
  20. * Class AuthItemChildService
  21. * @package services\rbac
  22. * @author qimall
  23. */
  24. class AuthItemChildService extends Service
  25. {
  26. /**
  27. * 当前的角色所有权限
  28. *
  29. * @var array
  30. */
  31. protected $allAuthNames = [];
  32. /**
  33. * 获取用户所有的权限 - 包含插件
  34. *
  35. * @param $role
  36. * @return array
  37. */
  38. public function getAuthByRole($role, $app_id, $addons_name = '')
  39. {
  40. if (!empty($this->allAuthNames)) {
  41. return $this->allAuthNames;
  42. }
  43. // 获取当前角色的权限
  44. // $allAuth = AuthItemChild::find()
  45. // ->select(['addons_name', 'name'])
  46. // ->where(['role_id' => $role['role_id']])
  47. // ->andWhere(['app_id' => $app_id])
  48. // ->andFilterWhere(['addons_name' => $addons_name])
  49. // ->asArray()
  50. // ->all();
  51. $i = 1;
  52. $page_size = 1000;
  53. $data = [];
  54. $query = AuthItemChild::find()->select(['addons_name', 'name'])
  55. ->where(['role_id' => $role['role_id'] ?? ''])
  56. ->andWhere(['app_id' => $app_id])
  57. ->andFilterWhere(['addons_name' => $addons_name])
  58. ->asArray();
  59. while ($list =$query->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  60. $data = array_merge($data, $list);
  61. $i++;
  62. }
  63. $allAuth = $data;
  64. $addonsName = [];
  65. foreach ($allAuth as $item) {
  66. !isset($addonsName[$item['addons_name']]) && $this->allAuthNames[] = $item['addons_name'];
  67. $this->allAuthNames[] = $item['name'];
  68. $addonsName[$item['addons_name']] = true;
  69. }
  70. return $this->allAuthNames;
  71. }
  72. /**
  73. * @param $allAuthItem
  74. * @param $name
  75. * @throws UnprocessableEntityHttpException
  76. * @throws \yii\db\Exception
  77. */
  78. public function accreditByAddon($allAuthItem, $name)
  79. {
  80. // 卸载权限
  81. Yii::$app->services->rbacAuthItem->delByAddonsName($name);
  82. // 重组
  83. foreach ($allAuthItem as &$val) {
  84. $val = ArrayHelper::regroupMapToArr($val, 'name');
  85. }
  86. $defaultAuth = [];
  87. // // 默认后台权限
  88. // if (!empty($allAuthItem[AppEnum::BACKEND])) {
  89. // $defaultAuth = ArrayHelper::merge(AddonDefaultRouteEnum::route(AppEnum::BACKEND, $name), $defaultAuth);
  90. // }
  91. //
  92. // // 默认商家权限
  93. // if (!empty($allAuthItem[AppEnum::MERCHANT])) {
  94. // $defaultAuth = ArrayHelper::merge(AddonDefaultRouteEnum::route(AppEnum::MERCHANT, $name), $defaultAuth);
  95. // }
  96. // 重组路由
  97. $allAuth = [];
  98. foreach ($allAuthItem as $key => $item) {
  99. $allAuth = ArrayHelper::merge($allAuth, $this->regroupByAddonsData($item, $name, $key));
  100. }
  101. // 创建权限
  102. $rows = $this->createByAddonsData(ArrayHelper::merge($defaultAuth, $allAuth));
  103. // 批量写入数据
  104. $field = ['title', 'name', 'app_id', 'is_addon', 'addons_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. * 授权
  110. *
  111. * @param int $role_id 角色ID
  112. * @param array $data 数据
  113. * @param string $is_addon 是否插件
  114. * @param string $app_id 应用ID
  115. * @throws \yii\db\Exception
  116. */
  117. public function accredit(int $role_id, array $data, int $is_addon, string $app_id)
  118. {
  119. // 删除原先所有权限
  120. AuthItemChild::deleteAll(['role_id' => $role_id, 'is_addon' => $is_addon]);
  121. if (empty($data)) {
  122. return;
  123. }
  124. $rows = [];
  125. $items = Yii::$app->services->rbacAuthItem->findByAppId($app_id, $data);
  126. foreach ($items as $value) {
  127. $rows[] = [
  128. $role_id,
  129. $value['id'],
  130. $value['name'],
  131. $value['app_id'],
  132. $value['is_addon'],
  133. $value['addons_name'],
  134. ];
  135. }
  136. $field = ['role_id', 'item_id', 'name', 'app_id', 'is_addon', 'addons_name'];
  137. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(AuthItemChild::tableName(), $field, $rows)->execute();
  138. }
  139. /**
  140. * 根据角色信息去授权
  141. *
  142. * @param AuthRole $role
  143. * @param $itemChilds
  144. * @throws \yii\db\Exception
  145. */
  146. public function accreditByDefault(AuthRole $role, $itemChilds)
  147. {
  148. $rows = [];
  149. foreach ($itemChilds as $child) {
  150. $rows[] = [
  151. $role->id,
  152. $child['item_id'],
  153. $child['name'],
  154. $child['app_id'],
  155. $child['is_addon'],
  156. $child['addons_name'],
  157. ];
  158. }
  159. $field = ['role_id', 'item_id', 'name', 'app_id', 'is_addon', 'addons_name'];
  160. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(AuthItemChild::tableName(), $field, $rows)->execute();
  161. }
  162. /**
  163. * 获取某角色的所有权限
  164. *
  165. * @param $role_id
  166. * @return array
  167. */
  168. public function findItemByRoleId($role_id)
  169. {
  170. $auth = AuthItemChild::find()
  171. ->where(['role_id' => $role_id])
  172. ->with(['item'])
  173. ->asArray()
  174. ->all();
  175. return array_column($auth, 'item');
  176. }
  177. /**
  178. * @param $item
  179. * @param $name
  180. * @param $app_id
  181. * @return mixed
  182. */
  183. protected function regroupByAddonsData($item, $name, $app_id)
  184. {
  185. foreach ($item as &$value) {
  186. $value['app_id'] = $app_id;
  187. $value['is_addon'] = WhetherEnum::ENABLED;
  188. $value['addons_name'] = $name;
  189. // 组合子级
  190. if (isset($value['child']) && !empty($value['child'])) {
  191. $value['child'] = $this->regroupByAddonsData($value['child'], $name, $app_id);
  192. }
  193. }
  194. return $item;
  195. }
  196. /**
  197. * @param array $data
  198. * @param int $pid
  199. * @param int $level
  200. * @param AuthItem $parent
  201. * @throws UnprocessableEntityHttpException
  202. * @throws \yii\db\Exception
  203. */
  204. protected function createByAddonsData(array $data, $pid = 0, $level = 1, $parent = '')
  205. {
  206. $rows = [];
  207. foreach ($data as $datum) {
  208. $model = new AuthItem();
  209. $model = $model->loadDefaultValues();
  210. $model->attributes = $datum;
  211. // 增加父级
  212. !empty($parent) && $model->setParent($parent);
  213. $model->pid = $pid;
  214. $model->level = $level;
  215. $model->name = StringHelper::toUnderScore($model->addons_name) . '/' . $model->name;
  216. $model->setScenario('addonsBatchCreate');
  217. if (!$model->validate()) {
  218. throw new UnprocessableEntityHttpException($model->getErrorMessage());
  219. }
  220. // 创建子权限
  221. if (isset($datum['child']) && !empty($datum['child'])) {
  222. // 有子权限的直接写入
  223. if (!$model->save()) {
  224. throw new UnprocessableEntityHttpException($model->getErrorMessage());
  225. }
  226. $rows = array_merge($rows, $this->createByAddonsData($datum['child'], $model->id, $level++, $model));
  227. } else {
  228. $model->tree = !empty($parent) ? $parent->tree . TreeHelper::prefixTreeKey($parent->id) : TreeHelper::defaultTreeKey();
  229. $rows[] = [
  230. $model->title,
  231. $model->name,
  232. $model->app_id,
  233. $model->is_addon,
  234. $model->addons_name,
  235. $pid,
  236. $level,
  237. $model->sort ?? 9999,
  238. $model->tree,
  239. time(),
  240. time(),
  241. ];
  242. unset($model);
  243. }
  244. }
  245. return $rows;
  246. }
  247. }