2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-09-12 01:27:04 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
const User = {
|
2023-09-28 23:00:03 +02:00
|
|
|
create: async function ({ username, password, role = "default" }) {
|
2023-12-05 18:13:06 +01:00
|
|
|
const passwordCheck = this.checkPasswordComplexity(password);
|
|
|
|
if (!passwordCheck.checkedOK) {
|
|
|
|
return { user: null, error: passwordCheck.error };
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
2023-12-05 18:13:06 +01:00
|
|
|
const bcrypt = require("bcrypt");
|
2023-09-28 23:00:03 +02:00
|
|
|
const hashedPassword = bcrypt.hashSync(password, 10);
|
|
|
|
const user = await prisma.users.create({
|
|
|
|
data: {
|
|
|
|
username,
|
|
|
|
password: hashedPassword,
|
|
|
|
role,
|
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
});
|
2023-09-28 23:00:03 +02:00
|
|
|
return { user, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO CREATE USER.", error.message);
|
|
|
|
return { user: null, error: error.message };
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
update: async function (userId, updates = {}) {
|
|
|
|
try {
|
2023-12-05 18:13:06 +01:00
|
|
|
// Rehash new password if it exists as update field
|
|
|
|
if (updates.hasOwnProperty("password")) {
|
|
|
|
const passwordCheck = this.checkPasswordComplexity(updates.password);
|
|
|
|
if (!passwordCheck.checkedOK) {
|
|
|
|
return { success: false, error: passwordCheck.error };
|
|
|
|
}
|
|
|
|
|
|
|
|
const bcrypt = require("bcrypt");
|
2023-11-27 19:47:07 +01:00
|
|
|
updates.password = bcrypt.hashSync(updates.password, 10);
|
|
|
|
} else {
|
|
|
|
delete updates.password;
|
|
|
|
}
|
|
|
|
|
2023-11-14 23:43:40 +01:00
|
|
|
await prisma.users.update({
|
2023-09-28 23:00:03 +02:00
|
|
|
where: { id: parseInt(userId) },
|
|
|
|
data: updates,
|
|
|
|
});
|
|
|
|
return { success: true, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { success: false, error: error.message };
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
2023-09-28 23:00:03 +02:00
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const user = await prisma.users.findFirst({ where: clause });
|
|
|
|
return user ? { ...user } : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.users.count({ where: clause });
|
|
|
|
return count;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
delete: async function (clause = {}) {
|
|
|
|
try {
|
2023-10-23 22:10:34 +02:00
|
|
|
await prisma.users.deleteMany({ where: clause });
|
2023-09-28 23:00:03 +02:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit = null) {
|
|
|
|
try {
|
|
|
|
const users = await prisma.users.findMany({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
});
|
|
|
|
return users;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
},
|
2023-12-05 18:13:06 +01:00
|
|
|
|
|
|
|
checkPasswordComplexity: function (passwordInput = "") {
|
|
|
|
const passwordComplexity = require("joi-password-complexity");
|
|
|
|
// Can be set via ENV variable on boot. No frontend config at this time.
|
|
|
|
// Docs: https://www.npmjs.com/package/joi-password-complexity
|
|
|
|
const complexityOptions = {
|
|
|
|
min: process.env.PASSWORDMINCHAR || 8,
|
|
|
|
max: process.env.PASSWORDMAXCHAR || 250,
|
|
|
|
lowerCase: process.env.PASSWORDLOWERCASE || 0,
|
|
|
|
upperCase: process.env.PASSWORDUPPERCASE || 0,
|
|
|
|
numeric: process.env.PASSWORDNUMERIC || 0,
|
|
|
|
symbol: process.env.PASSWORDSYMBOL || 0,
|
|
|
|
// reqCount should be equal to how many conditions you are testing for (1-4)
|
|
|
|
requirementCount: process.env.PASSWORDREQUIREMENTS || 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const complexityCheck = passwordComplexity(
|
|
|
|
complexityOptions,
|
|
|
|
"password"
|
|
|
|
).validate(passwordInput);
|
|
|
|
if (complexityCheck.hasOwnProperty("error")) {
|
|
|
|
let myError = "";
|
|
|
|
let prepend = "";
|
|
|
|
for (let i = 0; i < complexityCheck.error.details.length; i++) {
|
|
|
|
myError += prepend + complexityCheck.error.details[i].message;
|
|
|
|
prepend = ", ";
|
|
|
|
}
|
|
|
|
return { checkedOK: false, error: myError };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { checkedOK: true, error: "No error." };
|
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { User };
|