Added test and query call
This commit is contained in:
@@ -2,10 +2,15 @@
|
||||
//
|
||||
//Requiremenst for inserting data in DB
|
||||
const adduser= require('../helpfunctions/adduser');
|
||||
const addgroup= require('../helpfunctions/addgroup');
|
||||
|
||||
const handleAddUserToGroup = (req, res, db) => {
|
||||
//Parsing req body
|
||||
const { reqgroup, requser} =req.body;
|
||||
//Initializing an empty group if group does not exists.
|
||||
//Initialization needed for group clearing to mantain a group when all users are cleared
|
||||
addgroup.handleAddGroup(reqgroup, db);
|
||||
|
||||
//Checking if user-group combination existis in DB table groups
|
||||
//if yes returns error if no inserts in DB table
|
||||
db.transaction (trx => {
|
||||
|
||||
@@ -4,8 +4,10 @@ const handleClearGroup = (req, res, db) => {
|
||||
//Parsing req body
|
||||
const { reqgroup} =req.body;
|
||||
//Checking for groupname in table groups and deleting users
|
||||
db('groups')
|
||||
db('groups')
|
||||
.where('groupname', 'ilike', reqgroup)
|
||||
//Group initialization is maintained
|
||||
.andWhereNot('username', '')
|
||||
.del()
|
||||
.then (count => {
|
||||
if (count!==0) {
|
||||
|
||||
40
endpoints/querypermiss.js
Normal file
40
endpoints/querypermiss.js
Normal file
@@ -0,0 +1,40 @@
|
||||
//Call for querying what permissions a particular user has over a particular object
|
||||
//
|
||||
|
||||
const handleQuery = (req, res, db) => {
|
||||
//Parsing req body
|
||||
const { requser, reqobject} =req.body;
|
||||
//Checking for permissions in joined tables for cases of directly associated
|
||||
//or inherited permissions
|
||||
db.transaction ( trx => {
|
||||
trx.select('type')
|
||||
.from('groups')
|
||||
.fullOuterJoin('permissions', 'groups.groupname', 'permissions.owner')
|
||||
.where('objname', 'ilike', reqobject)
|
||||
//Check for inherited permissions
|
||||
.andWhere('username', 'ilike', requser)
|
||||
//Check for directly associated permissions
|
||||
.orWhere('owner', 'ilike', requser)
|
||||
.andWhere('ownertype', 'ilike', 'user')
|
||||
.then ( found => {
|
||||
if (found.length!==0) {
|
||||
//Responding with aray of permission types instead of array of objects
|
||||
res.json(found.map( (perm, index) => {
|
||||
return perm.type;
|
||||
}));
|
||||
}
|
||||
else {
|
||||
res.status(400).json('No query object permissions found.')
|
||||
}
|
||||
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback)
|
||||
.catch (err => res.status(400).json('Error accesing database.'))
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
handleQuery
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
//Call for testing if a particular user has a particular permission over a
|
||||
// particular object. Returns true or false.
|
||||
//
|
||||
//Requiremenst for checking data in DB
|
||||
const checkmemb= require('../helpfunctions/checkmemb');
|
||||
|
||||
const condTest = (req, res, db) => {
|
||||
//Parsing req body
|
||||
const { reqowner, reqobjname, reqtype} =req.body;
|
||||
|
||||
//Checking for owner with particular permission over a particular object
|
||||
//Checking for owner with particular permission over a particular object
|
||||
db.transaction (trx => {
|
||||
trx.select('objname')
|
||||
.from('permissions')
|
||||
@@ -22,40 +19,28 @@ const condTest = (req, res, db) => {
|
||||
if (found.length !==0) {
|
||||
return res.json(true);
|
||||
}
|
||||
//Checking for permissions inherited from groups
|
||||
return trx('permissions')
|
||||
.returning('owner')
|
||||
//Checking only for groups
|
||||
.where('ownertype', 'ilike', 'group')
|
||||
//Checking for existance of group with particular permission over a particular object
|
||||
.andWhere('objname', 'ilike', reqobjname)
|
||||
.andWhere('type', 'ilike', reqtype)
|
||||
.then (found => {
|
||||
//?
|
||||
console.log('found', found);
|
||||
//Found is an array of objects (rows from permissions table) where group has needed permisions
|
||||
//Check for every group if user is a member
|
||||
found.forEach( (row, index) => {
|
||||
//?
|
||||
console.log('row.owner', row.owner);
|
||||
// Check if our user is in found group and have inherited permission
|
||||
return trx('groups')
|
||||
.returning('*')
|
||||
.where('username', 'ilike', reqowner)
|
||||
.andWhere('groupname', 'ilike', 'admins')
|
||||
.then ( data => {
|
||||
console.log('data', data);
|
||||
if (data.length!==0) {
|
||||
return res.json(true);
|
||||
}
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback)
|
||||
});
|
||||
return res.json(false);
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback)
|
||||
.catch (err => res.status(400).json('Error accesing database.'))
|
||||
})
|
||||
//Checking for permissions inherited from groups
|
||||
db.transaction ( trx => {
|
||||
trx.select('*')
|
||||
.from('groups')
|
||||
.fullOuterJoin('permissions', 'groups.groupname', 'permissions.owner')
|
||||
//Checking only for groups
|
||||
.where('ownertype', 'ilike', 'group')
|
||||
.andWhere('username', 'ilike', reqowner)
|
||||
.then ( found => {
|
||||
if (found.length !==0) {
|
||||
return res.json(true);
|
||||
}
|
||||
else {
|
||||
//If no searched permission is found then it does not exists
|
||||
return res.json(false);
|
||||
}
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback)
|
||||
.catch (err => res.status(400).json('Error accesing database.'))
|
||||
|
||||
Reference in New Issue
Block a user