Search code examples
apostrophe-cms

How to obtain restricted page URL when anon


The title says it all: in the site which we are currently building, we have pages that have permissions for admin, guest, and anon.

One of the widgets we created allow for an admin to select any page, and then it generates a link in the page. With apostrophe-second-chance-login, this should work fine.

[...]
{
  label: 'Page, article, product, etc.',
  help: 'Content to which the button leads',
  name: '_target',
  type: 'joinByOne',
  withType: LinkableTypes,
  filters: {
    projection: {
      _url: 1
    }
  },
  idField: 'id'
}
[...]

LinkableTypes is just an array of the widget names to which the link can lead:

[
  "apostrophe-page",
  "article",
  "machine",
  "engine",
  "accessory"
]

apostrophe-page is obvious, the other ones are pieces.

We run into an issue where the widget in anon mode will not obtain any value for data.widget._target._url.

We started to look into overriding the load method in the construct

construct(self, options) {
  const superLoad = self.load;
    self.load = (req, widgets, callback) => {
    superLoad(self.apos.tasks.getReq(), widgets, callback)
  };
}

but it seems like it doesn't work in all contexts. In the language that is not the default, data.widget._target._url is null, while proper in the default language. We use apostrophe-workflow.

I am trying to avoid creating components which would have the URL hardcoded in them.


Solution

  • If you add permission: false to the filters section of your join, pages will be returned without regard to permissions. You should use this judiciously, but it allows you to create the links to pages a logged-in user can't immediately see that you are looking for.