exportPDF.js 2.4 KB

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