permissionController.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.PermissionController = void 0;
  4. const errorHandler_1 = require("../middleware/errorHandler");
  5. const permission_1 = require("../models/permission");
  6. const helpers_1 = require("../utils/helpers");
  7. class PermissionController {
  8. static async getAllPages(req, res) {
  9. try {
  10. const currentUser = req.user;
  11. if (!currentUser || currentUser.role !== 'admin') {
  12. throw new errorHandler_1.AppError('权限不足,只有管理员可以访问', 403);
  13. }
  14. const pages = await permission_1.PermissionModel.getAllPages();
  15. res.status(200).json({
  16. success: true,
  17. message: '获取页面列表成功',
  18. data: pages
  19. });
  20. }
  21. catch (error) {
  22. throw error;
  23. }
  24. }
  25. static async getUserPermissions(req, res) {
  26. try {
  27. const currentUser = req.user;
  28. if (!currentUser || currentUser.role !== 'admin') {
  29. throw new errorHandler_1.AppError('权限不足,只有管理员可以访问', 403);
  30. }
  31. const userId = (0, helpers_1.toString)(req.params.userId);
  32. if (!userId) {
  33. throw new errorHandler_1.AppError('用户ID不能为空', 400);
  34. }
  35. const permissions = await permission_1.PermissionModel.getUserPermissions(userId);
  36. res.status(200).json({
  37. success: true,
  38. message: '获取用户权限列表成功',
  39. data: permissions
  40. });
  41. }
  42. catch (error) {
  43. throw error;
  44. }
  45. }
  46. static async assignPermission(req, res) {
  47. try {
  48. const currentUser = req.user;
  49. if (!currentUser || currentUser.role !== 'admin') {
  50. throw new errorHandler_1.AppError('权限不足,只有管理员可以分配权限', 403);
  51. }
  52. const userId = (0, helpers_1.toString)(req.params.userId);
  53. const { pageId } = req.body;
  54. if (!userId || !pageId) {
  55. throw new errorHandler_1.AppError('用户ID和页面ID不能为空', 400);
  56. }
  57. const permission = await permission_1.PermissionModel.assignPermission(userId, pageId);
  58. res.status(201).json({
  59. success: true,
  60. message: '权限分配成功',
  61. data: permission
  62. });
  63. }
  64. catch (error) {
  65. throw error;
  66. }
  67. }
  68. static async assignPermissions(req, res) {
  69. try {
  70. const currentUser = req.user;
  71. if (!currentUser || currentUser.role !== 'admin') {
  72. throw new errorHandler_1.AppError('权限不足,只有管理员可以分配权限', 403);
  73. }
  74. const userId = (0, helpers_1.toString)(req.params.userId);
  75. const { pageIds } = req.body;
  76. if (!userId || !Array.isArray(pageIds)) {
  77. throw new errorHandler_1.AppError('用户ID和页面ID列表不能为空', 400);
  78. }
  79. await permission_1.PermissionModel.assignPermissions(userId, pageIds);
  80. res.status(200).json({
  81. success: true,
  82. message: '权限分配成功'
  83. });
  84. }
  85. catch (error) {
  86. throw error;
  87. }
  88. }
  89. static async removePermission(req, res) {
  90. try {
  91. const currentUser = req.user;
  92. if (!currentUser || currentUser.role !== 'admin') {
  93. throw new errorHandler_1.AppError('权限不足,只有管理员可以移除权限', 403);
  94. }
  95. const userId = (0, helpers_1.toString)(req.params.userId);
  96. const pageId = (0, helpers_1.toString)(req.params.pageId);
  97. if (!userId || !pageId) {
  98. throw new errorHandler_1.AppError('用户ID和页面ID不能为空', 400);
  99. }
  100. const success = await permission_1.PermissionModel.removePermission(userId, parseInt(pageId));
  101. if (!success) {
  102. throw new errorHandler_1.AppError('移除权限失败,权限不存在', 404);
  103. }
  104. res.status(200).json({
  105. success: true,
  106. message: '权限移除成功'
  107. });
  108. }
  109. catch (error) {
  110. throw error;
  111. }
  112. }
  113. static async checkPermission(req, res) {
  114. try {
  115. const userId = (0, helpers_1.toString)(req.params.userId);
  116. const pagePath = (0, helpers_1.toString)(req.params.pagePath);
  117. if (!userId || !pagePath) {
  118. throw new errorHandler_1.AppError('用户ID和页面路径不能为空', 400);
  119. }
  120. const hasPermission = await permission_1.PermissionModel.checkUserPermission(userId, pagePath);
  121. res.status(200).json({
  122. success: true,
  123. message: '权限检查成功',
  124. data: {
  125. hasPermission
  126. }
  127. });
  128. }
  129. catch (error) {
  130. throw error;
  131. }
  132. }
  133. }
  134. exports.PermissionController = PermissionController;
  135. //# sourceMappingURL=permissionController.js.map