PermissionService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if (!is_array($role->privileges)) {
  40. $privileges = json_decode($role->privileges, true);
  41. if (!is_array($privileges)) {
  42. return [];
  43. }
  44. } else {
  45. $privileges = $role->privileges;
  46. }
  47. // 转换为权限数组格式: controller.action
  48. $permissions = [];
  49. foreach ($privileges as $controller => $actions) {
  50. if (is_array($actions)) {
  51. foreach ($actions as $action) {
  52. $permissions[] = strtolower($controller . '.' . $action);
  53. }
  54. }
  55. }
  56. return $permissions;
  57. }
  58. /**
  59. * 获取所有权限配置
  60. */
  61. public static function getAllPermissions(): array
  62. {
  63. return Config::get('permission.permissions', []);
  64. }
  65. /**
  66. * 获取超级管理员角色ID列表
  67. */
  68. public static function getSuperAdminRoleIds(): array
  69. {
  70. return Config::get('permission.super_admin_role_ids', []);
  71. }
  72. /**
  73. * 检查是否是超级管理员
  74. */
  75. public static function isSuperAdmin(int $roleId): bool
  76. {
  77. return in_array($roleId, self::getSuperAdminRoleIds());
  78. }
  79. /**
  80. * 获取登录白名单
  81. */
  82. public static function getLoginWhitelist(): array
  83. {
  84. return Config::get('permission.login_whitelist', []);
  85. }
  86. /**
  87. * 获取权限白名单
  88. */
  89. public static function getPermissionWhitelist(): array
  90. {
  91. return Config::get('permission.permission_whitelist', []);
  92. }
  93. /**
  94. * 检查是否在登录白名单中
  95. */
  96. public static function isInLoginWhitelist(string $controller, string $action): bool
  97. {
  98. $whitelist = self::getLoginWhitelist();
  99. // 检查控制器级别白名单
  100. $controllers = $whitelist['controllers'] ?? [];
  101. if (in_array($controller, $controllers)) {
  102. return true;
  103. }
  104. // 检查具体方法白名单
  105. $actions = $whitelist['actions'] ?? [];
  106. $current = $controller . '/' . $action;
  107. if (in_array($current, $actions)) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * 检查是否在权限白名单中
  114. */
  115. public static function isInPermissionWhitelist(string $controller, string $action): bool
  116. {
  117. $whitelist = self::getPermissionWhitelist();
  118. // 检查控制器级别白名单
  119. $controllers = $whitelist['controllers'] ?? [];
  120. if (in_array($controller, $controllers)) {
  121. return true;
  122. }
  123. // 检查具体方法白名单
  124. $actions = $whitelist['actions'] ?? [];
  125. $current = $controller . '/' . $action;
  126. if (in_array($current, $actions)) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * 根据控制器名获取权限配置
  133. */
  134. public static function getControllerPermissions(string $controller): array
  135. {
  136. return Config::get('permission.permissions.' . $controller, []);
  137. }
  138. /**
  139. * 格式化权限列表用于前端展示
  140. */
  141. public static function formatPermissionsForDisplay(): array
  142. {
  143. $permissions = self::getAllPermissions();
  144. $formatted = [];
  145. foreach ($permissions as $controller => $config) {
  146. $formatted[] = [
  147. 'controller' => $controller,
  148. 'module' => $config['module'] ?? $controller,
  149. 'actions' => $config['actions'] ?? []
  150. ];
  151. }
  152. return $formatted;
  153. }
  154. }