访问某些记录而不是API网址的ACL
问题描述:
我遇到了登录用户可以在系统中的CMS中创建博客文章的情况。所以他在后台系统中创建这些帖子。访问某些记录而不是API网址的ACL
当该用户是在管理面板的Blog
页上,然后像这样的API请求被发送:
/api/blogs?filter={'userid': '100'}
这得到所有该用户发出的博客文章。该用户只能查看和编辑他自己的博客帖子。
但如果他改变了网址是这样的:
/api/blogs?filter={'userid': '1'}
那么他可以得到另一个用户的博客文章,我想禁止用户。
我知道Loopback有ACL。但据我所知,只能对整个GET
,POST
等请求施加限制。换句话说,用户可以拨打/api/blogs
或他不能。
在这种情况下,我希望用户能够调用API url。但我想禁止查看某些记录。
我应该如何在Loopback中以动态的方式处理这种情况?
答
如果我明白你的意思,你的问题可以通过'remote methods'和'remote hooks'来解决。
module.exports = function (Blog){
Blog.getBlogs = function(userid, cb){
// Db operation.
// Fetch blogs by userid.
cb(null, blogs);
};
Blog.remoteMethod('getBlogs', {
accepts: {arg: 'userid', type: 'number'},
returns: {arg: 'blogs', type: 'object'}
});
Blog.beforeRemote('getBlogs', function(ctx, unused, next) {
if(ctx.req.accessToken) {
// Fetch user id by accesToken
// Compare user id's
next();
} else {
next(new Error('You are not allowed to get these messages'))
}
});
}
由于您定义了远程挂钩,因此可以在执行前检查一些东西。您可以获取与访问令牌相关的用户标识。然后比较获取的用户标识和来自参数的传入用户标识。
您可以从环回单证获得更详细