首页 > 解决方案 > 超级账本作曲家访问控制,允许在交易中创建

问题描述

这是我想要实现的:

我想允许用户(某种参与者)创建资产,但只在事务中进行,而在此事务之外,我想拒绝所有用户创建资产的权限。

我尝试使用规则 .acl 文件中的条件使用函数来解决它:

rule UserCanCreateAssetOnlyInTransaction {
    description: "Deny all participants create access to all userWallets if not in transaction"
    participant(p): "com.example.User"
    operation: CREATE
    resource(r): "com.example.UserAsset"
    condition:(isInTransactionF())
    action: ALLOW 
}

然后在事务中创建变量 as var isInTransaction = true;,并在logic.js文件中添加:

/**
@returns {boolean} boolean true/false
*/
function isInTransactionF(){
    if(isInTransaction){
      return true; 
    }else{
      return false;
    }   
}

它不起作用,当我调用创建访问权限应该工作的唯一事务时,它说用户没有创建访问权限来提交此事务。我想我做错了什么,有什么办法可以解决这个问题吗?

标签: hyperledgerhyperledger-composer

解决方案


在你的功能中实现你想要的 - 你会说:

/**
@returns {boolean} boolean true/false
*/
function isInTransactionF() {
    var isInTransaction = true ;  // Boolean
    if(isInTransaction) {
    // if( Boolean(isInTransaction)) { // alternative
      return true; 
    } else{
      return false;
    }   
}

然后,您当前的 ACL 就可以工作了。

我可以调用 console.log 来查看返回的结果

console.log("The return result is " + isInTransactionF() );`  // true

限制参与者仅通过某个交易类创建资产 - 规则看起来像(即资产只能通过此类创建 - 假设没有其他资产创建规则,它应该在其他地方被隐式拒绝):

rule CreateAssetThruTxn {
    description: "sample""
    participant(p): "com.example.User"
    operation: CREATE
    resource(r): "com.example.UserAsset"
    transaction(tx): "com.example.AssetCreate"
    condition:(true)
    action: ALLOW 
}

如果您的 ACL 失败,那么您需要查看哪些其他 ACL 规则可能允许通过其他方式创建此资产,但我提供的规则将是控制它的常用方法(基于您在问题中提供的信息)


推荐阅读