unlink_third_party_id.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import minimist from 'minimist'
  2. import ThirdPartyIdentityManager from '../app/src/Features/User/ThirdPartyIdentityManager.mjs'
  3. import UserGetter from '../app/src/Features/User/UserGetter.mjs'
  4. import { scriptRunner } from './lib/ScriptRunner.mjs'
  5. /**
  6. * This script is used to remove a linked third party identity from a user account.
  7. *
  8. * Parameters:
  9. * --providerId: the third party identity provider (e.g. google, collabratec)
  10. * --userId: the id of the user
  11. * --commit: if present, the script will commit the changes to the database.
  12. *
  13. * Usage:
  14. *
  15. * - dry run:
  16. * node scripts/unlink_third_party_id.mjs --providerId=google --userId=${SOME_USER_ID}
  17. * - commit:
  18. * node scripts/unlink_third_party_id.mjs --providerId=google --userId=${SOME_USER_ID} --commit
  19. */
  20. let COMMIT = false
  21. let PROVIDER_ID
  22. let USER_ID
  23. const setup = () => {
  24. const argv = minimist(process.argv.slice(2))
  25. COMMIT = argv.commit !== undefined
  26. PROVIDER_ID = argv.providerId
  27. USER_ID = argv.userId
  28. if (!COMMIT) {
  29. console.warn('Doing dry run. Add --commit to commit changes')
  30. }
  31. }
  32. async function main() {
  33. if (!PROVIDER_ID) {
  34. throw new Error('No --providerId argument provided')
  35. }
  36. if (!USER_ID) {
  37. throw new Error('No --userId argument provided')
  38. }
  39. const auditLog = {
  40. initiatorId: undefined,
  41. ipAddress: '0.0.0.0',
  42. extraInfo: {
  43. script: true,
  44. },
  45. }
  46. const user = await UserGetter.promises.getUser(USER_ID, {
  47. thirdPartyIdentifiers: 1,
  48. })
  49. console.log(
  50. `Existing thirdPartyIdentifiers: ${JSON.stringify(
  51. user.thirdPartyIdentifiers
  52. )}`
  53. )
  54. console.log(`Removing third party identifier for provider: ${PROVIDER_ID}`)
  55. if (COMMIT) {
  56. const updatedUser = await ThirdPartyIdentityManager.promises.unlink(
  57. USER_ID,
  58. PROVIDER_ID,
  59. auditLog
  60. )
  61. console.log(
  62. `Remaining thirdPartyIdentifiers: ${JSON.stringify(
  63. updatedUser.thirdPartyIdentifiers
  64. )}`
  65. )
  66. }
  67. }
  68. setup()
  69. try {
  70. await scriptRunner(main)
  71. process.exit(0)
  72. } catch (error) {
  73. console.error(error)
  74. process.exit(1)
  75. }