generated from azures04/Base-REST-API
Add SQLite database and repositories, remove old routes
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.
This commit is contained in:
75
repositories/licenceRepository.js
Normal file
75
repositories/licenceRepository.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const database = require("../modules/database")
|
||||
|
||||
function create(key, productId, targetChannel) {
|
||||
const query = `
|
||||
INSERT INTO licenses (
|
||||
key,
|
||||
productId,
|
||||
targetChannel
|
||||
)
|
||||
VALUES (
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(key, productId, targetChannel)
|
||||
}
|
||||
|
||||
function findByKey(key) {
|
||||
const query = `
|
||||
SELECT id, key, productId, targetChannel, userId, usedAt
|
||||
FROM licenses
|
||||
WHERE key = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.get(key)
|
||||
}
|
||||
|
||||
function activate(licenseId, userId) {
|
||||
const query = `
|
||||
UPDATE licenses
|
||||
SET userId = ?, usedAt = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND userId IS NULL
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(userId, licenseId)
|
||||
}
|
||||
|
||||
function deactivate(licenseId, userId) {
|
||||
const query = `
|
||||
UPDATE licenses
|
||||
SET userId = ?, usedAt = NULL
|
||||
WHERE id = ? AND userId IS NULL
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(userId, licenseId)
|
||||
}
|
||||
|
||||
function findByProduct(productId) {
|
||||
const query = `
|
||||
SELECT * FROM licenses
|
||||
WHERE productId = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.all(productId)
|
||||
}
|
||||
|
||||
function remove(id) {
|
||||
const query = `
|
||||
DELETE FROM licenses
|
||||
WHERE id = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(id)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
create,
|
||||
remove,
|
||||
activate,
|
||||
findByKey,
|
||||
deactivate,
|
||||
findByProduct,
|
||||
}
|
||||
61
repositories/permissionsRepository.js
Normal file
61
repositories/permissionsRepository.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const database = require("../modules/database")
|
||||
|
||||
function grant(userId, productId, channel, permissionKey) {
|
||||
const query = `
|
||||
INSERT INTO permissions (
|
||||
userId,
|
||||
productId,
|
||||
channel,
|
||||
permissionKey
|
||||
)
|
||||
|
||||
VALUES (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(userId, productId, channel, permissionKey)
|
||||
}
|
||||
|
||||
function revoke(userId, productId, channel, permissionKey) {
|
||||
const query = `
|
||||
DELETE FROM permissions
|
||||
WHERE userId = ?
|
||||
AND productId = ?
|
||||
AND channel = ?
|
||||
AND permissionKey = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(userId, productId, channel, permissionKey)
|
||||
}
|
||||
|
||||
function findByUserAndProduct(userId, productId) {
|
||||
const query = `
|
||||
SELECT channel, permissionKey
|
||||
FROM permissions
|
||||
WHERE userId = ?
|
||||
AND productId = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.all(userId, productId)
|
||||
}
|
||||
|
||||
function revokeAllOnProduct(userId, productId) {
|
||||
const query = `
|
||||
DELETE FROM permissions
|
||||
WHERE userId = ?
|
||||
AND productId = ?
|
||||
`
|
||||
const statement = database.prepare(query)
|
||||
return statement.run(userId, productId)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
grant,
|
||||
revoke,
|
||||
findByUserAndProduct,
|
||||
revokeAllOnProduct
|
||||
};
|
||||
85
repositories/userRepository.js
Normal file
85
repositories/userRepository.js
Normal file
@@ -0,0 +1,85 @@
|
||||
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,
|
||||
}
|
||||
Reference in New Issue
Block a user