AuthRoleService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace services\rbac;
  3. use Yii;
  4. use yii\web\UnauthorizedHttpException;
  5. use common\enums\WhetherEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\ArrayHelper;
  8. use common\models\rbac\AuthRole;
  9. use common\helpers\TreeHelper;
  10. /**
  11. * Class AuthRoleService
  12. * @package services\rbac
  13. */
  14. class AuthRoleService
  15. {
  16. /**
  17. * 角色信息
  18. *
  19. * @var array
  20. */
  21. protected $roles = [];
  22. /**
  23. * 获取是否所有角色的条件
  24. *
  25. * @param bool $sourceAuthChild
  26. * @return array
  27. * @throws UnauthorizedHttpException
  28. */
  29. public function roleCondition($sourceAuthChild = false)
  30. {
  31. if ($sourceAuthChild == false || Yii::$app->services->rbacAuth->isSuperAdmin()) {
  32. return [];
  33. }
  34. $roles = Yii::$app->services->rbacAuthRole->getRoles();
  35. $where = [];
  36. $where[0] = 'or';
  37. $where[] = ['in', 'id', ArrayHelper::getColumn($roles, 'id')];
  38. foreach ($roles as $role) {
  39. $where[] = ['like', 'tree', $role['tree'] . TreeHelper::prefixTreeKey($role['id']) . '%', false];
  40. $where[] = ['like', 'tree', $role['tree'] . TreeHelper::prefixTreeKey($role['id']) . '%', false];
  41. }
  42. return $where;
  43. }
  44. /**
  45. * 获取当前登录的角色ID
  46. *
  47. * @return array
  48. * @throws UnauthorizedHttpException
  49. */
  50. public function getRoleIds()
  51. {
  52. return ArrayHelper::getColumn($this->getRoles(), 'id');
  53. }
  54. /**
  55. * 获取当前登录角色信息
  56. *
  57. * @return array|\yii\db\ActiveRecord[]
  58. * @throws UnauthorizedHttpException
  59. */
  60. public function getRoles()
  61. {
  62. if (Yii::$app->services->rbacAuth->isSuperAdmin()) {
  63. return [];
  64. }
  65. if (!$this->roles) {
  66. /* @var $assignment \common\models\rbac\AuthAssignment */
  67. if (empty($assignment = Yii::$app->user->identity->assignment ?? '')) {
  68. Yii::$app->user->logout();
  69. throw new UnauthorizedHttpException('未授权角色,请联系管理员');
  70. }
  71. $assignment = ArrayHelper::toArray($assignment);
  72. $this->roles = AuthRole::find()
  73. ->where(['in', 'id', ArrayHelper::getColumn($assignment, 'role_id')])
  74. ->andWhere(['status' => StatusEnum::ENABLED])
  75. ->asArray()
  76. ->all();
  77. if (!$this->roles) {
  78. throw new UnauthorizedHttpException('授权的角色已失效,请联系管理员');
  79. }
  80. }
  81. return $this->roles;
  82. }
  83. /**
  84. * 获取编辑的数据
  85. *
  86. * @param int $role_id
  87. * @param array $allAuth
  88. * @return array
  89. *
  90. */
  91. public function getJsTreeData($role_id, array $allAuth)
  92. {
  93. // 当前角色已有的权限
  94. $userAuth = Yii::$app->services->rbacAuthItemChild->findItemByRoleId($role_id);
  95. $addonName = $formAuth = $checkIds = $addonFormAuth = $addonsCheckIds = [];
  96. // 区分默认和插件权限
  97. foreach ($allAuth as $item) {
  98. if ($item['is_addon'] == WhetherEnum::DISABLED) {
  99. $formAuth[] = $item;
  100. } else {
  101. if ($item['pid'] == 0) {
  102. $item['pid'] = $item['addon_name'];
  103. }
  104. $addonFormAuth[] = $item;
  105. $addonName[] = $item['addon_name'];
  106. }
  107. }
  108. // 获取顶级插件数据
  109. $addons = Yii::$app->services->addons->findByNames($addonName);
  110. foreach ($addons as $addon) {
  111. $addonFormAuth[] = [
  112. 'id' => $addon['name'],
  113. 'pid' => 0,
  114. 'title' => $addon['title'],
  115. ];
  116. }
  117. // 区分默认和插件权限ID
  118. foreach ($userAuth as $value) {
  119. if (empty($value)) {
  120. continue;
  121. }
  122. if ($value['is_addon'] == WhetherEnum::DISABLED) {
  123. $checkIds[] = $value['id'];
  124. } else {
  125. $addonsCheckIds[] = $value['id'];
  126. }
  127. }
  128. return [$formAuth, $checkIds, $addonFormAuth, $addonsCheckIds];
  129. }
  130. /**
  131. * 获取上级角色
  132. *
  133. * @param $appId
  134. * @param false $sourceAuthChild
  135. * @param string $id
  136. * @return array
  137. * @throws UnauthorizedHttpException
  138. */
  139. public function getDropDownForEdit($appId, $sourceAuthChild = false, $id = '', $defaultData = [])
  140. {
  141. $list = $this->findAll($appId, Yii::$app->services->merchant->getNotNullId(), $this->roleCondition($sourceAuthChild));
  142. $list = ArrayHelper::merge($list, $defaultData);
  143. $list = ArrayHelper::removeByValue($list, $id);
  144. $list = ArrayHelper::arrayKey($list, 'id');
  145. $list = ArrayHelper::arraySort($list, 'level');
  146. foreach ($list as &$item) {
  147. if (!isset($list[$item['pid']])) {
  148. $item['pid'] = 0;
  149. $item['level'] = 1;
  150. }
  151. if ($item['pid'] > 0 && isset($list[$item['pid']])) {
  152. $item['level'] = $list[$item['pid']]['level'] + 1;
  153. }
  154. }
  155. $models = ArrayHelper::itemsMerge($list);
  156. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  157. if (Yii::$app->services->rbacAuth->isSuperAdmin()) {
  158. return ArrayHelper::merge([0 => '顶级角色'], $data);
  159. }
  160. return $data;
  161. }
  162. /**
  163. * @param $app_id
  164. * @param $merchant_id
  165. * @param array $condition
  166. * @return array
  167. */
  168. public function getMapList($app_id, $merchant_id, $condition = [])
  169. {
  170. $list = $this->findAll($app_id, $merchant_id, $condition);
  171. $data = [];
  172. foreach ($list as $item) {
  173. $data[$item['id'] ] = $item['title'];
  174. if ($item['annual_fee'] > 0) {
  175. $data[$item['id'] ] = $item['title'] . ' | ' . '年费(' . $item['annual_fee'] . ')';
  176. }
  177. }
  178. return $data;
  179. }
  180. /**
  181. * @param $app_id
  182. * @return array
  183. * @throws UnauthorizedHttpException
  184. */
  185. public function findByLoginUser($app_id)
  186. {
  187. $roles = $this->findAll($app_id, Yii::$app->services->merchant->getNotNullId(), $this->roleCondition(true));
  188. return ArrayHelper::map($roles, 'id', 'title');
  189. }
  190. /**
  191. * @param $id
  192. * @return array|null|\yii\db\ActiveRecord
  193. */
  194. public function findById($id)
  195. {
  196. return AuthRole::find()
  197. ->where(['id' => $id])
  198. ->asArray()
  199. ->one();
  200. }
  201. /**
  202. * 查询所有角色信息
  203. *
  204. * @return array
  205. */
  206. public function findAll($app_id, $merchant_id, $condition = []): array
  207. {
  208. return AuthRole::find()
  209. ->where(['app_id' => $app_id])
  210. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  211. ->andFilterWhere(['merchant_id' => $merchant_id])
  212. ->andFilterWhere($condition)
  213. ->orderBy('sort asc, created_at asc')
  214. ->asArray()
  215. ->all();
  216. }
  217. }
粤ICP备19079148号