remote-acl: add, list, remove (branch and user)

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-14 19:04:00 +00:00
parent 711d717606
commit 640a38f8c1
4 changed files with 60 additions and 3 deletions

View File

@@ -305,7 +305,15 @@ async function remoteBranchProtectionRules(a, b, p, options) {
await aclRemote.list(p, b, { branch: true }) await aclRemote.list(p, b, { branch: true })
} else if (a === 'add') { } else if (a === 'add') {
await aclRemote.add(p, b, { branch: true }) await aclRemote.add(p, b, { branch: true })
if (!b) {
console.error('branch is not provided')
process.exit(1)
}
} else if (a === 'remove') { } else if (a === 'remove') {
if (!b) {
console.error('branch is not provided')
process.exit(1)
}
await aclRemote.remove(p, b, { branch: true }) await aclRemote.remove(p, b, { branch: true })
} else { } else {
throw new Error('Invalid action') throw new Error('Invalid action')
@@ -316,8 +324,20 @@ async function remoteACL(a, b, p, options) {
if (a === 'list') { if (a === 'list') {
await aclRemote.list(p, b) await aclRemote.list(p, b)
} else if (a === 'add') { } else if (a === 'add') {
if (!b) {
console.error('User not provided')
process.exit(1)
}
if (b.split(':').length !== 2) {
console.error('Invalid role')
process.exit(1)
}
await aclRemote.add(p, b) await aclRemote.add(p, b)
} else if (a === 'remove') { } else if (a === 'remove') {
if (!b) {
console.error('User not provided')
process.exit(1)
}
await aclRemote.remove(p, b) await aclRemote.remove(p, b)
} else { } else {
throw new Error('Invalid action') throw new Error('Invalid action')

View File

@@ -9,14 +9,18 @@ async function getACLHandler (publicKey, req) {
} }
async function addACLHandler (publicKey, req) { async function addACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const { repoName, userId, acl, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
isBranch ? ACL.addProtectedBranch(repoName, name) : ACL.grantAccessToUser(repoName, ...name.split(':'))
const repoACL = ACL.getACL(repoName) const repoACL = ACL.getACL(repoName)
return Buffer.from(JSON.stringify(repoACL)) return Buffer.from(JSON.stringify(repoACL))
} }
async function delACLHandler (publicKey, req) { async function delACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const { repoName, userId, acl, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
isBranch ? ACL.removeProtectedBranch(repoName, name) : ACL.revokeAccessFromUser(repoName, name)
const repoACL = ACL.getACL(repoName) const repoACL = ACL.getACL(repoName)
return Buffer.from(JSON.stringify(repoACL)) return Buffer.from(JSON.stringify(repoACL))
@@ -35,8 +39,10 @@ async function parseACLRequest(publicKey, req) {
return { return {
repoName, repoName,
name: request.body.name,
userId, userId,
acl: request.body.acl, acl: request.body.acl,
isBranch: !!request.body.branch,
} }
} }

View File

@@ -28,6 +28,34 @@ function listACLBranch(repoACL) {
logBranches(repoACL) logBranches(repoACL)
} }
async function add(url, name, rpc, opts) {
const payload = { body: { url, method: 'add-acl', name } }
if (opts.branch) payload.body.branch = true
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const repoACLres = await rpc.request('add-acl', Buffer.from(JSON.stringify(payload)))
const repoACL = JSON.parse(repoACLres.toString())
opts.branch ? listACLBranch(repoACL) : listACLUser(repoACL, name.split(':')[0])
process.exit(0)
}
async function del(url, name, rpc, opts) {
const payload = { body: { url, method: 'del-acl', name } }
if (opts.branch) payload.body.branch = true
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const repoACLres = await rpc.request('del-acl', Buffer.from(JSON.stringify(payload)))
const repoACL = JSON.parse(repoACLres.toString())
opts.branch ? listACLBranch(repoACL) : listACLUser(repoACL, name)
process.exit(0)
}
async function wrapper (url, name, opts = {}, cb) { async function wrapper (url, name, opts = {}, cb) {
if (typeof opts === 'function') { if (typeof opts === 'function') {
cb = opts cb = opts
@@ -82,5 +110,7 @@ async function wrapper (url, name, opts = {}, cb) {
} }
module.exports = { module.exports = {
list: (url, name, opts) => wrapper(url, name, opts, list) list: (url, name, opts) => wrapper(url, name, opts, list),
add: (url, name, opts) => wrapper(url, name, opts, add),
remove: (url, name, opts) => wrapper(url, name, opts, del),
} }

View File

@@ -30,6 +30,7 @@ function logBranches(repoACL) {
module.exports = { module.exports = {
printACL, printACL,
printACLForUser,
checkIfGitRepo, checkIfGitRepo,
logBranches, logBranches,
} }