mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-01-10 18:04:13 +00:00
99 lines
3.4 KiB
Plaintext
99 lines
3.4 KiB
Plaintext
// Assume contract instance A is applying for contract instance B...
|
|
module NaiveDAC{
|
|
function initDAC(req){
|
|
rawAuthInfo = getAuthInfo();
|
|
var tmpStr = rawAuthInfo.substr(0, 6);
|
|
if (tmpStr == "Failed") {
|
|
Global.authInfo = {
|
|
};
|
|
// public keys of contract instances that a contract instance has accepted
|
|
// public key -> DOI | remark
|
|
Global.authInfo.acceptList = {
|
|
};
|
|
// public keys of contract instances that a contract instance is being applied for
|
|
// public key -> DOI | remark
|
|
Global.authInfo.appliedList = {
|
|
};
|
|
// DOIs of contract instances that a contract instance is applying for
|
|
// DOI -> remark
|
|
Global.authInfo.applyingDOIList = {
|
|
};
|
|
}
|
|
else {
|
|
Global.authInfo = JSON.parse(rawAuthInfo);
|
|
}
|
|
// Public key of the owner of a contract instance
|
|
Global.owner = req;
|
|
}
|
|
// B accepts one application from public key "req"
|
|
export function accept(req){
|
|
if (requester == Global.owner){
|
|
Global.authInfo.acceptList[req] = Global.authInfo.appliedList[req];
|
|
if (Global.authInfo.appliedList[req] != undefined){
|
|
Global.authInfo.appliedList[req] = undefined;
|
|
}
|
|
setAuthInfo(JSON.stringify(Global.authInfo));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
// B cancels one application from public key "req"
|
|
export function cancel(req){
|
|
if (requester == Global.owner){
|
|
Global.authInfo.appliedList[req] = Global.authInfo.acceptList[req];
|
|
if (Global.authInfo.acceptList[req] != undefined){
|
|
Global.authInfo.acceptList[req] = undefined;
|
|
}
|
|
setAuthInfo(JSON.stringify(Global.authInfo));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
// B is being applied for "remark" reason by a public "requester" and "requesterDOI"
|
|
export function applied(remark){
|
|
if (remark != undefined && requester != undefined && requesterDOI != undefined){
|
|
Global.authInfo.appliedList[requester] = requesterDOI + "|" + remark;
|
|
setAuthInfo(JSON.stringify(Global.authInfo));
|
|
return "success";
|
|
}
|
|
return "failed";
|
|
}
|
|
// A is applying for "targetDOI" for "remark" reason
|
|
export function applying(args){
|
|
args = JSON.parse(args);
|
|
if (args.remark != undefined && args.targetDOI != undefined){
|
|
Global.authInfo.applyingDOIList[args.targetDOI] = args.remark;
|
|
setAuthInfo(JSON.stringify(Global.authInfo));
|
|
return executeContractByDOI(args.targetDOI, "applied", args.remark);
|
|
}
|
|
return "failed";
|
|
}
|
|
export function loadResource(arg){
|
|
return Global.Resources.loadAsString(arg);
|
|
}
|
|
export function isOwner(arg){
|
|
return Global.owner == requester;
|
|
}
|
|
export function getAppliedList(arg){
|
|
return JSON.stringify(Global.authInfo.appliedList);
|
|
}
|
|
export function getAcceptList(arg){
|
|
return JSON.stringify(Global.authInfo.acceptList);
|
|
}
|
|
export function getApplyingDOIList(arg){
|
|
return JSON.stringify(Global.authInfo.applyingDOIList);
|
|
}
|
|
// check if public "requester" has the permission of B
|
|
export function appliedHasPermission(arg){
|
|
return (Global.authInfo.acceptList[requester]!=undefined);
|
|
}
|
|
// check if "req" has the permission of B
|
|
export function checkPermission(req){
|
|
return (Global.authInfo.acceptList[req]!=undefined);
|
|
}
|
|
// check if A with "pubkey" has the permission of B with "targetDOI"
|
|
export function applyingHasPermission(targetDOI){
|
|
return executeContractByDOI(targetDOI, "appliedHasPermission");
|
|
}
|
|
}
|