SystemRoleServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  87. $rule = trim(strtolower($request->rule()->getRule()));
  88. $method = trim(strtolower($request->method()));
  89. if ($rule == 'setting/admin/logout') {
  90. return true;
  91. }
  92. //验证访问接口是否存在
  93. if (!in_array($rule, array_map(function ($item) {
  94. return trim(strtolower(str_replace(' ', '', $item)));
  95. }, array_column($auth, 'api_url')))) {
  96. throw new AuthException(ApiErrorCode::ERR_RULE);
  97. }
  98. //验证访问接口是否有权限
  99. if (empty(array_filter($auth, function ($item) use ($rule, $method) {
  100. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  101. return true;
  102. }))) {
  103. throw new AuthException(ApiErrorCode::ERR_AUTH);
  104. }
  105. }
  106. /**
  107. * 获取指定权限
  108. * @param array $rules
  109. * @param int $type
  110. * @param string $cachePrefix
  111. * @return array|mixed
  112. * @throws \throwable
  113. */
  114. public function getRolesByAuth(array $rules, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  115. {
  116. if (empty($rules)) return [];
  117. $cacheName = md5($cachePrefix . '_' . $type . '_' . implode('_', $rules));
  118. return Cache::remember($cacheName, function () use ($rules, $type) {
  119. /** @var SystemMenusServices $menusService */
  120. $menusService = app()->make(SystemMenusServices::class);
  121. return $menusService->getColumn([['id', 'IN', $this->getRoleIds($rules)], ['auth_type', '=', $type]], 'api_url,methods');
  122. });
  123. }
  124. /**
  125. * 获取权限id
  126. * @param array $rules
  127. * @return array
  128. */
  129. public function getRoleIds(array $rules)
  130. {
  131. $rules = $this->dao->getColumn([['id', 'IN', $rules], ['status', '=', '1']], 'rules', 'id');
  132. return array_unique(explode(',', implode(',', $rules)));
  133. }
  134. }