PermissionService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\service;
  4. use app\model\UserRoleModel;
  5. use think\facade\Config;
  6. class PermissionService
  7. {
  8. /**
  9. * 检查用户权限
  10. */
  11. public static function checkPermission(int $userId, string $controller, string $action): bool
  12. {
  13. // 检查是否在白名单中
  14. if (self::isInPermissionWhitelist($controller, $action)) {
  15. return true;
  16. }
  17. $roleId = getUserRoleId($userId);
  18. if (!$roleId) {
  19. return false;
  20. }
  21. // 超级管理员拥有所有权限
  22. if (self::isSuperAdmin($roleId)) {
  23. return true;
  24. }
  25. // 获取用户权限并检查
  26. $permissions = self::getUserPermissions($roleId);
  27. $permission = strtolower($controller . '.' . $action);
  28. return in_array($permission, $permissions);
  29. }
  30. /**
  31. * 获取用户权限列表
  32. */
  33. public static function getUserPermissions(int $roleId): array
  34. {
  35. $role = UserRoleModel::find($roleId);
  36. if (!$role || empty($role->privileges)) {
  37. return [];
  38. }
  39. $privileges = json_decode($role->privileges, true);
  40. if (!is_array($privileges)) {
  41. return [];
  42. }
  43. // 转换为权限数组格式: controller.action
  44. $permissions = [];
  45. foreach ($privileges as $controller => $actions) {
  46. if (is_array($actions)) {
  47. foreach ($actions as $action) {
  48. $permissions[] = strtolower($controller . '.' . $action);
  49. }
  50. }
  51. }
  52. return $permissions;
  53. }
  54. /**
  55. * 获取所有权限配置
  56. */
  57. public static function getAllPermissions(): array
  58. {
  59. return Config::get('permission.permissions', []);
  60. }
  61. /**
  62. * 获取超级管理员角色ID列表
  63. */
  64. public static function getSuperAdminRoleIds(): array
  65. {
  66. return Config::get('permission.super_admin_role_ids', []);
  67. }
  68. /**
  69. * 检查是否是超级管理员
  70. */
  71. public static function isSuperAdmin(int $roleId): bool
  72. {
  73. return in_array($roleId, self::getSuperAdminRoleIds());
  74. }
  75. /**
  76. * 获取登录白名单
  77. */
  78. public static function getLoginWhitelist(): array
  79. {
  80. return Config::get('permission.login_whitelist', []);
  81. }
  82. /**
  83. * 获取权限白名单
  84. */
  85. public static function getPermissionWhitelist(): array
  86. {
  87. return Config::get('permission.permission_whitelist', []);
  88. }
  89. /**
  90. * 检查是否在登录白名单中
  91. */
  92. public static function isInLoginWhitelist(string $controller, string $action): bool
  93. {
  94. $whitelist = self::getLoginWhitelist();
  95. // 检查控制器级别白名单
  96. $controllers = $whitelist['controllers'] ?? [];
  97. if (in_array($controller, $controllers)) {
  98. return true;
  99. }
  100. // 检查具体方法白名单
  101. $actions = $whitelist['actions'] ?? [];
  102. $current = $controller . '/' . $action;
  103. if (in_array($current, $actions)) {
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * 检查是否在权限白名单中
  110. */
  111. public static function isInPermissionWhitelist(string $controller, string $action): bool
  112. {
  113. $whitelist = self::getPermissionWhitelist();
  114. // 检查控制器级别白名单
  115. $controllers = $whitelist['controllers'] ?? [];
  116. if (in_array($controller, $controllers)) {
  117. return true;
  118. }
  119. // 检查具体方法白名单
  120. $actions = $whitelist['actions'] ?? [];
  121. $current = $controller . '/' . $action;
  122. if (in_array($current, $actions)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * 根据控制器名获取权限配置
  129. */
  130. public static function getControllerPermissions(string $controller): array
  131. {
  132. return Config::get('permission.permissions.' . $controller, []);
  133. }
  134. /**
  135. * 格式化权限列表用于前端展示
  136. */
  137. public static function formatPermissionsForDisplay(): array
  138. {
  139. $permissions = self::getAllPermissions();
  140. $formatted = [];
  141. foreach ($permissions as $controller => $config) {
  142. $formatted[] = [
  143. 'controller' => $controller,
  144. 'module' => $config['module'] ?? $controller,
  145. 'actions' => $config['actions'] ?? []
  146. ];
  147. }
  148. return $formatted;
  149. }
  150. }