76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
//Main API for managing user permissions database
|
|
const express=require('express');
|
|
const cors= require('cors');
|
|
const bodyParser= require('body-parser');
|
|
const knex= require ('knex');
|
|
const { check, validationResult } = require('express-validator');
|
|
|
|
const app=express();
|
|
//Enabling CORS
|
|
app.use(cors());
|
|
//Enabling req body parsing
|
|
app.use(bodyParser.json());
|
|
//Connection with database -locally
|
|
const db = knex ({
|
|
client: 'pg',
|
|
connection: {
|
|
host : '127.0.0.1',
|
|
user : 'postgres',
|
|
password : '0904',
|
|
database : 'testdb'
|
|
}
|
|
});
|
|
|
|
//Requiremenst for JSON endpoint calls
|
|
const addusertogroup= require('./endpoints/addusertogroup');
|
|
const cleargroup= require('./endpoints/cleargroup');
|
|
const addpermission= require('./endpoints/addpermission');
|
|
|
|
//Checking basics
|
|
app.get('/', (req, res) => {
|
|
res.send("Work in progress...");
|
|
})
|
|
|
|
//Call for adding a user to a group
|
|
app.post('/addusertogroup', [
|
|
//Simple input validation with express-validator
|
|
check('reqgroup').isAlphanumeric().isLength({min:3}),
|
|
check('requser').isAlphanumeric().isLength({min:3})
|
|
], (req, res) => {
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) {
|
|
return res.status(422).json('Incorrect input data. Can not add an user.');
|
|
}
|
|
addusertogroup.handleAddUserToGroup(req, res, db)});
|
|
|
|
//Call for deleting all users from group
|
|
app.delete('/cleargroup', (req,res) => {
|
|
cleargroup.handleClearGroup(req, res, db)
|
|
});
|
|
|
|
//Call for adding a permission to a user or group
|
|
app.post('/addpermission', [
|
|
//Simple input validation with express-validator
|
|
check('reqowner').isAlphanumeric().isLength({min:3}),
|
|
check('reqobjname').isAlphanumeric().isLength({min:3}),
|
|
check('reqtype').isAlphanumeric().isLength({min:3}),
|
|
check('reqownertype').custom((value, {req, loc, pah}) => {
|
|
if (value.toLowerCase()==='group' || value.toLowerCase()==='user') {
|
|
return value;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
})
|
|
], (req, res) => {
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) {
|
|
return res.status(422).json('Incorrect input data. Can not add a permission.');
|
|
}
|
|
addpermission.handleAddPermission(req, res, db)});
|
|
|
|
|
|
//Listening on local port 3000 -temporary, to be changed to env Var
|
|
app.listen(3000, () => {
|
|
console.log(`App is running on port 3000`);
|
|
}) |