2017-09-02 73 views
0

我創建一個Web應用程序中,我使用REST用戶界面和REST API我用用環回到環回。我的用戶,acl,rollmapping,角色表都在mySQL中。在我的項目中,我可以控制訪問權限,當我嘗試使用回送UI(登錄並設置訪問令牌後)。但是,當我嘗試與管理員休息用戶界面我能夠登錄但無法控制訪問,在管理休息我已經給所有的網址和所有在authClient.jsx。我的authClient.jsx文件:無法限制在登錄管理權限使用管理上休息

const request = new Request('http://localhost:3004/api/Users/login', { 
    method: 'POST', 
    body: JSON.stringify({ email, password }), 
    headers: new Headers({ 'Content-Type': 'application/json' }) 
}); 

任何人都可以幫我解決這個問題嗎?

回答

1

您需要使用AOR權限

https://github.com/marmelab/aor-permissions

這將處理所有身份驗證和基於角色的訪問。

在API方面,您將需要創建一個自定義登錄方法,該方法也將返回請求中的用戶角色。下面

User.customLogin = (credentials, cb) => { 
     User.login(credentials, 'User', function(err, token) { 
      if (err) { 
       console.error(err) 
       return cb(err) 
      } 
      app.models.RoleMapping.findOne({where: {principalId: token.userId}, include: {relation: 'role'}}, function(err, rolemap) { 
       if (err) { 
        console.error(err) 
        return cb(err) 
       } 
       token.role = rolemap.role().name 
       return cb(null, token) 
      }) 
     }) 
    } 

保存在localStorage的上登錄,然後你可以使用AOR權限展現給每一位用戶的基於角色的看法用戶角色

類似。

編輯: 根據AOR明星貢獻者@gildas下面。 AOR權限將被棄用,所有功能都將移至AOR Core。所以請檢查您的AOR版本並據此作出決定。

+1

aor-permissions將會隨着即將到來的1.3.0版admin-on-rest而被棄用。事實上,它的功能已被合併到核心軟件包中。 – Gildas

+0

糟糕。我的錯。你們移動快速的男人:) –

+0

@kunalpareek你可以看看這個:https://github.com/kimkha/aor-loopback/issues/15 – b24