Fix merge conflicts

This commit is contained in:
AbegaM
2024-03-04 15:14:11 +03:00
10 changed files with 73 additions and 33 deletions

View File

@@ -82,14 +82,14 @@ NOTE: It is crucial to securely store a copy of the `Access token secret` and `R
### 3. Updating Super Users
To modify user information in a database, you can utilize the `updateuser` command. This command allows you to change a user's `password` and upgrade a normal user to a `superuser`. Below is an example of how to use it:
To modify a superuser information in a database, you can utilize the `updatesuperuser` command. This command allows you to change a superuser's `password` or upgrade/downgrade a normal user to a `superuser`. Below is an example of how to use it:
```
soul --d foobar.db updateuser --id=1 password=<new_password_for_the_user> // Update the password for the user with ID 1
soul --d foobar.db updatesuperuser --id=1 password=<new_password_for_the_user> // Update the password for the superuser with ID 1
soul --d foobar.db updateuser --id=1 --is_superuser=true // Upgrade the user with ID 1 to a superuser
soul --d foobar.db updatesuperuser --id=1 --is_superuser=true // Upgrade the user with ID 1 to a superuser
soul --d foobar.db updateuser --id=1 --is_superuser=false // Revoke the superuser role from the user with ID 1
soul --d foobar.db updatesuperuser --id=1 --is_superuser=false // Revoke the superuser role from the superuser with ID 1
```
## Documentation

View File

@@ -87,20 +87,20 @@ if (process.env.NO_CLI !== 'true') {
type: 'boolean',
demandOption: false,
})
.command('updateuser', 'Update a user', (yargs) => {
.command('updatesuperuser', 'Update a superuser', (yargs) => {
return yargs
.option('id', {
describe: 'The ID of the user you want to update',
describe: 'The ID of the superuser you want to update',
type: 'number',
demandOption: true,
})
.option('password', {
describe: 'The new password for the user you want to update',
describe: 'The new password for the superuser you want to update',
type: 'string',
demandOption: false,
})
.option('is_superuser', {
describe: 'The role of the user you want to update',
describe: 'The role of the superuser you want to update',
type: 'boolean',
demandOption: false,
});

22
src/commands.js Normal file
View File

@@ -0,0 +1,22 @@
const { yargs } = require('./cli');
const { updateSuperuser } = require('./controllers/auth');
const { argv } = yargs;
const runCLICommands = () => {
//If the updatesuperuser command is passed from the CLI execute the updatesuperuser function
if (argv._.includes('updatesuperuser')) {
const { id, password, is_superuser } = argv;
if (!password && !is_superuser) {
console.log(
'Please provide either the --password or --is_superuser flag when using the updateuser command.',
);
process.exit(1);
} else {
updateSuperuser({ id, password, is_superuser });
}
}
};
module.exports = { runCLICommands };

3
src/constants/api.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = {
defaultRoutes: ['_users', '_roles', '_roles_permissions', '_users_roles'],
};

View File

@@ -1,3 +1,5 @@
const dbTables = require('./dbTables');
const apiConstants = require('./api');
const constantRoles = require('./roles');
module.exports = { dbTables };
module.exports = { dbTables, apiConstants, constantRoles };

3
src/constants/roles.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = {
DEFAULT_ROLE: 'default',
};

View File

@@ -1,6 +1,6 @@
const { tableService } = require('../services');
const { rowService } = require('../services');
const { dbTables } = require('../constants');
const { dbTables, constantRoles } = require('../constants');
const config = require('../config');
const {
hashPassword,
@@ -10,6 +10,8 @@ const {
} = require('../utils');
const createDefaultTables = async () => {
let roleId;
// check if the default tables are already created
const roleTable = tableService.checkTableExists('_roles');
const usersTable = tableService.checkTableExists('_users');
@@ -17,27 +19,33 @@ const createDefaultTables = async () => {
tableService.checkTableExists('_roles_permissions');
const usersRolesTable = tableService.checkTableExists('_users_roles');
// create _users table
if (!usersTable) {
// create the _users table
tableService.createTable('_users', dbTables.userSchema);
}
// create _users_roles table
if (!usersRolesTable) {
// create the _users_roles table
tableService.createTable('_users_roles', dbTables.usersRoleSchema);
}
if (!roleTable && !rolesPermissionTable) {
// create _roles table
if (!roleTable) {
// create the _role table
tableService.createTable('_roles', dbTables.roleSchema);
// create a default role in the _roles table
const role = rowService.save({
tableName: '_roles',
fields: { name: 'default' },
fields: { name: constantRoles.DEFAULT_ROLE },
});
const roleId = role.lastInsertRowid;
roleId = role.lastInsertRowid;
}
// create _roles_permissions table
if (!rolesPermissionTable && roleId) {
// create the _roles_permissions table
tableService.createTable(
'_roles_permissions',
@@ -74,7 +82,7 @@ const createDefaultTables = async () => {
}
};
const updateUser = async (fields) => {
const updateSuperuser = async (fields) => {
const { id, password, is_superuser } = fields;
let newHashedPassword, newSalt;
let fieldsString = '';
@@ -265,7 +273,7 @@ const obtainAccessToken = async (req, res) => {
module.exports = {
createDefaultTables,
updateUser,
updateSuperuser,
registerUser,
obtainAccessToken,
};

View File

@@ -18,12 +18,10 @@ const authRoutes = require('./routes/auth');
const swaggerFile = require('./swagger/swagger.json');
const { setupExtensions } = require('./extensions');
const { createDefaultTables, updateUser } = require('./controllers/auth');
const { yargs } = require('./cli');
const { createDefaultTables } = require('./controllers/auth');
const { runCLICommands } = require('./commands');
const app = express();
const { argv } = yargs;
app.get('/health', (req, res) => {
res.send('OK');
});
@@ -84,19 +82,8 @@ if (config.auth) {
);
}
//If the updateuser command is passed from the CLI execute the updateuser function
if (argv._.includes('updateuser')) {
const { id, password, is_superuser } = argv;
if (!password && !is_superuser) {
console.log(
'Please provide either the --password or --is_superuser flag when using the updateuser command.',
);
process.exit(1);
} else {
updateUser({ id, password, is_superuser });
}
}
// If the user has passed custom CLI commands run the command and exit to avoid running the server
runCLICommands();
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
app.use('/api', rootRoutes);

View File

@@ -1,11 +1,20 @@
const config = require('../config');
const { registerUser } = require('../controllers/auth');
const { apiConstants } = require('../constants/');
const processRequest = async (req, res, next) => {
const resource = req.params.name;
const method = req.method;
// If the user sends a request when auth is set to false, throw an error
if (apiConstants.defaultRoutes.includes(resource) && !config.auth) {
return res.status(401).send({
message: 'You can not access this endpoint while AUTH is set to false',
});
}
// Execute user registration function
if (resource === '_users' && method === 'POST') {
if (resource === '_users' && method === 'POST' && config.auth) {
return registerUser(req, res);
}

View File

@@ -241,6 +241,9 @@
"responses": {
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
}
}
},
@@ -277,6 +280,9 @@
"schema": {
"$ref": "#/definitions/InsertRowErrorResponse"
}
},
"401": {
"description": "Unauthorized"
}
}
}