invalidate_tokens.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { db, ObjectId } = require('../app/src/infrastructure/mongojs')
  2. const minimist = require('minimist')
  3. const _ = require('lodash')
  4. const argv = minimist(process.argv.slice(2))
  5. const commit = argv.commit !== undefined
  6. const projectIds = argv._.map(x => {
  7. return ObjectId(x)
  8. })
  9. if (!commit) {
  10. console.log('Doing dry run without --commit')
  11. }
  12. console.log('checking', projectIds.length, 'projects')
  13. db.projects.find(
  14. {
  15. _id: { $in: projectIds }
  16. },
  17. (err, affectedProjects) => {
  18. if (err) {
  19. throw err
  20. }
  21. console.log('Found ' + affectedProjects.length + ' affected projects')
  22. affectedProjects.forEach(x => {
  23. console.log(
  24. JSON.stringify(
  25. _.pick(x, [
  26. '_id',
  27. 'owner_ref',
  28. 'tokenAccessReadOnly_refs',
  29. 'tokenAccessReadAndWrite_refs'
  30. ])
  31. )
  32. )
  33. })
  34. if (!commit) {
  35. console.log('dry run, not updating')
  36. process.exit(0)
  37. } else {
  38. db.projects.update(
  39. {
  40. _id: {
  41. $in: affectedProjects.map(x => {
  42. return x._id
  43. })
  44. }
  45. },
  46. {
  47. $set: {
  48. publicAccesLevel: 'private', // note the spelling in the db is publicAccesLevel (with one 's')
  49. tokenAccessReadOnly_refs: [],
  50. tokenAccessReadAndWrite_refs: []
  51. }
  52. },
  53. {
  54. multi: true
  55. },
  56. (err, result) => {
  57. console.log('err', err, 'result', result)
  58. db.close()
  59. setTimeout(() => {
  60. process.exit(0)
  61. }, 5000)
  62. }
  63. )
  64. }
  65. }
  66. )