SystemRoleServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\admin;
  12. use app\dao\system\admin\SystemRoleDao;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\services\system\SystemMenusServices;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\services\CacheService;
  18. /**
  19. * Class SystemRoleServices
  20. * @package app\services\system\admin
  21. * @method update($id, array $data, ?string $key = null) 修改数据
  22. * @method save(array $data) 保存数据
  23. * @method get(int $id, ?array $field = []) 获取数据
  24. * @method delete(int $id, ?string $key = null) 删除数据
  25. */
  26. class SystemRoleServices extends BaseServices
  27. {
  28. /**
  29. * 当前管理员权限缓存前缀
  30. */
  31. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  32. /**
  33. * SystemRoleServices constructor.
  34. * @param SystemRoleDao $dao
  35. */
  36. public function __construct(SystemRoleDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取权限
  42. * @return mixed
  43. */
  44. public function getRoleArray(array $where = [], string $field = '', string $key = '')
  45. {
  46. return $this->dao->getRoule($where, $field, $key);
  47. }
  48. /**
  49. * 获取表单所需的权限名称列表
  50. * @param int $level
  51. * @return array
  52. */
  53. public function getRoleFormSelect(int $level)
  54. {
  55. $list = $this->getRoleArray(['level' => $level, 'status' => 1]);
  56. $options = [];
  57. foreach ($list as $id => $roleName) {
  58. $options[] = ['label' => $roleName, 'value' => $id];
  59. }
  60. return $options;
  61. }
  62. /**
  63. * 身份管理列表
  64. * @param array $where
  65. * @return array
  66. */
  67. public function getRoleList(array $where)
  68. {
  69. [$page, $limit] = $this->getPageValue();
  70. $list = $this->dao->getRouleList($where, $page, $limit);
  71. $count = $this->dao->count($where);
  72. /** @var SystemMenusServices $service */
  73. $service = app()->make(SystemMenusServices::class);
  74. foreach ($list as &$item) {
  75. $item['rules'] = implode(',', array_merge($service->column(['id' => $item['rules']], 'menu_name', 'id')));
  76. }
  77. return compact('count', 'list');
  78. }
  79. /**
  80. * 后台验证权限
  81. * @param Request $request
  82. * @return bool|void
  83. * @throws \throwable
  84. */
  85. public function verifyAuth(Request $request)
  86. {
  87. // 获取当前的接口于接口类型
  88. $rule = trim(strtolower($request->rule()->getRule()));
  89. $method = trim(strtolower($request->method()));
  90. // 判断接口是一下两种的时候放行
  91. if (in_array($rule, ['setting/admin/logout', 'menuslist'])) {
  92. return true;
  93. }
  94. // 获取所有接口类型以及对应的接口
  95. $allAuth = CacheService::remember('all_auth', function () {
  96. /** @var SystemMenusServices $menusService */
  97. $menusService = app()->make(SystemMenusServices::class);
  98. $allList = $menusService->getColumn([['api_url', '<>', ''], ['auth_type', '=', 2]], 'api_url,methods');
  99. $allAuth = [];
  100. foreach ($allList as $item) {
  101. $allAuth[trim(strtolower($item['methods']))][] = trim(strtolower(str_replace(' ', '', $item['api_url'])));
  102. }
  103. return $allAuth;
  104. });
  105. // 权限菜单未添加时放行
  106. if (!in_array($rule, $allAuth[$method])) return true;
  107. // 获取管理员的接口权限列表,存在时放行
  108. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  109. if (isset($auth[$method]) && in_array($rule, $auth[$method])) {
  110. return true;
  111. } else {
  112. throw new AuthException(110000);
  113. }
  114. }
  115. /**
  116. * 获取指定权限
  117. * @param array $rules
  118. * @param int $type
  119. * @param string $cachePrefix
  120. * @return array|mixed
  121. * @throws \throwable
  122. */
  123. public function getRolesByAuth(array $rules, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  124. {
  125. if (empty($rules)) return [];
  126. $cacheName = md5($cachePrefix . '_' . $type . '_' . implode('_', $rules));
  127. return CacheService::remember($cacheName, function () use ($rules, $type) {
  128. /** @var SystemMenusServices $menusService */
  129. $menusService = app()->make(SystemMenusServices::class);
  130. $authList = $menusService->getColumn([['id', 'IN', $this->getRoleIds($rules)], ['auth_type', '=', $type]], 'api_url,methods');
  131. $rolesAuth = [];
  132. foreach ($authList as $item) {
  133. $rolesAuth[trim(strtolower($item['methods']))][] = trim(strtolower(str_replace(' ', '', $item['api_url'])));
  134. }
  135. return $rolesAuth;
  136. });
  137. }
  138. /**
  139. * 获取权限id
  140. * @param array $rules
  141. * @return array
  142. */
  143. public function getRoleIds(array $rules)
  144. {
  145. $rules = $this->dao->getColumn([['id', 'IN', $rules], ['status', '=', '1']], 'rules', 'id');
  146. return array_unique(explode(',', implode(',', $rules)));
  147. }
  148. }