2021-11-25 00:56:04 +00:00
|
|
|
const database = require('./../database');
|
|
|
|
const passport = require('passport');
|
2021-11-25 03:58:49 +00:00
|
|
|
const localStrategy = require('passport-local').Strategy;
|
2021-11-25 01:13:33 +00:00
|
|
|
const bcrypt = require('bcrypt');
|
2021-11-25 00:56:04 +00:00
|
|
|
|
2021-11-25 05:29:29 +00:00
|
|
|
class User {
|
2021-12-02 20:13:14 +00:00
|
|
|
constructor(id, email, isAdmin, name) {
|
2021-11-25 05:29:29 +00:00
|
|
|
this.id = id;
|
|
|
|
this.email = email;
|
|
|
|
this.isAdmin = isAdmin;
|
2021-12-02 20:13:14 +00:00
|
|
|
this.name = name;
|
2021-11-25 05:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:58:49 +00:00
|
|
|
|
2021-11-26 02:21:21 +00:00
|
|
|
async function checkForAdminAccount() {
|
|
|
|
|
|
|
|
const adminUsersQuery = `SELECT *
|
|
|
|
FROM accounts.users
|
|
|
|
WHERE admin = true;`;
|
|
|
|
const adminUsers = await database.executeQuery(adminUsersQuery);
|
|
|
|
|
|
|
|
if(adminUsers.length == 0) {
|
|
|
|
const passwordHash = await generateHash('admin');
|
|
|
|
const createTempAdminQuery = `INSERT INTO accounts.users(email, password, admin)
|
|
|
|
VALUES('admin@example.com', $1, true);`;
|
|
|
|
database.executeQuery(createTempAdminQuery, [passwordHash]);
|
|
|
|
console.log("Created temp admin account 'admin@example.com' with password 'admin'.");
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 21:49:08 +00:00
|
|
|
database.initializationStatus.then(() => checkForAdminAccount());
|
2021-11-26 02:21:21 +00:00
|
|
|
|
|
|
|
|
2021-11-25 03:58:49 +00:00
|
|
|
passport.use(new localStrategy({
|
|
|
|
usernameField: 'email',
|
|
|
|
passwordField: 'password'},
|
|
|
|
(username, password, cb) => {
|
2021-11-25 01:13:33 +00:00
|
|
|
query = `SELECT user_id, email, password, admin
|
2021-11-25 00:56:04 +00:00
|
|
|
FROM accounts.users
|
|
|
|
WHERE email = $1`;
|
2021-11-25 03:58:49 +00:00
|
|
|
database.executeQuery(query, [username])
|
2021-11-25 01:13:33 +00:00
|
|
|
.then(result => {
|
|
|
|
if(result.length > 0) {
|
|
|
|
const first = result[0];
|
|
|
|
const matches = bcrypt.compareSync(password, first[2]);
|
|
|
|
if(matches) {
|
2021-11-25 03:58:49 +00:00
|
|
|
return cb(null, { id: first[0], email: first[1], admin: first[3] })
|
2021-11-25 01:13:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-25 03:58:49 +00:00
|
|
|
return cb(null, false)
|
2021-11-25 01:13:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-25 03:58:49 +00:00
|
|
|
return cb(null, false)
|
2021-11-25 00:56:04 +00:00
|
|
|
}
|
2021-11-25 01:13:33 +00:00
|
|
|
});
|
2021-11-25 00:56:04 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => {
|
|
|
|
done(null, user.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
passport.deserializeUser((id, cb) => {
|
2021-11-25 01:13:33 +00:00
|
|
|
query = `SELECT user_id, email, admin
|
2021-11-25 00:56:04 +00:00
|
|
|
FROM accounts.users
|
2021-11-25 03:58:49 +00:00
|
|
|
WHERE user_id = $1`;
|
2021-11-25 01:13:33 +00:00
|
|
|
database.executeQuery(query, [parseInt(id, 10)])
|
|
|
|
.then(result => {
|
|
|
|
cb(null, result[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-11-25 19:40:19 +00:00
|
|
|
async function generateHash(password) {
|
2021-11-25 19:41:22 +00:00
|
|
|
const salt = bcrypt.genSaltSync();
|
2021-11-25 19:40:19 +00:00
|
|
|
return bcrypt.hashSync(password, salt);
|
|
|
|
}
|
2021-11-25 01:13:33 +00:00
|
|
|
|
2021-12-02 20:13:14 +00:00
|
|
|
async function create(email, password, isAdmin, name) {
|
2021-11-25 19:40:19 +00:00
|
|
|
const hash = await generateHash(password);
|
2021-11-25 01:13:33 +00:00
|
|
|
|
2021-12-02 20:13:14 +00:00
|
|
|
const query = `INSERT INTO accounts.users(email, password, admin, name)
|
|
|
|
VALUES($1, $2, $3, $4)`;
|
|
|
|
await database.executeQuery(query, [email, hash, isAdmin, name]);
|
2021-11-25 01:25:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 20:13:14 +00:00
|
|
|
async function edit(id, email, password, isAdmin, name) {
|
2021-11-25 19:40:19 +00:00
|
|
|
if(password) {
|
|
|
|
const hash = await generateHash(password);
|
|
|
|
|
|
|
|
const query = `UPDATE accounts.users
|
|
|
|
SET email = $2,
|
|
|
|
password = $3,
|
2021-12-02 20:13:14 +00:00
|
|
|
admin = $4,
|
|
|
|
name = $5
|
2021-11-25 19:40:19 +00:00
|
|
|
WHERE user_id = $1;`;
|
2021-12-02 20:13:14 +00:00
|
|
|
await database.executeQuery(query, [id, email, hash, isAdmin, name]);
|
2021-11-25 19:40:19 +00:00
|
|
|
} else {
|
|
|
|
const query = `UPDATE accounts.users
|
|
|
|
SET email = $2,
|
2021-12-02 20:13:14 +00:00
|
|
|
admin = $3,
|
|
|
|
name = $4
|
2021-11-25 19:40:19 +00:00
|
|
|
WHERE user_id = $1;`;
|
2021-12-02 20:13:14 +00:00
|
|
|
await database.executeQuery(query, [id, email, isAdmin, name]);
|
2021-11-25 19:40:19 +00:00
|
|
|
}
|
2021-12-02 20:13:14 +00:00
|
|
|
return new User(id, email, isAdmin, name);
|
2021-11-25 19:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 19:49:31 +00:00
|
|
|
async function remove(id) {
|
|
|
|
const query = `DELETE FROM accounts.users
|
|
|
|
WHERE user_id = $1
|
2021-12-02 20:13:14 +00:00
|
|
|
RETURNING email, admin, name;`;
|
2021-11-25 19:49:31 +00:00
|
|
|
const row = (await database.executeQuery(query, [id]))[0];
|
2021-12-02 20:13:14 +00:00
|
|
|
return new User(id, row[0], row[1], row[2]);
|
2021-11-25 19:49:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 05:29:29 +00:00
|
|
|
async function retrieveAll() {
|
2021-12-02 20:13:14 +00:00
|
|
|
const query = `SELECT user_id, email, admin, name
|
2021-11-25 05:29:29 +00:00
|
|
|
FROM accounts.users
|
2021-12-02 20:13:14 +00:00
|
|
|
ORDER BY name;`;
|
2021-11-25 05:29:29 +00:00
|
|
|
const table = await database.executeQuery(query);
|
|
|
|
|
|
|
|
const accountsList = [];
|
|
|
|
table.forEach((row) => {
|
2021-12-02 20:13:14 +00:00
|
|
|
accountsList.push(new User(row[0], row[1], row[2], row[3]));
|
2021-11-25 05:29:29 +00:00
|
|
|
});
|
|
|
|
return accountsList;
|
|
|
|
}
|
|
|
|
|
2021-11-25 19:04:18 +00:00
|
|
|
async function getFromID(id) {
|
2021-12-02 20:13:14 +00:00
|
|
|
const query = `SELECT user_id, email, admin, name
|
2021-11-25 19:04:18 +00:00
|
|
|
FROM accounts.users
|
|
|
|
WHERE user_id = $1;`;
|
|
|
|
const row = (await database.executeQuery(query, [id]))[0];
|
|
|
|
|
2021-12-02 20:13:14 +00:00
|
|
|
return new User(id, row[1], row[2], row[3]);
|
2021-11-25 19:04:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 19:40:19 +00:00
|
|
|
exports.create = create;
|
|
|
|
exports.edit = edit;
|
2021-11-25 19:49:31 +00:00
|
|
|
exports.remove = remove;
|
2021-11-25 05:29:29 +00:00
|
|
|
exports.retrieveAll = retrieveAll;
|
2021-11-25 19:04:18 +00:00
|
|
|
exports.getFromID = getFromID;
|
2021-11-25 03:58:49 +00:00
|
|
|
exports.passport = passport;
|