export.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* global JsonRoutes */
  2. if(Meteor.isServer) {
  3. // todo XXX once we have a real API in place, move that route there
  4. // todo XXX also share the route definition between the client and the server
  5. // so that we could use something like ApiRoutes.path('boards/export', boardId)
  6. // on the client instead of copy/pasting the route path manually between the client and the server.
  7. /*
  8. * This route is used to export the board FROM THE APPLICATION.
  9. * If user is already logged-in, pass loginToken as param "authToken":
  10. * '/api/boards/:boardId?authToken=:token'
  11. *
  12. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  13. * for detailed explanations
  14. */
  15. JsonRoutes.add('get', '/api/boards/:boardId', function (req, res) {
  16. const boardId = req.params.boardId;
  17. let user = null;
  18. // todo XXX for real API, first look for token in Authentication: header
  19. // then fallback to parameter
  20. const loginToken = req.query.authToken;
  21. if (loginToken) {
  22. const hashToken = Accounts._hashLoginToken(loginToken);
  23. user = Meteor.users.findOne({
  24. 'services.resume.loginTokens.hashedToken': hashToken,
  25. });
  26. }
  27. const exporter = new Exporter(boardId);
  28. if(exporter.canExport(user)) {
  29. JsonRoutes.sendResult(res, 200, exporter.build());
  30. } else {
  31. // we could send an explicit error message, but on the other hand the only way to
  32. // get there is by hacking the UI so let's keep it raw.
  33. JsonRoutes.sendResult(res, 403);
  34. }
  35. });
  36. }
  37. class Exporter {
  38. constructor(boardId) {
  39. this._boardId = boardId;
  40. }
  41. build() {
  42. const byBoard = {boardId: this._boardId};
  43. // we do not want to retrieve boardId in related elements
  44. const noBoardId = {fields: {boardId: 0}};
  45. const result = {
  46. _format: 'wekan-board-1.0.0',
  47. };
  48. _.extend(result, Boards.findOne(this._boardId, {fields: {stars: 0}}));
  49. result.lists = Lists.find(byBoard, noBoardId).fetch();
  50. result.cards = Cards.find(byBoard, noBoardId).fetch();
  51. result.comments = CardComments.find(byBoard, noBoardId).fetch();
  52. result.activities = Activities.find(byBoard, noBoardId).fetch();
  53. // for attachments we only export IDs and absolute url to original doc
  54. result.attachments = Attachments.find(byBoard).fetch().map((attachment) => { return {
  55. _id: attachment._id,
  56. cardId: attachment.cardId,
  57. url: Meteor.absoluteUrl(Utils.stripLeadingSlash(attachment.url())),
  58. };});
  59. // we also have to export some user data - as the other elements only include id
  60. // but we have to be careful:
  61. // 1- only exports users that are linked somehow to that board
  62. // 2- do not export any sensitive information
  63. const users = {};
  64. result.members.forEach((member) => {users[member.userId] = true;});
  65. result.lists.forEach((list) => {users[list.userId] = true;});
  66. result.cards.forEach((card) => {
  67. users[card.userId] = true;
  68. if (card.members) {
  69. card.members.forEach((memberId) => {users[memberId] = true;});
  70. }
  71. });
  72. result.comments.forEach((comment) => {users[comment.userId] = true;});
  73. result.activities.forEach((activity) => {users[activity.userId] = true;});
  74. const byUserIds = {_id: {$in: Object.getOwnPropertyNames(users)}};
  75. // we use whitelist to be sure we do not expose inadvertently
  76. // some secret fields that gets added to User later.
  77. const userFields = {fields: {
  78. _id: 1,
  79. username: 1,
  80. 'profile.fullname': 1,
  81. 'profile.initials': 1,
  82. 'profile.avatarUrl': 1,
  83. }};
  84. result.users = Users.find(byUserIds, userFields).fetch().map((user) => {
  85. // user avatar is stored as a relative url, we export absolute
  86. if(user.profile.avatarUrl) {
  87. user.profile.avatarUrl = Meteor.absoluteUrl(Utils.stripLeadingSlash(user.profile.avatarUrl));
  88. }
  89. return user;
  90. });
  91. return result;
  92. }
  93. canExport(user) {
  94. const board = Boards.findOne(this._boardId);
  95. return board && board.isVisibleBy(user);
  96. }
  97. }