94 lines
2.4 KiB
JavaScript
94 lines
2.4 KiB
JavaScript
/**
|
|
* * Copyright (c) 2016, Intel Corporation.
|
|
*
|
|
* This program is licensed under the terms and conditions of the
|
|
* Apache License, version 2.0. The full text of the Apache License is at
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
/**
|
|
* This class will instantiate the ORM, load in the models, call the method
|
|
* to create db connections, test the connection, then create the tables and
|
|
* relationships if not present
|
|
*/
|
|
const Sequelize = require('sequelize'),
|
|
config = require('config');
|
|
|
|
function init() {
|
|
const dbConfig = {...config.get("db.users")};
|
|
let configPath = process.env.NODE_CONFIG_DIR;
|
|
if (configPath) {
|
|
configPath = configPath.replace(/config/, '');
|
|
} else {
|
|
configPath = './'
|
|
}
|
|
dbConfig.host = `${configPath}${dbConfig.host}`;
|
|
|
|
const db = {
|
|
sequelize: new Sequelize(dbConfig),
|
|
Sequelize: Sequelize
|
|
};
|
|
|
|
return db.sequelize.authenticate().then(function () {
|
|
const User = db.sequelize.define('users', {
|
|
id: {
|
|
type: Sequelize.INTEGER,
|
|
primaryKey: true,
|
|
autoIncrement: true
|
|
},
|
|
displayName: Sequelize.STRING,
|
|
notes: Sequelize.STRING,
|
|
uid: Sequelize.STRING,
|
|
isLDAP: Sequelize.BOOLEAN,
|
|
authToken: Sequelize.STRING,
|
|
authDate: Sequelize.DATE,
|
|
authenticated: Sequelize.BOOLEAN,
|
|
mailVerified: Sequelize.BOOLEAN,
|
|
mail: Sequelize.STRING,
|
|
memberSince: Sequelize.DATE,
|
|
password: Sequelize.STRING, /* SHA hash of user supplied password for !isLDAP users */
|
|
passwordExpires: Sequelize.DATE
|
|
}, {
|
|
timestamps: false
|
|
});
|
|
|
|
const Authentication = db.sequelize.define('authentication', {
|
|
key: {
|
|
type: Sequelize.STRING,
|
|
primaryKey: true,
|
|
allowNull: false
|
|
},
|
|
issued: Sequelize.DATE,
|
|
type: {
|
|
type: Sequelize.ENUM,
|
|
values: [ 'account-setup', 'password-reset' ]
|
|
},
|
|
userId: {
|
|
type: Sequelize.INTEGER,
|
|
allowNull: false,
|
|
references: {
|
|
model: User,
|
|
key: 'id',
|
|
}
|
|
}
|
|
}, {
|
|
timestamps: false
|
|
});
|
|
|
|
return db.sequelize.sync({
|
|
force: false
|
|
}).then(function () {
|
|
return db;
|
|
});
|
|
}).catch(function (error) {
|
|
console.log("ERROR: Failed to authenticate with USER DB");
|
|
console.log("ERROR: " + JSON.stringify(config.get("db"), null, 2));
|
|
console.log(error);
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
module.exports = init();
|