OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / scripts / grant-admin.js

63 lines · javascript ref:
#!/usr/bin/env bare

// Load Node.js compatibility for Bare runtime
await import('bare-node-runtime/global')

import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
// IdentityRegistry removed — unified MetadataBase handles identity

const userId = process.argv[2]

if (!userId) {
  console.error('Usage: bare grant-admin.js <userId>')
  process.exit(1)
}

console.log(`Granting admin permissions to user: ${userId}`)

// Open storage
const storage = './pear-git-storage'
const store = new Corestore(storage)

// Open registry (Hyperbee for direct lookups)
const registryCore = store.get({ name: 'registry' })
const registry = new Hyperbee(registryCore, {
  keyEncoding: 'utf-8',
  valueEncoding: 'json'
})

await registry.ready()

// Get user
const userKey = `user:${userId}`
const userEntry = await registry.get(userKey)

if (!userEntry) {
  console.error(`User not found: ${userId}`)
  process.exit(1)
}

const user = userEntry.value

console.log('Current user:', {
  userId: user.userId,
  username: user.username,
  globalPermissions: user.globalPermissions
})

// Add admin permission if not already present
if (!user.globalPermissions.includes('admin')) {
  user.globalPermissions.push('admin')

  // Save updated user
  await registry.put(userKey, user)

  console.log('✅ Admin permission granted!')
  console.log('Updated permissions:', user.globalPermissions)
} else {
  console.log('✅ User already has admin permission')
}

process.exit(0)