invalidate_tokens.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js'
  2. import minimist from 'minimist'
  3. const argv = minimist(process.argv.slice(2))
  4. const commit = argv.commit !== undefined
  5. const projectIds = argv._.map(x => {
  6. return new ObjectId(x)
  7. })
  8. if (!commit) {
  9. console.log('Doing dry run without --commit')
  10. }
  11. console.log('checking', projectIds.length, 'projects')
  12. await waitForDb()
  13. const affectedProjects = await db.projects
  14. .find(
  15. { _id: { $in: projectIds } },
  16. {
  17. projection: {
  18. _id: 1,
  19. owner_ref: 1,
  20. tokenAccessReadOnly_refs: 1,
  21. tokenAccessReadAndWrite_refs: 1,
  22. },
  23. }
  24. )
  25. .toArray()
  26. console.log('Found ' + affectedProjects.length + ' affected projects')
  27. affectedProjects.forEach(project => {
  28. console.log(JSON.stringify(project))
  29. })
  30. if (!commit) {
  31. console.log('dry run, not updating')
  32. process.exit(0)
  33. } else {
  34. try {
  35. const result = await db.projects.updateMany(
  36. { _id: { $in: affectedProjects.map(project => project._id) } },
  37. {
  38. $set: {
  39. publicAccesLevel: 'private', // note the spelling in the db is publicAccesLevel (with one 's')
  40. tokenAccessReadOnly_refs: [],
  41. tokenAccessReadAndWrite_refs: [],
  42. },
  43. }
  44. )
  45. console.log('result', JSON.stringify(result))
  46. process.exit(0)
  47. } catch (err) {
  48. console.error('err', err)
  49. process.exit(1)
  50. }
  51. }