Skip to content

Instantly share code, notes, and snippets.

@danscan
Last active January 25, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danscan/d84b90b153075f28da4a to your computer and use it in GitHub Desktop.
Save danscan/d84b90b153075f28da4a to your computer and use it in GitHub Desktop.
Policy function
function requestUserOwnsPost(requestUser, post) {
return requestUser.id === post.owner;
}
const db = {
postsById: {
'2': {
id: 2,
owner: 1,
body: 'hey',
},
},
getPost(postId) {
return this.postsById[postId];
},
patchPost(postId, patch) {
const post = this.getPost(postId);
const patchedPost = { ...post, ...patch };
this.postsById[postId] = patchedPost;
return patchedPost;
},
};
app.patch('/posts/:postId', (req, res, next) => {
const { postId } = req.params;
const post = db.getPost(postId);
const permitOperation = requestUserOwnsPost(req.user, post);
if (!permitOperation) {
return next(new Error('You cannot patch a post you do not own'));
}
return req.json(db.patchPost(postId, req.body));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment