exportPDF.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { TAPi18n } from '/imports/i18n';
  2. import { runOnServer } from './runOnServer';
  3. runOnServer(function() {
  4. // the ExporterCardPDF class is only available on server and in order to import
  5. // it here we use runOnServer to have it inside a function instead of an
  6. // if (Meteor.isServer) block
  7. import { ExporterCardPDF } from './server/ExporterCardPDF';
  8. // todo XXX once we have a real API in place, move that route there
  9. // todo XXX also share the route definition between the client and the server
  10. // so that we could use something like
  11. // `ApiRoutes.path('boards/exportExcel', boardId)``
  12. // on the client instead of copy/pasting the route path manually between the
  13. // client and the server.
  14. /**
  15. * @operation exportExcel
  16. * @tag Boards
  17. *
  18. * @summary This route is used to export the board Excel.
  19. *
  20. * @description If user is already logged-in, pass loginToken as param
  21. * "authToken": '/api/boards/:boardId/exportExcel?authToken=:token'
  22. *
  23. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  24. * for detailed explanations
  25. *
  26. * @param {string} boardId the ID of the board we are exporting
  27. * @param {string} authToken the loginToken
  28. */
  29. Picker.route('/api/boards/:boardId/lists/:listId/cards/:cardId/exportPDF', function (params, req, res) {
  30. const boardId = params.boardId;
  31. const paramListId = req.params.listId;
  32. const paramCardId = req.params.cardId;
  33. let user = null;
  34. let impersonateDone = false;
  35. let adminId = null;
  36. const loginToken = params.query.authToken;
  37. if (loginToken) {
  38. const hashToken = Accounts._hashLoginToken(loginToken);
  39. user = Meteor.users.findOne({
  40. 'services.resume.loginTokens.hashedToken': hashToken,
  41. });
  42. adminId = user._id.toString();
  43. impersonateDone = ImpersonatedUsers.findOne({
  44. adminId: adminId,
  45. });
  46. } else if (!Meteor.settings.public.sandstorm) {
  47. Authentication.checkUserId(req.userId);
  48. user = Users.findOne({
  49. _id: req.userId,
  50. isAdmin: true,
  51. });
  52. }
  53. const exporterCardPDF = new ExporterCardPDF(boardId);
  54. if (exporterCardPDF.canExport(user) || impersonateDone) {
  55. if (impersonateDone) {
  56. ImpersonatedUsers.insert({
  57. adminId: adminId,
  58. boardId: boardId,
  59. reason: 'exportCardPDF',
  60. });
  61. }
  62. exporterCardPDF.build(res);
  63. } else {
  64. res.end(TAPi18n.__('user-can-not-export-card-to-pdf'));
  65. }
  66. });
  67. });