SystemRoleServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\utils\ApiErrorCode;
  18. use think\facade\Cache;
  19. /**
  20. * Class SystemRoleServices
  21. * @package app\services\system\admin
  22. * @method update($id, array $data, ?string $key = null) 修改数据
  23. * @method save(array $data) 保存数据
  24. * @method get(int $id, ?array $field = []) 获取数据
  25. * @method delete(int $id, ?string $key = null) 删除数据
  26. */
  27. class SystemRoleServices extends BaseServices
  28. {
  29. /**
  30. * 当前管理员权限缓存前缀
  31. */
  32. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  33. /**
  34. * SystemRoleServices constructor.
  35. * @param SystemRoleDao $dao
  36. */
  37. public function __construct(SystemRoleDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 获取权限
  43. * @return mixed
  44. */
  45. public function getRoleArray(array $where = [], string $field = '', string $key = '')
  46. {
  47. return $this->dao->getRoule($where, $field, $key);
  48. }
  49. /**
  50. * 获取表单所需的权限名称列表
  51. * @param int $level
  52. * @return array
  53. */
  54. public function getRoleFormSelect(int $level)
  55. {
  56. $list = $this->getRoleArray(['level' => $level, 'status' => 1]);
  57. $options = [];
  58. foreach ($list as $id => $roleName) {
  59. $options[] = ['label' => $roleName, 'value' => $id];
  60. }
  61. return $options;
  62. }
  63. /**
  64. * 身份管理列表
  65. * @param array $where
  66. * @return array
  67. */
  68. public function getRoleList(array $where)
  69. {
  70. [$page, $limit] = $this->getPageValue();
  71. $list = $this->dao->getRouleList($where, $page, $limit);
  72. $count = $this->dao->count($where);
  73. /** @var SystemMenusServices $service */
  74. $service = app()->make(SystemMenusServices::class);
  75. foreach ($list as &$item) {
  76. $item['rules'] = implode(',', array_merge($service->column(['id' => $item['rules']], 'menu_name', 'id')));
  77. }
  78. return compact('count', 'list');
  79. }
  80. /**
  81. * 后台验证权限
  82. * @param Request $request
  83. */
  84. public function verifiAuth(Request $request)
  85. {
  86. $rule = trim(strtolower($request->rule()->getRule()));
  87. $method = trim(strtolower($request->method()));
  88. if (in_array($rule, ['setting/admin/logout', 'menuslist'])) {
  89. return true;
  90. }
  91. //权限菜单未添加时返回true
  92. $allAuth = Cache::remember('all_auth', function () {
  93. /** @var SystemMenusServices $menusService */
  94. $menusService = app()->make(SystemMenusServices::class);
  95. return $menusService->getColumn([['api_url', '<>', ''], ['auth_type', '=', 2]], 'api_url,methods');
  96. });
  97. if (!in_array($rule, array_map(function ($item) {
  98. return trim(strtolower(str_replace(' ', '', $item)));
  99. }, array_column($allAuth, 'api_url')))) {
  100. return true;
  101. }
  102. //菜单按钮能看到的情况下所有接口都能访问
  103. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  104. //验证访问接口是否存在
  105. if (!in_array($rule, array_map(function ($item) {
  106. return trim(strtolower(str_replace(' ', '', $item)));
  107. }, array_column($auth, 'api_url')))) {
  108. // throw new AuthException(ApiErrorCode::ERR_RULE);
  109. return true;
  110. }
  111. //验证访问接口是否有权限
  112. if (empty(array_filter($auth, function ($item) use ($rule, $method) {
  113. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  114. return true;
  115. }))) {
  116. throw new AuthException(ApiErrorCode::ERR_AUTH);
  117. }
  118. }
  119. /**
  120. * 获取指定权限
  121. * @param array $rules
  122. * @param int $type
  123. * @param string $cachePrefix
  124. * @return array|mixed
  125. * @throws \throwable
  126. */
  127. public function getRolesByAuth(array $rules, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  128. {
  129. if (empty($rules)) return [];
  130. $cacheName = md5($cachePrefix . '_' . $type . '_' . implode('_', $rules));
  131. return Cache::remember($cacheName, function () use ($rules, $type) {
  132. /** @var SystemMenusServices $menusService */
  133. $menusService = app()->make(SystemMenusServices::class);
  134. return $menusService->getColumn([['id', 'IN', $this->getRoleIds($rules)], ['auth_type', '=', $type]], 'api_url,methods');
  135. });
  136. }
  137. /**
  138. * 获取权限id
  139. * @param array $rules
  140. * @return array
  141. */
  142. public function getRoleIds(array $rules)
  143. {
  144. $rules = $this->dao->getColumn([['id', 'IN', $rules], ['status', '=', '1']], 'rules', 'id');
  145. return array_unique(explode(',', implode(',', $rules)));
  146. }
  147. }