| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
- const ACCESS_TYPE = {
- unknown: 0,
- ldapAccount: 1,
- email: 2,
- link: 3,
- everyOne: 4
- };
- const ACCESS_TO = {
- unknown: 0,
- item: 1,
- tag: 2,
- meta: 3,
- everything: 4,
- admin: 5
- };
- const ACCESS_GRANT = {
- none: 0,
- read: 1,
- write: 2,
- readNoMeta: 3
- };
- function AccessModel() {
- DatabaseModel.call(this);
- this.id = null;
- this.type = ACCESS_TYPE.unknown;
- this.typeData = "";
- this.typeLabel = "";
- this.accessTo = ACCESS_TO.unknown;
- this.accessToData = "";
- this.accessToDataDeserialized = null;
- this.grant = ACCESS_GRANT.none;
- }
- AccessModel.prototype = Object.create(DatabaseModel.prototype);
- AccessModel.prototype.getTableName = function() {
- return "access";
- }
- AccessModel.prototype.createOrUpdateBase = async function(dbHelper) {
- await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'access' (
- id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
- type TINYINT NOT NULL,
- typeData STRING NOT NULL,
- accessTo TINYINT NOT NULL,
- accessToData STRING NOT NULL,
- grant TINYINT NOT NULL
- )`);
- try { await dbHelper.runSql("ALTER TABLE 'access' ADD COLUMN typeLabel STRING"); } catch (err) {}
- if (!((await dbHelper.fetch(AccessModel, { accessTo: ACCESS_TO.admin }) || []).length)) {
- let freeAdminAccess = new AccessModel();
- freeAdminAccess.type = ACCESS_TYPE.everyOne;
- freeAdminAccess.accessTo = ACCESS_TO.admin;
- await dbHelper.insertOne(freeAdminAccess);
- console.log("No Admin access detected ! Creating open admin access");
- }
- }
- AccessModel.prototype.describe = function() {
- return {
- "id": this.id,
- "type": this.type,
- "typeLabel": this.typeLabel,
- "typeData": this.typeData,
- "accessTo": this.accessTo,
- "accessToData": this.accessToData,
- "grant": this.grant
- };
- }
- AccessModel.prototype.versionColumn = function() { return ""; }
- AccessModel.prototype.fromDb = function(dbObj) {
- this.id = dbObj["id"];
- this.type = dbObj["type"];
- this.typeLabel = dbObj["typeLabel"];
- this.typeData = dbObj["typeData"];
- this.accessTo = dbObj["accessTo"];
- this.accessToData = dbObj["accessToData"];
- try {
- if (this.accessTo === ACCESS_TO.meta)
- this.accessToDataDeserialized = JSON.parse(this.accessToData);
- }
- catch (err) {}
- this.grant = dbObj["grant"];
- }
- module.exports.AccessModel = AccessModel;
- module.exports.ACCESS_TYPE = ACCESS_TYPE;
- module.exports.ACCESS_TO = ACCESS_TO;
- module.exports.ACCESS_GRANT = ACCESS_GRANT;
|