2015-06-20 48 views
0

我遇到了登錄用戶可以在系統中的CMS中創建博客文章的情況。所以他在後臺系統中創建這些帖子。訪問某些記錄而不是API網址的ACL

當該用戶是在管理面板的Blog頁上,然後像這樣的API請求被髮送:

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

這得到所有該用戶發出的博客文章。該用戶只能查看和編輯他自己的博客帖子。

但如果他改變了網址是這樣的:

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

那麼他可以得到另一個用戶的博客文章,我想禁止用戶。

我知道Loopback有ACL。但據我所知,只能對整個GETPOST等請求施加限制。換句話說,用戶可以撥打/api/blogs或他不能。

在這種情況下,我希望用戶能夠調用API url。但我想禁止查看某些記錄。

我應該如何在Loopback中以動態的方式處理這種情況?

回答

0

如果我明白你的意思,你的問題可以通過'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')) 
     } 
    }); 
} 

由於您定義了遠程掛鉤,因此可以在執行前檢查一些東西。您可以獲取與訪問令牌相關的用戶標識。然後比較獲取的用戶標識和來自參數的傳入用戶標識。

您可以從環回單證獲得更詳細

  1. Remote methods
  2. Remote hooks