SystemRoleServices.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Request;
  13. use qiniu\basic\BaseServices;
  14. use app\model\system\admin\SystemRole;
  15. use qiniu\exceptions\AdminException;
  16. use qiniu\exceptions\AuthException;
  17. use qiniu\utils\ApiErrorCode;
  18. use qiniu\services\CacheService;
  19. use think\db\exception\DbException;
  20. /**
  21. * Class SystemRoleServices
  22. * @package app\services\system
  23. * @mixin SystemRole
  24. */
  25. class SystemRoleServices extends BaseServices
  26. {
  27. /**
  28. * 当前管理员权限缓存前缀
  29. */
  30. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  31. /**
  32. * SystemRoleServices constructor.
  33. * @param SystemRole $model
  34. */
  35. public function __construct(SystemRole $model)
  36. {
  37. $this->model = $model;
  38. }
  39. /**
  40. * 获取权限
  41. * @return mixed
  42. */
  43. public function getRoleArray(array $where = [], string $field = '', string $key = '')
  44. {
  45. return $this->search($where)->column($field ?: 'role_name', $key ?: 'id');
  46. }
  47. /**
  48. * 身份管理列表
  49. * @param array $where
  50. * @return array
  51. * @throws DbException
  52. */
  53. public function getRoleList(array $where)
  54. {
  55. [$page, $limit] = $this->getPageValue();
  56. $list = $this->getList($where, '*', $page, $limit);
  57. $count = $this->getCount($where);
  58. /** @var SystemMenusServices $service */
  59. $service = app()->make(SystemMenusServices::class);
  60. foreach ($list as &$item) {
  61. $item['rules'] = implode(',', array_merge($service->search(['id' => $item['rules']])->column('menu_name', 'id')));
  62. }
  63. return compact('count', 'list');
  64. }
  65. /**
  66. * 后台验证权限
  67. * @param Request $request
  68. * @return bool
  69. * @throws \Throwable
  70. */
  71. public function verifiAuth(Request $request)
  72. {
  73. $rule = str_replace('adminapi/', '', trim(strtolower($request->rule()->getRule())));
  74. if (in_array($rule, ['logout', 'menuslist'])) {
  75. return true;
  76. }
  77. $method = trim(strtolower($request->method()));
  78. $auth = $this->getAllRoles(2);
  79. //验证访问接口是否存在
  80. if (!in_array($method . '@@' . $rule, array_map(function ($item) {
  81. return trim(strtolower($item['methods'])) . '@@' . trim(strtolower(str_replace(' ', '', $item['api_url'])));
  82. }, $auth))) {
  83. return true;
  84. }
  85. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  86. //验证访问接口是否有权限
  87. if ($auth && empty(array_filter($auth, function ($item) use ($rule, $method) {
  88. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  89. return true;
  90. else
  91. return false;
  92. }))) {
  93. throw new AuthException(ApiErrorCode::ERR_AUTH);
  94. }
  95. return true;
  96. }
  97. /**
  98. * 获取所有权限
  99. * @param int $auth_type
  100. * @param int $type
  101. * @param string $cachePrefix
  102. * @return array|bool|mixed|null
  103. * @throws \Throwable
  104. */
  105. public function getAllRoles(int $auth_type = 1, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  106. {
  107. $cacheName = md5($cachePrefix . '_' . $auth_type . '_' . $type . '_ALl');
  108. return CacheService::redisHandler('system_menus')->remember($cacheName, function () use ($auth_type, $type) {
  109. /** @var SystemMenusServices $menusService */
  110. $menusService = app()->make(SystemMenusServices::class);
  111. return $menusService->getColumn([['auth_type', '=', $auth_type], ['type', '=', $type]], 'api_url,methods');
  112. });
  113. }
  114. /**
  115. * 获取指定权限
  116. * @param array $roles
  117. * @param int $auth_type
  118. * @param int $type
  119. * @param string $cachePrefix
  120. * @return array|bool|mixed|null
  121. * @throws \Throwable
  122. */
  123. public function getRolesByAuth(array $roles, int $auth_type = 1, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  124. {
  125. if (empty($roles)) return [];
  126. $cacheName = md5($cachePrefix . '_' . $auth_type . '_' . $type . '_' . implode('_', $roles));
  127. CacheService::redisHandler('system_menus')->clear();
  128. return CacheService::redisHandler('system_menus')->remember($cacheName, function () use ($roles, $auth_type, $type) {
  129. /** @var SystemMenusServices $menusService */
  130. $menusService = app()->make(SystemMenusServices::class);
  131. return $menusService->getColumn([['id', 'IN', $this->getRoleIds($roles)], ['auth_type', '=', $auth_type], ['type', '=', $type]], 'api_url,methods');
  132. });
  133. }
  134. /**
  135. * 获取权限id
  136. * @param array $roles
  137. * @param string $field
  138. * @param string $key
  139. * @return array
  140. */
  141. public function getRoleIds(array $roles, string $field = 'rules', string $key = 'id')
  142. {
  143. $rules = $this->model->getColumn([['id', 'IN', $roles], ['status', '=', '1']], $field, $key);
  144. return $rules ? array_unique(explode(',', implode(',', $rules))) : [];
  145. }
  146. public function create($data)
  147. {
  148. if (parent::create($data)) {
  149. CacheService::clear();
  150. return true;
  151. } else {
  152. throw new AdminException('添加失败');
  153. }
  154. }
  155. public function update($id, array $data, ?string $key = null)
  156. {
  157. if (parent::update($id, $data)) {
  158. CacheService::clear();
  159. return true;
  160. } else {
  161. throw new AdminException('修改失败');
  162. }
  163. }
  164. public function delete($id, ?string $key = null)
  165. {
  166. if (parent::delete($id)) {
  167. CacheService::clear();
  168. return true;
  169. } else {
  170. throw new AdminException('删除失败,请稍候再试!');
  171. }
  172. }
  173. }