invalidate_tokens.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { db, ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb')
  2. const minimist = require('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. waitForDb().then(async () => {
  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. }
  52. })