initial permission supprt - fixes #10
parent
b17fc2dc6c
commit
5d4a7385a6
@ -0,0 +1,50 @@
|
||||
const permissions = module.exports = {};
|
||||
|
||||
permissions.eval = (mxid, expansion, db) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let accessGranted = false;
|
||||
|
||||
db.each('SELECT omni, project, service, subcommand, args \
|
||||
FROM permissions \
|
||||
WHERE mxid=?',
|
||||
[mxid],
|
||||
(err, row) => { // callback for each returned row
|
||||
// check for omnipotence
|
||||
if(row.omni === 1) {
|
||||
accessGranted = true;
|
||||
return;
|
||||
};
|
||||
|
||||
// check for project permissions
|
||||
const regexProject = new RegExp(row.project);
|
||||
if(!regexProject.test(expansion.project)) {
|
||||
return;
|
||||
};
|
||||
// check for service permissions
|
||||
const regexService = new RegExp(row.service);
|
||||
if(!regexService.test(expansion.service)) {
|
||||
return;
|
||||
};
|
||||
// check for subcommand permissions
|
||||
const regexSubcommand = new RegExp(row.subcommand);
|
||||
if(!regexSubcommand.test(expansion.subcommand)) {
|
||||
return;
|
||||
};
|
||||
// check for args permissions
|
||||
const regexArgs = new RegExp(row.args);
|
||||
if(regexArgs.test(expansion.args)) {
|
||||
accessGranted = true;
|
||||
return;
|
||||
};
|
||||
},
|
||||
() => { // gets called after all rows are processed
|
||||
if(accessGranted) {
|
||||
resolve('access granted');
|
||||
return;
|
||||
} else {
|
||||
reject('permission denied');
|
||||
return;
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
Reference in New Issue