1
0

access.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
  2. const ACCESS_TYPE = {
  3. unknown: 0,
  4. ldapAccount: 1,
  5. email: 2,
  6. link: 3,
  7. everyOne: 4
  8. };
  9. const ACCESS_TO = {
  10. unknown: 0,
  11. item: 1,
  12. tag: 2,
  13. meta: 3,
  14. everything: 4,
  15. admin: 5
  16. };
  17. const ACCESS_GRANT = {
  18. none: 0,
  19. read: 1,
  20. write: 2,
  21. readNoMeta: 3
  22. };
  23. function AccessModel() {
  24. DatabaseModel.call(this);
  25. this.id = null;
  26. this.type = ACCESS_TYPE.unknown;
  27. this.typeData = "";
  28. this.typeLabel = "";
  29. this.accessTo = ACCESS_TO.unknown;
  30. this.accessToData = "";
  31. this.accessToDataDeserialized = null;
  32. this.grant = ACCESS_GRANT.none;
  33. }
  34. AccessModel.prototype = Object.create(DatabaseModel.prototype);
  35. AccessModel.prototype.getTableName = function() {
  36. return "access";
  37. }
  38. AccessModel.prototype.createOrUpdateBase = async function(dbHelper) {
  39. await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'access' (
  40. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  41. type TINYINT NOT NULL,
  42. typeData STRING NOT NULL,
  43. accessTo TINYINT NOT NULL,
  44. accessToData STRING NOT NULL,
  45. grant TINYINT NOT NULL
  46. )`);
  47. try { await dbHelper.runSql("ALTER TABLE 'access' ADD COLUMN typeLabel STRING"); } catch (err) {}
  48. if (!((await dbHelper.fetch(AccessModel, { accessTo: ACCESS_TO.admin }) || []).length)) {
  49. let freeAdminAccess = new AccessModel();
  50. freeAdminAccess.type = ACCESS_TYPE.everyOne;
  51. freeAdminAccess.accessTo = ACCESS_TO.admin;
  52. await dbHelper.insertOne(freeAdminAccess);
  53. console.log("No Admin access detected ! Creating open admin access");
  54. }
  55. }
  56. AccessModel.prototype.describe = function() {
  57. return {
  58. "id": this.id,
  59. "type": this.type,
  60. "typeLabel": this.typeLabel,
  61. "typeData": this.typeData,
  62. "accessTo": this.accessTo,
  63. "accessToData": this.accessToData,
  64. "grant": this.grant
  65. };
  66. }
  67. AccessModel.prototype.versionColumn = function() { return ""; }
  68. AccessModel.prototype.fromDb = function(dbObj) {
  69. this.id = dbObj["id"];
  70. this.type = dbObj["type"];
  71. this.typeLabel = dbObj["typeLabel"];
  72. this.typeData = dbObj["typeData"];
  73. this.accessTo = dbObj["accessTo"];
  74. this.accessToData = dbObj["accessToData"];
  75. try {
  76. if (this.accessTo === ACCESS_TO.meta)
  77. this.accessToDataDeserialized = JSON.parse(this.accessToData);
  78. }
  79. catch (err) {}
  80. this.grant = dbObj["grant"];
  81. }
  82. module.exports.AccessModel = AccessModel;
  83. module.exports.ACCESS_TYPE = ACCESS_TYPE;
  84. module.exports.ACCESS_TO = ACCESS_TO;
  85. module.exports.ACCESS_GRANT = ACCESS_GRANT;