Introduced help fnc for database
This commit is contained in:
@@ -1,9 +1,55 @@
|
|||||||
//Call for adding a permission to a group or user
|
//Call for adding a permission to a group or user
|
||||||
//
|
//
|
||||||
|
//Requiremenst for inserting data in DB
|
||||||
|
const adduser= require('../helpfunctions/adduser');
|
||||||
|
const addgroup= require('../helpfunctions/addgroup');
|
||||||
|
const addobject= require('../helpfunctions/addobject');
|
||||||
|
|
||||||
const handleAddPermission = (req, res, db) => {
|
const handleAddPermission = (req, res, db) => {
|
||||||
//Parsing req body
|
//Parsing req body
|
||||||
const { reqowner, reqownertype, reqobjname, reqtype} =req.body;
|
const { reqowner, reqownertype, reqobjname, reqtype} =req.body;
|
||||||
res.json('Trying to add a permission');
|
//Checking if req input combination existis in DB table permissions
|
||||||
|
//if yes returns error if no inserts permission in DB table
|
||||||
|
db.transaction (trx => {
|
||||||
|
trx.select('objname')
|
||||||
|
.from('permissions')
|
||||||
|
.where('owner', 'ilike', reqowner)
|
||||||
|
.andWhere('ownertype','ilike', reqownertype)
|
||||||
|
.andWhere('objname', 'ilike', reqobjname)
|
||||||
|
.andWhere('type', 'ilike', reqtype)
|
||||||
|
.then( data => {
|
||||||
|
if (data.length===0) {
|
||||||
|
return trx('permissions')
|
||||||
|
.returning('*')
|
||||||
|
.insert({
|
||||||
|
owner: reqowner,
|
||||||
|
ownertype: reqownertype,
|
||||||
|
objname: reqobjname,
|
||||||
|
type: reqtype
|
||||||
|
})
|
||||||
|
.into('permissions')
|
||||||
|
.then( permission => {
|
||||||
|
res.json(permission[0]);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return res.status(400).json('This permission already exists.');
|
||||||
|
})
|
||||||
|
.then(trx.commit)
|
||||||
|
.catch(trx.rollback)
|
||||||
|
.catch (err => res.status(400).json('Error accesing database.'))
|
||||||
|
})
|
||||||
|
if (reqownertype.toLowerCase()==='user') {
|
||||||
|
//Inserts new user (implicitly) if needed
|
||||||
|
adduser.handleAddUser(reqowner, db);
|
||||||
|
}
|
||||||
|
if (reqownertype.toLowerCase()==='group') {
|
||||||
|
//Inserts new group (implicitly) if needed
|
||||||
|
addgroup.handleAddGroup(reqowner, db);
|
||||||
|
}
|
||||||
|
//Inserts new object (implicitly) if needed
|
||||||
|
addobject.handleAddObject(reqobjname, db);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
//Call for adding a user to a group
|
//Call for adding a user to a group
|
||||||
//
|
//
|
||||||
|
//Requiremenst for inserting data in DB
|
||||||
|
const adduser= require('../helpfunctions/adduser');
|
||||||
|
|
||||||
const handleAddUserToGroup = (req, res, db) => {
|
const handleAddUserToGroup = (req, res, db) => {
|
||||||
//Parsing req body
|
//Parsing req body
|
||||||
const { reqgroup, requser} =req.body;
|
const { reqgroup, requser} =req.body;
|
||||||
@@ -29,27 +32,10 @@ const handleAddUserToGroup = (req, res, db) => {
|
|||||||
.catch(trx.rollback)
|
.catch(trx.rollback)
|
||||||
.catch (err => res.status(400).json('Error accesing database.'))
|
.catch (err => res.status(400).json('Error accesing database.'))
|
||||||
})
|
})
|
||||||
//Checking if user existis in DB table users
|
|
||||||
//if no inserts new user (implicitly)
|
//Inserts new user (implicitly) if needed
|
||||||
db.transaction (trx => {
|
adduser.handleAddUser(requser, db);
|
||||||
trx.select('username')
|
|
||||||
.from('users')
|
|
||||||
.where('username', 'ilike', requser)
|
|
||||||
.then ( data => {
|
|
||||||
if (data.length===0) {
|
|
||||||
return trx('users')
|
|
||||||
.returning('*')
|
|
||||||
.insert( {
|
|
||||||
username: requser
|
|
||||||
})
|
|
||||||
.then(user => {
|
|
||||||
console.log('Inserted new user implicitly.');
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(trx.commit)
|
|
||||||
.catch(trx.rollback)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
27
helpfunctions/addgroup.js
Normal file
27
helpfunctions/addgroup.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
//Checking if group existis in DB table groups
|
||||||
|
//if no inserts new group with no users (implicitly)
|
||||||
|
const handleAddGroup = (reqgroup, db) => {
|
||||||
|
db.transaction (trx => {
|
||||||
|
trx.select('groupname')
|
||||||
|
.from('groups')
|
||||||
|
.where('groupname', 'ilike', reqgroup)
|
||||||
|
.then ( data => {
|
||||||
|
if (data.length===0) {
|
||||||
|
return trx('groups')
|
||||||
|
.returning('*')
|
||||||
|
.insert( {
|
||||||
|
groupname: reqgroup,
|
||||||
|
username: ''
|
||||||
|
})
|
||||||
|
.then(group => {
|
||||||
|
console.log('Inserted new empty group implicitly.');
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(trx.commit)
|
||||||
|
.catch(trx.rollback)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
handleAddGroup
|
||||||
|
}
|
||||||
26
helpfunctions/addobject.js
Normal file
26
helpfunctions/addobject.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//Checking if object existis in DB table objects
|
||||||
|
//if no inserts new object (implicitly)
|
||||||
|
const handleAddObject = (reqobjname, db) => {
|
||||||
|
db.transaction (trx => {
|
||||||
|
trx.select('objname')
|
||||||
|
.from('objects')
|
||||||
|
.where('objname', 'ilike', reqobjname)
|
||||||
|
.then ( data => {
|
||||||
|
if (data.length===0) {
|
||||||
|
return trx('objects')
|
||||||
|
.returning('*')
|
||||||
|
.insert( {
|
||||||
|
objname: reqobjname
|
||||||
|
})
|
||||||
|
.then(obj => {
|
||||||
|
console.log('Inserted new object implicitly.');
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(trx.commit)
|
||||||
|
.catch(trx.rollback)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
handleAddObject
|
||||||
|
}
|
||||||
26
helpfunctions/adduser.js
Normal file
26
helpfunctions/adduser.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//Checking if user existis in DB table users
|
||||||
|
//if no inserts new user (implicitly)
|
||||||
|
const handleAddUser = (requser, db) => {
|
||||||
|
db.transaction (trx => {
|
||||||
|
trx.select('username')
|
||||||
|
.from('users')
|
||||||
|
.where('username', 'ilike', requser)
|
||||||
|
.then ( data => {
|
||||||
|
if (data.length===0) {
|
||||||
|
return trx('users')
|
||||||
|
.returning('*')
|
||||||
|
.insert( {
|
||||||
|
username: requser
|
||||||
|
})
|
||||||
|
.then(user => {
|
||||||
|
console.log('Inserted new user implicitly.');
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(trx.commit)
|
||||||
|
.catch(trx.rollback)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
handleAddUser
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user