accounts.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const db = require('../database.js');
  2. const TABLE_NAME = "accounts";
  3. function AccountManager() {
  4. }
  5. AccountManager.prototype.fromGoogleIdAuth = function(id, cb) {
  6. var self = this;
  7. db.database.queryFirst(TABLE_NAME, { authGoogleUserId: id }, (err, result) => {
  8. cb(self.fromDb(result));
  9. });
  10. };
  11. AccountManager.prototype.fromFacebookIdAuth = function(id, cb) {
  12. var self = this;
  13. db.database.queryFirst(TABLE_NAME, { authFacebookUserId: id }, (err, result) => {
  14. cb(self.fromDb(result));
  15. });
  16. };
  17. AccountManager.prototype.fromSlackIdAuth = function(id, cb) {
  18. var self = this;
  19. db.database.queryFirst(TABLE_NAME, { authSlackUserEmailAndTeam: id }, (err, result) => {
  20. cb(self.fromDb(result));
  21. });
  22. };
  23. AccountManager.prototype.fromId = function(id, cb) {
  24. var self = this;
  25. db.database.queryFirst(TABLE_NAME, { id: id }, (err, result) => {
  26. cb(self.fromDb(result));
  27. });
  28. };
  29. AccountManager.prototype.fromDb = function(dbResult) {
  30. if (!dbResult)
  31. return null;
  32. return new Account(dbResult);
  33. };
  34. function Account(dbResult) {
  35. this.id;
  36. this.authGoogleUserId;
  37. this.authFacebookUserId;
  38. this.authSlackUserEmailAndTeam;
  39. this.certificates;
  40. this.cguReadAndAccepted;
  41. this.services;
  42. this.dirty;
  43. if (dbResult) {
  44. this.id = dbResult.id;
  45. this.authSlackUserEmailAndTeam = dbResult.authSlackUserEmailAndTeam;
  46. this.authGoogleUserId = dbResult.authGoogleUserId;
  47. this.authFacebookUserId = dbResult.authFacebookUserId;
  48. this.certificates = dbResult.certificates;
  49. this.cguReadAndAccepted = !!dbResult.cguReadAndAccepted;
  50. this.services = JSON.parse(dbResult.services);
  51. this.permanentPhoneAccess = dbResult.permanentPhoneAccess.split(',').filter(token => token.length);
  52. this.dirty = false;
  53. } else {
  54. this.id = null;
  55. this.authSlackUserEmailAndTeam = null;
  56. this.authGoogleUserId = null;
  57. this.authFacebookUserId = null;
  58. this.createCertificate();
  59. this.cguReadAndAccepted = false;
  60. this.permanentPhoneAccess = [];
  61. this.services = [];
  62. this.dirty = true;
  63. }
  64. }
  65. Account.prototype.toDb = function() {
  66. return {
  67. authGoogleUserId: this.authGoogleUserId
  68. ,authFacebookUserId: this.authFacebookUserId
  69. ,authSlackUserEmailAndTeam: this.authSlackUserEmailAndTeam
  70. ,certificates: this.certificates
  71. ,cguReadAndAccepted: this.cguReadAndAccepted ? 1 : 0
  72. ,services: JSON.stringify(this.services)
  73. ,permanentPhoneAccess: ',' +this.permanentPhoneAccess.join(',') +','
  74. };
  75. };
  76. Account.prototype.createCertificate = function() {
  77. //TODO create certificate
  78. this.certificates = null;
  79. };
  80. Account.prototype.addService = function(serviceType, serviceName, serviceId, oauthParam) {
  81. this.services.push([serviceType, serviceName, serviceId, oauthParam]);
  82. };
  83. Account.prototype.getServices = function() {
  84. var services = {};
  85. this.services.forEach((i) => {
  86. services[i[2]] = {
  87. type: i[0],
  88. name: i[1],
  89. oauthParam: i[3]
  90. };
  91. });
  92. return services;
  93. };
  94. Account.prototype.edit = function() {
  95. this.dirty = true;
  96. return this;
  97. };
  98. AccountManager.prototype.createAccount = function() {
  99. return new Account();
  100. };
  101. AccountManager.prototype.save = function(account, cb) {
  102. if (!account.id) {
  103. // insert and save id
  104. var data = account.toDb();
  105. data.id = null;
  106. db.database.insert(TABLE_NAME, data, (newId) => {
  107. account.id = newId;
  108. account.dirty = false;
  109. if (cb) cb(account);
  110. });
  111. } else if (account.dirty) {
  112. // update
  113. db.database.update(TABLE_NAME, account.id, account.toDb(), () => {
  114. account.dirty = false;
  115. if (cb) cb(account);
  116. });
  117. } else {
  118. if (cb) cb(account);
  119. }
  120. };
  121. module.exports.accountManager = new AccountManager();
  122. module.exports.updateTable = function(dbObject, currentVersion, cb) {
  123. if (!currentVersion) {
  124. console.info("Creating table " +TABLE_NAME);
  125. dbObject.run('CREATE TABLE IF NOT EXISTS `' +TABLE_NAME +'` ('
  126. +"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
  127. +"`authGoogleUserId` STRING UNIQUE,"
  128. +"`authFacebookUserId` STRING UNIQUE,"
  129. +"`authSlackUserEmailAndTeam` STRING UNIQUE,"
  130. +"`certificates` STRING,"
  131. +"`cguReadAndAccepted` BOOLEAN NOT NULL DEFAULT FALSE,"
  132. +"`services` STRING NOT NULL"
  133. +')', cb);
  134. } else if (currentVersion === 2) {
  135. console.info("Updating table " +TABLE_NAME);
  136. dbObject.run('ALTER TABLE `' +TABLE_NAME +'` ADD COLUMN `permanentPhoneAccess` STRING NOT NULL DEFAULT "";', cb);
  137. } else {
  138. cb(null);
  139. }
  140. };