简体   繁体   English

使用Connect Roles进​​行Node.js角色授权

[英]Node.js roles authorization using Connect Roles

I have been following Connect roles https://github.com/ForbesLindesay/connect-roles for authorization in my node js application every thing is coming through except this This is what documented in above link 我一直在关注连接角色https://github.com/ForbesLindesay/connect-roles以便在我的节点js应用程序中进行授权,除此之外,所有这些都是通过以上链接记录的内容

user.can(action) and user.is(action)

Inside the views of an express application you may use user.can and user.is 

which are equivallent to req.user.can and req.user.is 这与req.user.can和req.user.is等效

e.g.

<% if (user.can('impersonate')) { %>
  <button id="impersonate">Impersonate</button>
<% } %>

I tried to use it but i am very much confused because in my view (my html page) how can i check for specific role. 我试图使用它,但我非常困惑,因为在我看来(我的HTML页面)我如何检查具体的角色。 Do i have to call service for checking back in the server. 我是否必须致电服务以便在服务器中进行检查。 Because i cannot use directly like in my app.js file as this 因为我不能像我的app.js文件那样直接使用

var user = require('connect-roles');
user.use('access private page', function (req) {
  if (req.user.role ==== 'moderator') {
    return true;
  }});
app.get('/private', user.can('access private page'), function (req, res) {
  res.render('private');
});

If you are using Jade it is as easy as: 如果您使用Jade,它就像以下一样简单:

if userCan('do something')
   button#special Do Something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM