Role.registerResolver('$articleViewer', function(role, context, cb) {
function reject(err) {
debug('reject:', err);
if (err) {
return cb(err);
}
cb(null, false);
}
// $articleViewer is only for to Article models
if (context.modelName !== 'Article') {
return reject();
}
// do not allow anonymous users
var userId = context.accessToken.userId;
if (!userId) {
return reject();
}
// get the article
context.model.findById(context.modelId, {}, function(err, article) {
if (err || !article) {
return reject(err);
}
// article is public, allow it
if (article.is_public) {
return cb(null, true);
}
Role.isOwner(context.model, context.modelId, userId, function(err, owner) {
// user is owner, allow it
if (owner) {
return cb(null, true);
}
return reject(err);
});
});
});