sandstorm.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  2. // in the package definition.
  3. const isSandstorm = Meteor.settings && Meteor.settings.public &&
  4. Meteor.settings.public.sandstorm;
  5. if (isSandstorm && Meteor.isServer) {
  6. // In sandstorm we only have one board per sandstorm instance. Since we want
  7. // to keep most of our code unchanged, we simply hard-code a board `_id` and
  8. // redirect the user to this particular board.
  9. const sandstormBoard = {
  10. _id: 'sandstorm',
  11. // XXX Should be shared with the grain instance name.
  12. title: 'Wekan',
  13. slug: 'libreboard',
  14. members: [],
  15. // Board access security is handled by sandstorm, so in our point of view we
  16. // can alway assume that the board is public (unauthorized users won't be
  17. // able to access it anyway).
  18. permission: 'public',
  19. };
  20. function updateUserPermissions(userId, permissions) {
  21. const isActive = permissions.includes('participate');
  22. const isAdmin = permissions.includes('configure');
  23. const permissionDoc = { userId, isActive, isAdmin };
  24. const boardMembers = Boards.findOne(sandstormBoard._id).members;
  25. const memberIndex = _.pluck(boardMembers, 'userId').indexOf(userId);
  26. let modifier;
  27. if (memberIndex > -1)
  28. modifier = { $set: { [`members.${memberIndex}`]: permissionDoc }};
  29. else if (!isActive)
  30. modifier = {};
  31. else
  32. modifier = { $push: { members: permissionDoc }};
  33. Boards.update(sandstormBoard._id, modifier);
  34. }
  35. Picker.route('/', (params, req, res) => {
  36. // Redirect the user to the hard-coded board. On the first launch the user
  37. // will be redirected to the board before its creation. But that's not a
  38. // problem thanks to the reactive board publication. We used to do this
  39. // redirection on the client side but that was sometimes visible on loading,
  40. // and the home page was accessible by pressing the back button of the
  41. // browser, a server-side redirection solves both of these issues.
  42. //
  43. // XXX Maybe the sandstorm http-bridge could provide some kind of "home URL"
  44. // in the manifest?
  45. const base = req.headers['x-sandstorm-base-path'];
  46. // XXX If this routing scheme changes, this will break. We should generate
  47. // the location URL using the router, but at the time of writing, the
  48. // it is only accessible on the client.
  49. const path = `/boards/${sandstormBoard._id}/${sandstormBoard.slug}`;
  50. res.writeHead(301, {
  51. Location: base + path,
  52. });
  53. res.end();
  54. // `accounts-sandstorm` populate the Users collection when new users
  55. // accesses the document, but in case a already known user comes back, we
  56. // need to update his associated document to match the request HTTP headers
  57. // informations.
  58. const user = Users.findOne({
  59. 'services.sandstorm.id': req.headers['x-sandstorm-user-id'],
  60. });
  61. if (user) {
  62. updateUserPermissions(user._id, user.permissions);
  63. }
  64. });
  65. // On the first launch of the instance a user is automatically created thanks
  66. // to the `accounts-sandstorm` package. After its creation we insert the
  67. // unique board document. Note that when the `Users.after.insert` hook is
  68. // called, the user is inserted into the database but not connected. So
  69. // despite the appearances `userId` is null in this block.
  70. //
  71. // XXX We should support the `preferredHandle` exposed by Sandstorm
  72. Users.after.insert((userId, doc) => {
  73. if (!Boards.findOne(sandstormBoard._id)) {
  74. Boards.insert(sandstormBoard, {validate: false});
  75. Activities.update(
  76. { activityTypeId: sandstormBoard._id },
  77. { $set: { userId: doc._id }}
  78. );
  79. }
  80. updateUserPermissions(doc._id, doc.services.sandstorm.permissions);
  81. });
  82. // LibreBoard v0.8 didn’t implement the Sandstorm sharing model and instead
  83. // kept the visibility setting (“public” or “private”) in the UI as does the
  84. // main Meteor application. We need to enforce “public” visibility as the
  85. // sharing is now handled by Sandstorm.
  86. // See https://github.com/wekan/wekan/issues/346
  87. Migrations.add('enforce-public-visibility-for-sandstorm', () => {
  88. Boards.update('sandstorm', { $set: { permission: 'public' }});
  89. });
  90. }
  91. if (isSandstorm && Meteor.isClient) {
  92. // XXX Hack. `Meteor.absoluteUrl` doesn't work in Sandstorm, since every
  93. // session has a different URL whereas Meteor computes absoluteUrl based on
  94. // the ROOT_URL environment variable. So we overwrite this function on a
  95. // sandstorm client to return relative paths instead of absolutes.
  96. const _absoluteUrl = Meteor.absoluteUrl;
  97. const _defaultOptions = Meteor.absoluteUrl.defaultOptions;
  98. Meteor.absoluteUrl = (path, options) => { // eslint-disable-line meteor/core
  99. const url = _absoluteUrl(path, options);
  100. return url.replace(/^https?:\/\/127\.0\.0\.1:[0-9]{2,5}/, '');
  101. };
  102. Meteor.absoluteUrl.defaultOptions = _defaultOptions;
  103. }
  104. // We use this blaze helper in the UI to hide some templates that does not make
  105. // sense in the context of sandstorm, like board staring, board archiving, user
  106. // name edition, etc.
  107. Blaze.registerHelper('isSandstorm', isSandstorm);