From 3f8d95b74f4fc7d549f194eb2efd9b0abe967041 Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Sun, 26 Feb 2023 16:42:30 +0800 Subject: [PATCH] access control tohub --- src/git3/index.ts | 118 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/src/git3/index.ts b/src/git3/index.ts index 37b712a..84c8e7a 100644 --- a/src/git3/index.ts +++ b/src/git3/index.ts @@ -234,6 +234,124 @@ program console.log(explorerTxUrl(receipt.transactionHash, protocol.netConfig.explorers)) }) +program + .command("addManager") + .argument("", "hub_name.NS or hub_address:chain_id") + .argument("", "manager address") + .description("add a manager into hub") + .action(async (hub,managerAddr) => { + let protocol = await parseGit3URI(hub, { ignoreProtocolHeader: true, skipRepoName: true }) + let [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(protocol.wallet.address) + if (!isAdmin) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`[addManager] can only be executed with the admin authority of this hub: ${hubName}`) + return + } + + [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(managerAddr) + if (isManager) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`${managerAddr} is already a manager to hub: ${hubName}`) + return + } + + const txManager = new TxManager(protocol.hub, protocol.chainId, protocol.netConfig.txConst) + let receipt = await txManager.SendCall("addManager", [managerAddr]) + console.log(explorerTxUrl(receipt.transactionHash, protocol.netConfig.explorers)) + }) + +program + .command("removeManager") + .argument("", "hub_name.NS or hub_address:chain_id") + .argument("", "manager address") + .description("remove a manager from hub") + .action(async (hub,managerAddr) => { + let protocol = await parseGit3URI(hub, { ignoreProtocolHeader: true, skipRepoName: true }) + let [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(protocol.wallet.address) + if (!isAdmin) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`[removeManager] can only be executed with the admin authority of this hub: ${hubName}`) + return + } + + [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(managerAddr) + if (!isManager) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`${managerAddr} is not a manager to hub: ${hubName}`) + return + } + + const txManager = new TxManager(protocol.hub, protocol.chainId, protocol.netConfig.txConst) + let receipt = await txManager.SendCall("removeManager", [managerAddr]) + console.log(explorerTxUrl(receipt.transactionHash, protocol.netConfig.explorers)) + }) + +program + .command("addContributor") + .argument("", "hub_name.NS or hub_address:chain_id") + .argument("", "contributor address") + .description("add a manager into hub") + .action(async (hub,contributorAddr) => { + let protocol = await parseGit3URI(hub, { ignoreProtocolHeader: true, skipRepoName: true }) + let [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(protocol.wallet.address) + if (!isManager) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`[addContributor] can only be executed with the manager authority of this hub: ${hubName}`) + return + } + + [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(contributorAddr) + if (isContributor) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`${contributorAddr} is already a contributor to hub: ${hubName}`) + return + } + const txManager = new TxManager(protocol.hub, protocol.chainId, protocol.netConfig.txConst) + let receipt = await txManager.SendCall("addContributor", [contributorAddr]) + console.log(explorerTxUrl(receipt.transactionHash, protocol.netConfig.explorers)) + }) + +program + .command("removeContributor") + .argument("", "hub_name.NS or hub_address:chain_id") + .argument("", "contributor address") + .description("add a manager into hub") + .action(async (hub,contributorAddr) => { + let protocol = await parseGit3URI(hub, { ignoreProtocolHeader: true, skipRepoName: true }) + let [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(protocol.wallet.address) + if (!isManager) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`[removeContributor] can only be executed with the manager authority of this hub: ${hubName}`) + return + } + + [isAdmin,isManager,isContributor] = await protocol.hub.memberRole(contributorAddr) + if (!isContributor) { + let hubName = protocol.ns + ? `${protocol.nsName}.${protocol.nsDomain}` + : protocol.hubAddress + console.error(`${contributorAddr} is not a contributor to hub: ${hubName}`) + return + } + const txManager = new TxManager(protocol.hub, protocol.chainId, protocol.netConfig.txConst) + let receipt = await txManager.SendCall("removeContributor", [contributorAddr]) + console.log(explorerTxUrl(receipt.transactionHash, protocol.netConfig.explorers)) + }) + program .command("info") .argument("[wallet]", "wallet you want to get info", "default")