exportExcel.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { runOnServer } from './runOnServer';
  2. runOnServer(function() {
  3. // the ExporterExcel 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 { ExporterExcel } from './server/ExporterExcel';
  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/exportExcel', function (params, req, res) {
  29. const boardId = params.boardId;
  30. let user = null;
  31. let impersonateDone = false;
  32. let adminId = null;
  33. const loginToken = params.query.authToken;
  34. if (loginToken) {
  35. const hashToken = Accounts._hashLoginToken(loginToken);
  36. user = Meteor.users.findOne({
  37. 'services.resume.loginTokens.hashedToken': hashToken,
  38. });
  39. adminId = user._id.toString();
  40. impersonateDone = ImpersonatedUsers.findOne({
  41. adminId: adminId,
  42. });
  43. } else if (!Meteor.settings.public.sandstorm) {
  44. Authentication.checkUserId(req.userId);
  45. user = Users.findOne({
  46. _id: req.userId,
  47. isAdmin: true,
  48. });
  49. }
  50. const exporterExcel = new ExporterExcel(boardId);
  51. if (exporterExcel.canExport(user) || impersonateDone) {
  52. if (impersonateDone) {
  53. ImpersonatedUsers.insert({
  54. adminId: adminId,
  55. boardId: boardId,
  56. reason: 'exportExcel',
  57. });
  58. }
  59. exporterExcel.build(res);
  60. } else {
  61. res.end(TAPi18n.__('user-can-not-export-excel'));
  62. }
  63. });
  64. });