【问题标题】:How can I Add ACL rules at run time for all model?如何在运行时为所有模型添加 ACL 规则?
【发布时间】:2016-02-15 13:36:36
【问题描述】:

我正在尝试在运行时为所有模型创建 acl 规则。 我有包含 ACL 信息的表单,例如:

id, 
model name,
property,
accesstype,
principalId, (here I am assign roldId from role table).
PrincipalType.

提交表单后,信息将存储在ACL表中(DB:mysql)。我的问题是如何在 mixin 或 boot scritpt 或任何中获取数据,何时从 acl 表中获取 acl 数据以及如何在运行时分配给所有模型。

我在 mixin 和 boot 脚本中进行了尝试,但我无法弄清楚。

在 mixin 文件中,如何获取 acl 数据以及如何分配给所有模型..

我真的很困惑,因为我不知道什么时候会在运行时为所有模型(如引导、混合、操作挂钩)推送 acl 数据。

请给出任何想法。

我想要喜欢,在 mixin 或 boot script 或任何一个。

从 acl 获取所有数据并分配给所有模型。

In mixin.// I don't know how to get acl data from database in mixin files. 
ACL.find(function(err, data)
{
    var acl = data;
});

// doing some iteration..

 Model.settings.acls.push(data);

【问题讨论】:

  • 我之前没有这样做过,但想法不是将ACL添加到模型中,而是添加到create an ACL 对于模型然后add it to the registry .同样,我以前没有这样做过,但祝你好运!
  • Jakererlla 感谢您的回复。如果你尝试得到结果,请分享给我。
  • 你能分享任何添加注册表的示例或教程吗,我距离开始强循环只有 1 周。你能分享任何用于训练目的的实时强循环应用程序吗?
  • 您好,我还有一个问题。如何从 acl 表数据中为模型应用 acl 规则。

标签: node.js loopbackjs strongloop


【解决方案1】:

默认情况下,ACL(基类)在运行时收集并连接静态 JSON acl 规则和 ACL 表数据,因此它将自动工作。

从 Model.JSON 获取静态 ACL 例如:user.json

ACL.getStaticACLs = function getStaticACLs(model, property) {
var modelClass = loopback.findModel(model);
var staticACLs = [];
if (modelClass && modelClass.settings.acls) {
  modelClass.settings.acls.forEach(function(acl) {
    var prop = acl.property;
    // We support static ACL property with array of string values.
    if (Array.isArray(prop) && prop.indexOf(property) >= 0)
      prop = property;
    if (!prop || prop === ACL.ALL || property === prop) {
      staticACLs.push(new ACL({
        model: model,
        property: prop || ACL.ALL,
        principalType: acl.principalType,
        principalId: acl.principalId, // TODO: Should it be a name?
        accessType: acl.accessType || ACL.ALL,
        permission: acl.permission
      }));
    }
  });
}
var prop = modelClass && (
  // regular property
  modelClass.definition.properties[property] ||
  // relation/scope
  (modelClass._scopeMeta && modelClass._scopeMeta[property]) ||
  // static method
  modelClass[property] ||
  // prototype method
  modelClass.prototype[property]);
if (prop && prop.acls) {
  prop.acls.forEach(function(acl) {
    staticACLs.push(new ACL({
      model: modelClass.modelName,
      property: property,
      principalType: acl.principalType,
      principalId: acl.principalId,
      accessType: acl.accessType,
      permission: acl.permission
    }));
  });
}
return staticACLs;

};

从 ACL 表(数据库)获取 ACL

var self = this;
var roleModel = registry.getModelByType(Role);
this.find({where: {model: model.modelName, property: propertyQuery,
  accessType: accessTypeQuery}}, function(err, acls) {
  if (err) {
    if (callback) callback(err);
    return;
  }
  var inRoleTasks = [];

  acls = acls.concat(staticACLs);

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多