AuthRoleService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace services\rbac;
  3. use backend\models\AdminRole;
  4. use Yii;
  5. use common\components\Service;
  6. use common\enums\StatusEnum;
  7. use common\enums\WhetherEnum;
  8. use common\helpers\ArrayHelper;
  9. use common\helpers\TreeHelper;
  10. use common\models\rbac\AuthRole;
  11. use common\enums\AppEnum;
  12. use yii\web\UnauthorizedHttpException;
  13. /**
  14. *
  15. * 角色
  16. *
  17. * Class AuthRoleService
  18. * @package services\rbac
  19. * @author qimall
  20. */
  21. class AuthRoleService extends Service
  22. {
  23. /**
  24. * 角色信息
  25. *
  26. * @var array
  27. */
  28. protected $roles = [];
  29. /**
  30. * 获取当前角色信息
  31. *
  32. * @return array|\yii\db\ActiveRecord|null
  33. * @throws UnauthorizedHttpException
  34. */
  35. public function getRole()
  36. {
  37. if (Yii::$app->services->auth->isSuperAdmin()) {
  38. return [];
  39. }
  40. if(empty(Yii::$app->user->identity) || empty(Yii::$app->user->identity->getId())) return [];
  41. $admin_role = AdminRole::findOne(['admin_id'=>Yii::$app->user->identity->getId()]);
  42. if(empty($admin_role)){
  43. return [];
  44. }
  45. return $admin_role;
  46. }
  47. /**
  48. * 检测用户商场管理员角色
  49. * @return bool
  50. */
  51. public function isMallAdmin()
  52. {
  53. $admin_role = AdminRole::findOne(['admin_id'=>Yii::$app->user->identity->getId()]);
  54. if (empty($admin_role)) return false;
  55. $auth = AuthRole::findOne(['id' => $admin_role->role_id, 'is_default' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]);
  56. if ($auth) return true;
  57. return false;
  58. }
  59. /**
  60. * 获取编辑的数据
  61. *
  62. * @param int $role_id
  63. * @param array $allAuth
  64. * @return array
  65. *
  66. */
  67. public function getJsTreeData($role_id, array $allAuth)
  68. {
  69. // 当前角色已有的权限
  70. $auth = Yii::$app->services->rbacAuthItemChild->findItemByRoleId($role_id);
  71. $addonName = $formAuth = $checkIds = $addonFormAuth = $addonsCheckIds = [];
  72. // 区分默认和插件权限
  73. foreach ($allAuth as $item) {
  74. if ($item['is_addon'] == WhetherEnum::DISABLED) {
  75. $formAuth[] = $item;
  76. } else {
  77. if ($item['pid'] == 0) {
  78. $item['pid'] = $item['addons_name'];
  79. }
  80. $addonFormAuth[] = $item;
  81. $addonName[$item['addons_name']] = $item['addons_name'];
  82. }
  83. }
  84. // 获取顶级插件数据
  85. $addons = Yii::$app->services->addons->findByNames(array_keys($addonName));
  86. foreach ($addons as $addon) {
  87. $addonFormAuth[] = [
  88. 'id' => $addon['name'],
  89. 'pid' => 0,
  90. 'title' => $addon['title'],
  91. ];
  92. }
  93. // 区分默认和插件权限ID
  94. foreach ($auth as $value) {
  95. if ($value['is_addon'] == WhetherEnum::DISABLED) {
  96. $checkIds[] = $value['id'];
  97. } else {
  98. $addonsCheckIds[] = $value['id'];
  99. }
  100. }
  101. return [$formAuth, $checkIds, $addonFormAuth, $addonsCheckIds];
  102. }
  103. /**
  104. * 获取角色名称
  105. *
  106. * @return array|mixed|string
  107. * @throws UnauthorizedHttpException
  108. */
  109. public function getTitle()
  110. {
  111. return $this->getRole()['title'] ?? '游客';
  112. }
  113. /**
  114. * 获取上级角色
  115. *
  116. * 注意:如果是其他应用则需要自行获取上级
  117. *
  118. * @param $id
  119. * @return array
  120. */
  121. public function getDropDownForEdit($app_id, $id = '')
  122. {
  123. $list = $this->findAll($app_id, Yii::$app->services->merchant->getId());
  124. $list = ArrayHelper::removeByValue($list, $id);
  125. $models = ArrayHelper::itemsMerge($list);
  126. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  127. if (Yii::$app->services->auth->isSuperAdmin()) {
  128. return ArrayHelper::merge([0 => '顶级角色'], $data);
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 获取是否所有角色的条件
  134. *
  135. * @param bool $sourceAuthChild
  136. * @return array
  137. * @throws UnauthorizedHttpException
  138. */
  139. public function roleCondition($sourceAuthChild = false)
  140. {
  141. if ($sourceAuthChild == false) {
  142. return [];
  143. }
  144. $role = Yii::$app->services->rbacAuthRole->getRole();
  145. if (empty($role)) {
  146. return [];
  147. }
  148. $tree = $role['tree'] . TreeHelper::prefixTreeKey($role['id']);
  149. return ['like', 'tree', $tree . '%', false];
  150. }
  151. /**
  152. * 获取分配角色列表
  153. *
  154. * @param string $app_id 应用id
  155. * @param bool $sourceAuthChild 权限来源(false:所有权限,true:当前角色)
  156. * @return array
  157. * @throws UnauthorizedHttpException
  158. */
  159. public function getDropDown($app_id, $sourceAuthChild = false)
  160. {
  161. $list = $this->findAll($app_id, Yii::$app->services->merchant->getId(), $this->roleCondition($sourceAuthChild));
  162. $pid = 0;
  163. $treeStat = 1;
  164. if ($sourceAuthChild == true && ($role = Yii::$app->services->rbacAuthRole->getRole())) {
  165. $pid = $role['id'];
  166. $treeStat = $role['level'] + 1;
  167. }
  168. $models = ArrayHelper::itemsMerge($list, $pid);
  169. return ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models, 'id', 'title', $treeStat), 'id', 'title');
  170. }
  171. /**
  172. * 获取当前角色的子角色
  173. *
  174. * @return array
  175. * @throws UnauthorizedHttpException
  176. */
  177. public function getChildes($app_id): array
  178. {
  179. return $this->findAll($app_id, Yii::$app->services->merchant->getId(), $this->roleCondition(true));
  180. }
  181. /**
  182. * 复制默认角色进入商户
  183. *
  184. * @param $app_id
  185. * @param $merchant_id
  186. * @return bool|AuthRole
  187. * @throws \yii\db\Exception
  188. */
  189. public function cloneInDefault($app_id, $merchant_id)
  190. {
  191. if (!($default = $this->findDefault($app_id))) {
  192. return false;
  193. }
  194. $role = new AuthRole();
  195. $role->attributes = $default;
  196. $role->merchant_id = $merchant_id;
  197. if ($role->save()) {
  198. Yii::$app->services->rbacAuthItemChild->accreditByDefault($role, $default['authItemChild']);
  199. return $role;
  200. }
  201. return false;
  202. }
  203. /**
  204. * 查询所有角色信息
  205. *
  206. * @return array
  207. */
  208. public function findAll($app_id, $merchant_id, $condition = []): array
  209. {
  210. return AuthRole::find()
  211. ->where(['app_id' => $app_id])
  212. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  213. ->andFilterWhere(['merchant_id' => $merchant_id])
  214. ->andFilterWhere($condition)
  215. ->orderBy('sort asc, created_at asc')
  216. ->asArray()
  217. ->all();
  218. }
  219. /**
  220. * 获取默认已启用的角色
  221. *
  222. * @param $app_id
  223. * @return array|\yii\db\ActiveRecord|null
  224. */
  225. public function findDefault($app_id)
  226. {
  227. return AuthRole::find()
  228. ->where([
  229. 'is_default' => StatusEnum::ENABLED,
  230. 'app_id' => $app_id,
  231. 'status' => StatusEnum::ENABLED,
  232. ])
  233. ->with('authItemChild')
  234. ->asArray()
  235. ->one();
  236. }
  237. /**
  238. * 获取当前商户默认已启用的角色
  239. *
  240. * @param $app_id
  241. * @return array|\yii\db\ActiveRecord|null|AuthRole
  242. */
  243. public function findDefaultByMerchantId($merchant_id, $app_id = AppEnum::MERCHANT)
  244. {
  245. return AuthRole::find()
  246. ->where([
  247. 'is_default' => StatusEnum::ENABLED,
  248. 'app_id' => $app_id,
  249. 'merchant_id' => $merchant_id,
  250. 'status' => StatusEnum::ENABLED,
  251. ])
  252. ->one();
  253. }
  254. /**
  255. * @param $id
  256. * @return array|null|\yii\db\ActiveRecord
  257. */
  258. public function findById($id)
  259. {
  260. return AuthRole::find()
  261. ->where(['id' => $id])
  262. ->asArray()
  263. ->one();
  264. }
  265. }