exportExcel.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { runOnServer } from './runOnServer';
  4. runOnServer(function() {
  5. // the ExporterExcel 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 { ExporterExcel } from './server/ExporterExcel';
  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/exportExcel', function (params, req, res) {
  32. const boardId = params.boardId;
  33. let user = null;
  34. let impersonateDone = false;
  35. let adminId = null;
  36. // First check if board exists and is public to avoid unnecessary authentication
  37. const board = ReactiveCache.getBoard(boardId);
  38. if (!board) {
  39. res.end('Board not found');
  40. return;
  41. }
  42. // If board is public, skip expensive authentication operations
  43. if (board.isPublic()) {
  44. // Public boards don't require authentication - skip hash operations
  45. const exporterExcel = new ExporterExcel(boardId, 'en');
  46. exporterExcel.build(res);
  47. return;
  48. }
  49. // Only perform expensive authentication for private boards
  50. const loginToken = params.query.authToken;
  51. if (loginToken) {
  52. // Validate token length to prevent resource abuse
  53. if (loginToken.length > 10000) {
  54. if (process.env.DEBUG === 'true') {
  55. console.warn('Suspiciously long auth token received, rejecting to prevent resource abuse');
  56. }
  57. res.end('Invalid token');
  58. return;
  59. }
  60. const hashToken = Accounts._hashLoginToken(loginToken);
  61. user = ReactiveCache.getUser({
  62. 'services.resume.loginTokens.hashedToken': hashToken,
  63. });
  64. adminId = user._id.toString();
  65. impersonateDone = ReactiveCache.getImpersonatedUser({ adminId: adminId });
  66. } else if (!Meteor.settings.public.sandstorm) {
  67. Authentication.checkUserId(req.userId);
  68. user = ReactiveCache.getUser({
  69. _id: req.userId,
  70. isAdmin: true,
  71. });
  72. }
  73. let userLanguage = 'en';
  74. if(user && user.profile){
  75. userLanguage = user.profile.language
  76. }
  77. const exporterExcel = new ExporterExcel(boardId, userLanguage);
  78. if (exporterExcel.canExport(user) || impersonateDone) {
  79. if (impersonateDone) {
  80. ImpersonatedUsers.insert({
  81. adminId: adminId,
  82. boardId: boardId,
  83. reason: 'exportExcel',
  84. });
  85. }
  86. exporterExcel.build(res);
  87. } else {
  88. res.end(TAPi18n.__('user-can-not-export-excel'));
  89. }
  90. });
  91. });