AuthAssignmentService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace services\rbac;
  3. use Yii;
  4. use common\components\Service;
  5. use common\models\rbac\AuthAssignment;
  6. use yii\web\UnprocessableEntityHttpException;
  7. /**
  8. * Class AuthAssignmentService
  9. * @package services\rbac
  10. */
  11. class AuthAssignmentService extends Service
  12. {
  13. /**
  14. * 分配角色
  15. *
  16. * @param array $role_ids 角色id
  17. * @param int $user_id 用户id
  18. * @param string $app_id 应用id
  19. * @throws UnprocessableEntityHttpException
  20. */
  21. public function assign(array $role_ids, int $user_id, string $app_id)
  22. {
  23. // 移除已有的授权
  24. AuthAssignment::deleteAll(['user_id' => $user_id, 'app_id' => $app_id]);
  25. if (empty($role_ids)) {
  26. return true;
  27. }
  28. foreach ($role_ids as $role_id) {
  29. $model = new AuthAssignment();
  30. $model->user_id = $user_id;
  31. $model->role_id = $role_id;
  32. $model->app_id = $app_id;
  33. !$model->save() && $this->error($model);
  34. }
  35. return true;
  36. }
  37. /**
  38. * 获取当前用户权限的下面的所有用户id
  39. *
  40. * @param $app_id
  41. * @return array
  42. * @throws \yii\web\UnauthorizedHttpException
  43. */
  44. public function getChildIds($app_id)
  45. {
  46. if (Yii::$app->services->rbacAuth->isSuperAdmin()) {
  47. return [];
  48. }
  49. $roleIds = Yii::$app->services->rbacAuthRole->getRoleIds();
  50. $childRoles = Yii::$app->services->rbacAuthRole->findByLoginUser($app_id);
  51. $childRoleIds = array_keys($childRoles);
  52. if (!$childRoleIds) {
  53. return [-1];
  54. }
  55. foreach ($childRoleIds as $key => $childRoleId) {
  56. if (in_array($childRoleId, $roleIds)) {
  57. unset($childRoleIds[$key]);
  58. }
  59. }
  60. // 用户ID
  61. $userIds = $this->findUserIdByRoleId($childRoleIds);
  62. return !empty($userIds) ? $userIds : [-1];
  63. }
  64. /**
  65. * @param $app_id
  66. * @return array|int[]|\yii\db\ActiveRecord
  67. * @throws \yii\web\UnauthorizedHttpException
  68. */
  69. public function getRoleChildMap($app_id)
  70. {
  71. $roleIds = Yii::$app->services->rbacAuthRole->getRoleIds();
  72. $childRoles = Yii::$app->services->rbacAuthRole->findByLoginUser($app_id);
  73. foreach ($childRoles as $key => $childRole) {
  74. if (in_array($key, $roleIds)) {
  75. unset($childRoles[$key]);
  76. }
  77. }
  78. return !empty($childRoles) ? $childRoles : [];
  79. }
  80. /**
  81. * @param $user_id
  82. * @param $app_id
  83. * @return array|\yii\db\ActiveRecord|null
  84. */
  85. public function findRoleIdSByUserIdAndAppId($user_id, $app_id)
  86. {
  87. return AuthAssignment::find()
  88. ->select(['role_id'])
  89. ->where(['app_id' => $app_id])
  90. ->andWhere(['user_id' => $user_id])
  91. ->asArray()
  92. ->column();
  93. }
  94. /**
  95. * @param $user_id
  96. * @param $app_id
  97. * @return array|\yii\db\ActiveRecord|null
  98. */
  99. public function findUserIdByRoleId($role_ids)
  100. {
  101. return AuthAssignment::find()
  102. ->where(['in', 'role_id', $role_ids])
  103. ->select('user_id')
  104. ->asArray()
  105. ->column();
  106. }
  107. /**
  108. * @param $role_ids
  109. * @param $user_ids
  110. * @return array|\yii\db\ActiveRecord[]
  111. */
  112. public function findByUserIds($user_ids)
  113. {
  114. return AuthAssignment::find()
  115. ->where(['in', 'user_id', $user_ids])
  116. ->asArray()
  117. ->all();
  118. }
  119. }
粤ICP备19079148号