可编程访问控制

建议你使用声明式访问控制来实现业务网络定义中的访问控制规则。但是,你可以通过获取和测试当前参与者,在交易处理器中实现可编程的访问控制。你可以针对当前参与者的属性运行测试,以允许或拒绝交易处理器函数的执行。

在你开始之前

在你执行这些步骤之前,你必须对业务网络定义中的参与者进行建模,并将其部署为业务网络。你必须创建了这些参与者的一些实例,并向这些参与者发放身份。

下节的过程显示了使用下面参与者模型的示例:

  1. namespace net.biz.digitalPropertyNetwork
  2. participant Person identified by personId {
  3. o String personId
  4. o String firstName
  5. o String lastName
  6. }
  7. participant PrivilegedPerson extends Person {
  8. }

过程

  1. 在你的交易处理器函数中,使用以下getCurrentParticipant功能验证当前参与者的类型是否符合要求:
  1. function onPrivilegedTransaction(privilegedTransaction) {
  2. var currentParticipant = getCurrentParticipant();
  3. if (currentParticipant.getFullyQualifiedType() !== 'net.biz.digitalPropertyNetwork.PrivilegedPerson') {
  4. throw new Error('Transaction can only be submitted by a privileged person');
  5. }
  6. // Current participant must be a privileged person to get here.
  7. }
  1. 在你的交易处理器函数中,使用以下getCurrentParticipant函数验证当前参与者的参与者ID :
  1. function onPrivilegedTransaction(privilegedTransaction) {
  2. var currentParticipant = getCurrentParticipant();
  3. if (currentParticipant.getFullyQualifiedIdentifier() !== 'net.biz.digitalPropertyNetwork.Person#PERSON_1') {
  4. throw new Error('Transaction can only be submitted by person 1');
  5. }
  6. // Current participant must be person 1 to get here.
  7. }

可以将当前参与者的参与者ID与(通过关联)链接到资产的参与者进行比较,以验证当前参与者是否有权访问或修改某资产:

  1. function onPrivilegedTransaction(privilegedTransaction) {
  2. // Get the owner of the asset in the transaction.
  3. var assetOwner = privilegedTransaction.asset.owner;
  4. var currentParticipant = getCurrentParticipant();
  5. if (currentParticipant.getFullyQualifiedIdentifier() !== asset.owner.getFullyQualifiedIdentifier()) {
  6. throw new Error('Transaction can only be submitted by the owner of the asset');
  7. }
  8. // Current participant must be the owner of the asset to get here.
  9. }