export.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { Exporter } from './exporter';
  2. import { Meteor } from 'meteor/meteor';
  3. /* global JsonRoutes */
  4. if (Meteor.isServer) {
  5. import { Picker } from 'meteor/communitypackages:picker';
  6. // todo XXX once we have a real API in place, move that route there
  7. // todo XXX also share the route definition between the client and the server
  8. // so that we could use something like
  9. // `ApiRoutes.path('boards/export', boardId)``
  10. // on the client instead of copy/pasting the route path manually between the
  11. // client and the server.
  12. /**
  13. * @operation exportJson
  14. * @tag Boards
  15. *
  16. * @summary This route is used to export the board to a json file format.
  17. *
  18. * @description If user is already logged-in, pass loginToken as param
  19. * "authToken": '/api/boards/:boardId/export?authToken=:token'
  20. *
  21. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  22. * for detailed explanations
  23. *
  24. * @param {string} boardId the ID of the board we are exporting
  25. * @param {string} authToken the loginToken
  26. */
  27. JsonRoutes.add('get', '/api/boards/:boardId/export', function (req, res) {
  28. const boardId = req.params.boardId;
  29. let user = null;
  30. let impersonateDone = false;
  31. let adminId = null;
  32. const loginToken = req.query.authToken;
  33. if (loginToken) {
  34. const hashToken = Accounts._hashLoginToken(loginToken);
  35. user = Meteor.users.findOne({
  36. 'services.resume.loginTokens.hashedToken': hashToken,
  37. });
  38. adminId = user._id.toString();
  39. impersonateDone = ImpersonatedUsers.findOne({
  40. adminId: adminId,
  41. });
  42. } else if (!Meteor.settings.public.sandstorm) {
  43. Authentication.checkUserId(req.userId);
  44. user = Users.findOne({ _id: req.userId, isAdmin: true });
  45. }
  46. const exporter = new Exporter(boardId);
  47. if (exporter.canExport(user) || impersonateDone) {
  48. if (impersonateDone) {
  49. ImpersonatedUsers.insert({
  50. adminId: adminId,
  51. boardId: boardId,
  52. reason: 'exportJSON',
  53. });
  54. }
  55. JsonRoutes.sendResult(res, {
  56. code: 200,
  57. data: exporter.build(),
  58. });
  59. } else {
  60. // we could send an explicit error message, but on the other hand the only
  61. // way to get there is by hacking the UI so let's keep it raw.
  62. JsonRoutes.sendResult(res, 403);
  63. }
  64. });
  65. // todo XXX once we have a real API in place, move that route there
  66. // todo XXX also share the route definition between the client and the server
  67. // so that we could use something like
  68. // `ApiRoutes.path('boards/export', boardId)``
  69. // on the client instead of copy/pasting the route path manually between the
  70. // client and the server.
  71. /**
  72. * @operation exportJson
  73. * @tag Boards
  74. *
  75. * @summary This route is used to export a attachement to a json file format.
  76. *
  77. * @description If user is already logged-in, pass loginToken as param
  78. * "authToken": '/api/boards/:boardId/attachments/:attachmentId/export?authToken=:token'
  79. *
  80. *
  81. * @param {string} boardId the ID of the board we are exporting
  82. * @param {string} attachmentId the ID of the attachment we are exporting
  83. * @param {string} authToken the loginToken
  84. */
  85. JsonRoutes.add(
  86. 'get',
  87. '/api/boards/:boardId/attachments/:attachmentId/export',
  88. function (req, res) {
  89. const boardId = req.params.boardId;
  90. const attachmentId = req.params.attachmentId;
  91. let user = null;
  92. let impersonateDone = false;
  93. let adminId = null;
  94. const loginToken = req.query.authToken;
  95. if (loginToken) {
  96. const hashToken = Accounts._hashLoginToken(loginToken);
  97. user = Meteor.users.findOne({
  98. 'services.resume.loginTokens.hashedToken': hashToken,
  99. });
  100. adminId = user._id.toString();
  101. impersonateDone = ImpersonatedUsers.findOne({
  102. adminId: adminId,
  103. });
  104. } else if (!Meteor.settings.public.sandstorm) {
  105. Authentication.checkUserId(req.userId);
  106. user = Users.findOne({ _id: req.userId, isAdmin: true });
  107. }
  108. const exporter = new Exporter(boardId, attachmentId);
  109. if (exporter.canExport(user) || impersonateDone) {
  110. if (impersonateDone) {
  111. ImpersonatedUsers.insert({
  112. adminId: adminId,
  113. boardId: boardId,
  114. attachmentId: attachmentId,
  115. reason: 'exportJSONattachment',
  116. });
  117. }
  118. JsonRoutes.sendResult(res, {
  119. code: 200,
  120. data: exporter.build(),
  121. });
  122. } else {
  123. // we could send an explicit error message, but on the other hand the only
  124. // way to get there is by hacking the UI so let's keep it raw.
  125. JsonRoutes.sendResult(res, 403);
  126. }
  127. },
  128. );
  129. /**
  130. * @operation exportCSV/TSV
  131. * @tag Boards
  132. *
  133. * @summary This route is used to export the board to a CSV or TSV file format.
  134. *
  135. * @description If user is already logged-in, pass loginToken as param
  136. *
  137. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  138. * for detailed explanations
  139. *
  140. * @param {string} boardId the ID of the board we are exporting
  141. * @param {string} authToken the loginToken
  142. * @param {string} delimiter delimiter to use while building export. Default is comma ','
  143. */
  144. Picker.route('/api/boards/:boardId/export/csv', function (params, req, res) {
  145. const boardId = params.boardId;
  146. let user = null;
  147. let impersonateDone = false;
  148. let adminId = null;
  149. const loginToken = params.query.authToken;
  150. if (loginToken) {
  151. const hashToken = Accounts._hashLoginToken(loginToken);
  152. user = Meteor.users.findOne({
  153. 'services.resume.loginTokens.hashedToken': hashToken,
  154. });
  155. adminId = user._id.toString();
  156. impersonateDone = ImpersonatedUsers.findOne({
  157. adminId: adminId,
  158. });
  159. } else if (!Meteor.settings.public.sandstorm) {
  160. Authentication.checkUserId(req.userId);
  161. user = Users.findOne({
  162. _id: req.userId,
  163. isAdmin: true,
  164. });
  165. }
  166. const exporter = new Exporter(boardId);
  167. if (exporter.canExport(user) || impersonateDone) {
  168. if (impersonateDone) {
  169. let exportType = 'exportCSV';
  170. if( params.query.delimiter == "\t" ) {
  171. exportType = 'exportTSV';
  172. }
  173. ImpersonatedUsers.insert({
  174. adminId: adminId,
  175. boardId: boardId,
  176. reason: exportType,
  177. });
  178. }
  179. let userLanguage = 'en';
  180. if (user && user.profile) {
  181. userLanguage = user.profile.language
  182. }
  183. if( params.query.delimiter == "\t" ) {
  184. // TSV file
  185. res.writeHead(200, {
  186. 'Content-Type': 'text/tsv',
  187. });
  188. }
  189. else {
  190. // CSV file (comma or semicolon)
  191. res.writeHead(200, {
  192. 'Content-Type': 'text/csv; charset=utf-8',
  193. });
  194. // Adding UTF8 BOM to quick fix MS Excel issue
  195. // use Uint8Array to prevent from converting bytes to string
  196. res.write(new Uint8Array([0xEF, 0xBB, 0xBF]));
  197. }
  198. res.write(exporter.buildCsv(params.query.delimiter, userLanguage));
  199. res.end();
  200. } else {
  201. res.writeHead(403);
  202. res.end('Permission Error');
  203. }
  204. });
  205. }