generated from azures04/Base-REST-API
Introduces a new SQLite database setup using better-sqlite3, with initialization scripts and repository modules for users, permissions, and licenses. Removes legacy user and register routes and related test files. Updates dependencies to include better-sqlite3, bcryptjs, and ftp-srv.
85 lines
1.9 KiB
JavaScript
85 lines
1.9 KiB
JavaScript
const database = require("../modules/database")
|
|
|
|
function register(firstName = null, lastName = null, username, hashedPassword) {
|
|
const query = `
|
|
INSERT INTO accounts (
|
|
firstName,
|
|
lastName,
|
|
username,
|
|
password
|
|
)
|
|
|
|
VALUES (
|
|
?,
|
|
?,
|
|
?,
|
|
?
|
|
)
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(firstName, lastName, username, hashedPassword)
|
|
}
|
|
|
|
function remove(id) {
|
|
const query = `
|
|
DELETE FROM accounts
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(id)
|
|
}
|
|
|
|
function changePassword(id, hashedPassword) {
|
|
const query = `
|
|
UPDATE accounts
|
|
SET password = ?
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(id, hashedPassword)
|
|
}
|
|
|
|
function findByUsername(identifier) {
|
|
const query = `
|
|
SELECT id, username, password
|
|
FROM accounts
|
|
WHERE username = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(identifier)
|
|
}
|
|
|
|
function findById(identifier) {
|
|
const query = `
|
|
SELECT id, firstName, lastName, username, createdAt
|
|
FROM accounts
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(identifier)
|
|
}
|
|
|
|
function hasPermission(userId, productName, channel, permission) {
|
|
const query = `
|
|
SELECT 1 FROM permissions p
|
|
JOIN products pr ON p.product_id = pr.id
|
|
WHERE p.user_id = ?
|
|
AND pr.name = ?
|
|
AND (p.channel = ? OR p.channel = '*')
|
|
AND p.permission_key = ?
|
|
`
|
|
|
|
const stmt = db.prepare(query)
|
|
const result = stmt.get(userId, productName, channel, permission)
|
|
|
|
return !!result
|
|
}
|
|
|
|
module.exports = {
|
|
remove,
|
|
register,
|
|
findById,
|
|
hasPermission,
|
|
findByUsername,
|
|
changePassword,
|
|
} |