exportPDF.js 2.5 KB

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