Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/project level permissions #8

Merged
merged 2 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ As a template:

```

### permission

- Required: `N`
- Default: `guest`

### permissionErrorMessage

- Required: `N`
- Default: `You must be logged in to view this page`

## Writing Documentation

All documentation is written in Markdown with some added bonuses. Check [Markdown Guide](https://www.markdownguide.org/extended-syntax/) for a reference on how to write Markdown. In addition to the basics, you may also include [tables](https://www.markdownguide.org/extended-syntax/#tables) and [code blocks](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks).
Expand Down
12 changes: 6 additions & 6 deletions lib/guide.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ In your module, be sure to include a sections key.`

options.path = options.path || 'guide';
options.public = options.public || false;
options.permission = options.permission || 'guest';
options.permissionErrorMessage = options.permissionErrorMessage || 'You must be logged in to view this page';

const stylesheets = options.stylesheets;
const scripts = options.scripts;
Expand All @@ -36,13 +38,11 @@ In your module, be sure to include a sections key.`
path: options.path
};

const permissionErrorMessage = 'You must be logged in to view this page';

self.addRoutes = () => {
self.apos.app.get(`/${options.path}`, (req, res) => {
if (!canView(req)) {
res.statusCode = 403;
return res.send(permissionErrorMessage);
return res.send(options.permissionErrorMessage);
}

const sections = setActive(
Expand Down Expand Up @@ -90,7 +90,7 @@ In your module, be sure to include a sections key.`
function canView(req) {
return (
options.public === true ||
(options.public === false && self.apos.permissions.can(req, 'guest'))
(options.public === false && self.apos.permissions.can(req, options.permission))
);
}

Expand All @@ -99,7 +99,7 @@ In your module, be sure to include a sections key.`
self.apos.app.get(demo, (req, res) => {
if (!canView(req)) {
res.statusCode = 403;
return res.send(permissionErrorMessage);
return res.send(options.permissionErrorMessage);
}
const template = path.basename(demo);
return self.sendPage(req, `demos/${template}`, { ...data });
Expand All @@ -123,7 +123,7 @@ In your module, be sure to include a sections key.`
self.apos.app.get(`${url}`, (req, res) => {
if (!canView(req)) {
res.statusCode = 403;
return res.send(permissionErrorMessage);
return res.send(options.permissionErrorMessage);
}

const sections = setActive(self.data.sections, page);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"apostrophe": "^2.108.1"
},
"devDependencies": {
"apostrophe": "^2.116.1",
"eslint": "^7.4.0",
"eslint-config-apostrophe": "^3.2.0",
"eslint-config-standard": "^14.1.1",
Expand Down