多用户角色环回 [英] Multiple users roles loopback

查看:68
本文介绍了多用户角色环回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Loopback作为后端来创建应用程序.我以前已经使用过环回,但是现在我想做一些以前从未做过的事情.

I am trying to make an application using Loopback as my back-end. I already used loopback before, but right now I want to do something that I never done before.

我想要的很简单,我将有3种类型的用户,管理员,服务人员和默认用户.但是,我需要限制每种用户的访问控制.管理员可以请求我的所有路由,但是例如默认用户只能请求我将指定的某些路由. ACL部分我知道该怎么做,但是我找不到任何解释如何使每种类型的用户成为角色并使其正常工作的内容.

What I want is simple, I will have 3 types of users, administrator, servicer and default. But, I need to restrict the access controls for each type of user; the administrator can request all my routes, but de default user for example can only request some routes that I will specify. The ACL part I know how to do, but I can't find anything explaining how to make each type of user a role and make it work.

任何人都可以在此处发布至少具有两个用户和角色的示例吗?

Anyone can post here an example with at least two users and roles?

推荐答案

第一步是将2个新角色持久化到数据库中,即管理员"和服务员".您可以手动执行此步骤,也可以创建可重复使用的脚本:

The first step is to persist the 2 new roles into your database, "administrator" and "servicer". You can either do this step manually or create a script you can reuse:

// commands/add_roles.js

let app = require('../server/server')

function createRole(name, description, done) {
  app.models.Role.findOrCreate(
    {where: {name: name}}, 
    {name, description},
    err => {
      // TODO handle error
      
      done && done()
    }
  )  
}

createRole('administrator', 'Administrators have more control on the data', () => {
  createRole('servicer', 'servicer description', process.exit)
})

然后,将角色与用户相关联.根据您的应用程序,在需要时执行以下代码.

Then, you associate a role to a user. Execute the code below whenever you desire, depending on your application.

app.models.Role.findOne({where: {name: 'administrator'}}, (err, role) => {
  // TODO handle error

  app.models.RoleMapping.findOrCreate({where: {principalId: user.id}}, {
    roleId: role.id,
    principalType: RoleMapping.USER,
    principalId: user.id
  }, function (err) {
    // TODO handle error
      
    // if no errors, user has now the role administrator
  })
})

您现在可以在模型的ACL中使用角色管理员"和服务员".

You can now use the roles "administrator" and "servicer" in your models' ACLs.

这篇关于多用户角色环回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆