OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / lib / commands / admin.js

52 lines · javascript ref:
/** pgh admin users|grant|revoke|deactivate|reactivate */

export const usage = `pgh admin — admin operations on a daemon

  pgh admin users                          List all users
  pgh admin grant <userId> <permission>
  pgh admin revoke <identityId>
  pgh admin deactivate <userId> [reason]
  pgh admin reactivate <userId>
  pgh admin rebind <userId> <identityId>`

export async function run (client, args) {
  const sub = args[0]
  if (!sub) throw new Error(usage)

  if (sub === 'users') return client.call('list-users', {})

  if (sub === 'grant') {
    const userId = args[1]
    const perm = args[2]
    if (!userId || !perm) throw new Error('usage: pgh admin grant <userId> <permission>')
    return client.call('grant-permission', { userId, permission: perm })
  }

  if (sub === 'revoke') {
    const identityId = args[1]
    if (!identityId) throw new Error('usage: pgh admin revoke <identityId>')
    return client.call('revoke-identity', { identityId })
  }

  if (sub === 'deactivate') {
    const userId = args[1]
    if (!userId) throw new Error('usage: pgh admin deactivate <userId>')
    return client.call('deactivate-user', { userId, reason: args[2] || null })
  }

  if (sub === 'reactivate') {
    const userId = args[1]
    if (!userId) throw new Error('usage: pgh admin reactivate <userId>')
    return client.call('reactivate-user', { userId })
  }

  if (sub === 'rebind') {
    const userId = args[1]
    const identityId = args[2]
    if (!userId || !identityId) throw new Error('usage: pgh admin rebind <userId> <identityId>')
    return client.call('admin-rebind-identity', { userId, identityId })
  }

  throw new Error('unknown admin subcommand: ' + sub + '\n' + usage)
}