Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env node
  2. // One-time PRODUCTION migration for "BizGaze-only logins".
  3. //
  4. // Deletes the in-app (pre-BizGaze) local accounts. Combined with the BizGaze-only login
  5. // change, every user then signs in through BizGaze and is provisioned into the same
  6. // tenant — which restores the admin's "see all sessions" report.
  7. //
  8. // A "pre-BizGaze" account = a user with NO 'sso_user_created' audit entry for its email
  9. // (i.e. created locally via register/console, not provisioned by a BizGaze login).
  10. //
  11. // SAFE BY DEFAULT: dry-run unless you pass --apply. BACK UP THE DB FIRST.
  12. // Dry run : node scripts/migrate-bizgaze-only.js
  13. // Apply : node scripts/migrate-bizgaze-only.js --apply
  14. // Honors DB_PATH (same env var the server uses).
  15. const db = require('../db');
  16. const APPLY = process.argv.includes('--apply');
  17. const tableExists = (name) => !!db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?").get(name);
  18. const ssoEmails = new Set(
  19. db.prepare("SELECT DISTINCT lower(user_email) AS e FROM audit_log WHERE action='sso_user_created' AND user_email IS NOT NULL")
  20. .all().map((r) => r.e),
  21. );
  22. const users = db.prepare('SELECT id,email,name,role,team_id,active FROM users').all();
  23. const keep = users.filter((u) => ssoEmails.has(String(u.email).toLowerCase()));
  24. const remove = users.filter((u) => !ssoEmails.has(String(u.email).toLowerCase()));
  25. console.log('=== Teams ===');
  26. for (const t of db.prepare('SELECT id,name FROM teams').all()) {
  27. const uc = db.prepare('SELECT COUNT(*) AS c FROM users WHERE team_id=?').get(t.id).c;
  28. console.log(` ${t.id} ${t.name} (${uc} users)`);
  29. }
  30. console.log('\n=== Users ===');
  31. console.log(` total: ${users.length} | BizGaze-provisioned (keep): ${keep.length} | local pre-BizGaze (delete): ${remove.length}`);
  32. console.log('\n KEEP (already BizGaze-provisioned):');
  33. keep.forEach((u) => console.log(` ${u.email} [${u.role}] team ${u.team_id}`));
  34. console.log('\n DELETE (local / pre-BizGaze):');
  35. remove.forEach((u) => console.log(` ${u.email} [${u.role}] team ${u.team_id}`));
  36. if (!remove.length) { console.log('\nNothing to delete. Done.'); process.exit(0); }
  37. if (!APPLY) {
  38. console.log('\nDRY RUN — no changes made. Re-run with --apply to delete the local accounts above.');
  39. console.log('After deletion, those users sign in via BizGaze and are recreated automatically.');
  40. process.exit(0);
  41. }
  42. const delAuth = db.prepare('DELETE FROM sessions_auth WHERE user_id=?');
  43. const delRefresh = tableExists('refresh_tokens') ? db.prepare('DELETE FROM refresh_tokens WHERE user_id=?') : null;
  44. const delUser = db.prepare('DELETE FROM users WHERE id=?');
  45. let deleted = 0;
  46. db.exec('BEGIN');
  47. try {
  48. for (const u of remove) {
  49. delAuth.run(u.id); // clear active sessions (FK) — also logs them out
  50. if (delRefresh) delRefresh.run(u.id);
  51. delUser.run(u.id);
  52. deleted++;
  53. }
  54. db.exec('COMMIT');
  55. } catch (e) {
  56. db.exec('ROLLBACK');
  57. console.error('FAILED — rolled back, no changes applied:', e.message);
  58. process.exit(1);
  59. }
  60. console.log(`\nDONE. Deleted ${deleted} local account(s). They are recreated via BizGaze on next sign-in.`);