exportPDF.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { runOnServer } from './runOnServer';
  4. runOnServer(function() {
  5. // the ExporterCardPDF class is only available on server and in order to import
  6. // it here we use runOnServer to have it inside a function instead of an
  7. // if (Meteor.isServer) block
  8. import { ExporterCardPDF } from './server/ExporterCardPDF';
  9. import { Picker } from 'meteor/communitypackages:picker';
  10. // todo XXX once we have a real API in place, move that route there
  11. // todo XXX also share the route definition between the client and the server
  12. // so that we could use something like
  13. // `ApiRoutes.path('boards/exportExcel', boardId)``
  14. // on the client instead of copy/pasting the route path manually between the
  15. // client and the server.
  16. /**
  17. * @operation exportExcel
  18. * @tag Boards
  19. *
  20. * @summary This route is used to export the board Excel.
  21. *
  22. * @description If user is already logged-in, pass loginToken as param
  23. * "authToken": '/api/boards/:boardId/exportExcel?authToken=:token'
  24. *
  25. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  26. * for detailed explanations
  27. *
  28. * @param {string} boardId the ID of the board we are exporting
  29. * @param {string} authToken the loginToken
  30. */
  31. Picker.route('/api/boards/:boardId/lists/:listId/cards/:cardId/exportPDF', function (params, req, res) {
  32. const boardId = params.boardId;
  33. const paramListId = req.params.listId;
  34. const paramCardId = req.params.cardId;
  35. let user = null;
  36. let impersonateDone = false;
  37. let adminId = null;
  38. // First check if board exists and is public to avoid unnecessary authentication
  39. const board = ReactiveCache.getBoard(boardId);
  40. if (!board) {
  41. res.end('Board not found');
  42. return;
  43. }
  44. // If board is public, skip expensive authentication operations
  45. if (board.isPublic()) {
  46. // Public boards don't require authentication - skip hash operations
  47. const exporterCardPDF = new ExporterCardPDF(boardId);
  48. exporterCardPDF.build(res);
  49. return;
  50. }
  51. // Only perform expensive authentication for private boards
  52. const loginToken = params.query.authToken;
  53. if (loginToken) {
  54. // Validate token length to prevent resource abuse
  55. if (loginToken.length > 10000) {
  56. if (process.env.DEBUG === 'true') {
  57. console.warn('Suspiciously long auth token received, rejecting to prevent resource abuse');
  58. }
  59. res.end('Invalid token');
  60. return;
  61. }
  62. const hashToken = Accounts._hashLoginToken(loginToken);
  63. user = ReactiveCache.getUser({
  64. 'services.resume.loginTokens.hashedToken': hashToken,
  65. });
  66. adminId = user._id.toString();
  67. impersonateDone = ReactiveCache.getImpersonatedUser({ adminId: adminId });
  68. } else if (!Meteor.settings.public.sandstorm) {
  69. Authentication.checkUserId(req.userId);
  70. user = ReactiveCache.getUser({
  71. _id: req.userId,
  72. isAdmin: true,
  73. });
  74. }
  75. const exporterCardPDF = new ExporterCardPDF(boardId);
  76. if (exporterCardPDF.canExport(user) || impersonateDone) {
  77. if (impersonateDone) {
  78. ImpersonatedUsers.insert({
  79. adminId: adminId,
  80. boardId: boardId,
  81. reason: 'exportCardPDF',
  82. });
  83. }
  84. exporterCardPDF.build(res);
  85. } else {
  86. res.end(TAPi18n.__('user-can-not-export-card-to-pdf'));
  87. }
  88. });
  89. });