【问题标题】:ACL on certain records instead of API URLs某些记录上的 ACL 而不是 API URL
【发布时间】:2015-06-21 09:52:54
【问题描述】:

我遇到了这样一种情况,即登录用户可以在类似 CMS 的系统中创建博客文章。所以他在后端系统中创建了这些帖子。

当该用户在管理面板中的Blog 页面上时,会发送如下 API 请求:

/api/blogs?filter={'userid': '100'}

这将获取该用户发表的所有博客文章。此用户只能查看和编辑他自己的博客文章。

但如果他将 URL 更改为这样的内容:

/api/blogs?filter={'userid': '1'}

然后他可以获取其他用户的博客文章,我想禁止这样做。

我知道 Loopback 有 ACL。但据我所知,只能对整个GETPOST 等请求进行限制。换句话说,用户可以拨打/api/blogs,也可以不拨打。

在这种情况下,我希望用户能够调用 API url。但我想禁止查看某些记录。

我应该如何在 Loopback 中以动态的方式处理这样的场景?

【问题讨论】:

    标签: loopbackjs strongloop loopback


    【解决方案1】:

    如果我完全理解您,您的问题可以通过“远程方法”和“远程挂钩”来解决。

    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'))
            }
        });
    }
    

    由于您定义了远程挂钩,因此可以在执行前检查某些内容。您可以获取与访问令牌相关的用户 ID。然后比较从参数中获取的用户 id 和传入的用户 id。

    您可以从环回文档中获得更多详细信息

    1. Remote methods
    2. Remote hooks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多