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

Fixed access control oversight in starter (fixes #2518) #2532

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/twelve-singers-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-keystone-app': minor
---

Improved starter access control example so users can't make themselves admins using it.
12 changes: 11 additions & 1 deletion packages/create-keystone-app/example-projects/starter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const userOwnsItem = ({ authentication: { item: user } }) => {
}
return { id: user.id };
};

const userIsAdminOrOwner = auth => {
const isAdmin = access.userIsAdmin(auth);
const isOwner = access.userOwnsItem(auth);
return isAdmin ? isAdmin : isOwner;
};

const access = { userIsAdmin, userOwnsItem, userIsAdminOrOwner };

keystone.createList('User', {
Expand All @@ -38,11 +40,19 @@ keystone.createList('User', {
type: Text,
isUnique: true,
},
isAdmin: { type: Checkbox },
isAdmin: {
type: Checkbox,
// Field-level access controls
// Here, we set more restrictive field access so a non-admin cannot make themselves admin.
access: {
update: access.userIsAdmin,
},
},
password: {
type: Password,
},
},
// List-level access controls
access: {
read: access.userIsAdminOrOwner,
update: access.userIsAdminOrOwner,
Expand Down