mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 11:07:08 +00:00
Add function to return allowed operations in a given context
This commit is contained in:
1
lib/www/client/source/src/lib/organisations/operations.js
Symbolic link
1
lib/www/client/source/src/lib/organisations/operations.js
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../../../server/lib/organisations/operations.js
|
||||
@@ -1,6 +1,6 @@
|
||||
const { setSurvey, pool } = require('../connection');
|
||||
const { vessel } = require('../vesel');
|
||||
const { access } = require('../../organisations');
|
||||
const { access, operations } = require('../../organisations');
|
||||
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
||||
|
||||
// Cache the per-project organisations access here
|
||||
@@ -62,15 +62,23 @@ async function orgAccess (userOrgs, pid, operation) {
|
||||
return access(userOrgs, itemOrgs, operation);
|
||||
}
|
||||
|
||||
/** Check to which operations the user has access to in the
|
||||
* project given by 'pid`.
|
||||
*
|
||||
* If `pid` is `null`, check against vessel access.
|
||||
*/
|
||||
async function allowedOperations (userOrgs, pid) {
|
||||
const itemOrgs = pid === null
|
||||
? await vesselOrganisations()
|
||||
: await projectOrganisations(pid);
|
||||
|
||||
return operations(userOrgs, itemOrgs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Filter an array of objects by organisation access to a given operation
|
||||
*/
|
||||
function orgFilter (userOrgs, list, operation, fn = (item) => item.organisations ) {
|
||||
console.log("orgFilter");
|
||||
console.log("userOrgs", userOrgs);
|
||||
console.log("list", list);
|
||||
console.log("operation", operation);
|
||||
console.log("fn", fn);
|
||||
return list.filter ( (item) => access(userOrgs, fn(item), operation) );
|
||||
}
|
||||
|
||||
@@ -79,5 +87,6 @@ module.exports = {
|
||||
projectOrganisations,
|
||||
vesselOrganisations,
|
||||
orgAccess,
|
||||
allowedOperations,
|
||||
orgFilter
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = {
|
||||
access: require('./access.js')
|
||||
access: require('./access.js'),
|
||||
operations: require('./operations.js'),
|
||||
};
|
||||
|
||||
46
lib/www/server/lib/organisations/operations.js
Normal file
46
lib/www/server/lib/organisations/operations.js
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
/** List the operations to which the user has access
|
||||
*
|
||||
* @a userOrgs is the user's organisations
|
||||
* @a itemOrgs is the item's organisations
|
||||
*
|
||||
*/
|
||||
function operations (userOrgs = {}, itemOrgs = {}) {
|
||||
// console.log("userOrgs", userOrgs);
|
||||
// console.log("itemOrgs", itemOrgs);
|
||||
const ops = [];
|
||||
|
||||
for (const userOrg in userOrgs) {
|
||||
for (const operation in userOrgs[userOrg]) {
|
||||
if (userOrg in itemOrgs) {
|
||||
// Found an organisation in common between user and project
|
||||
// (there might be many)
|
||||
if (itemOrgs[userOrg][operation] == true && userOrgs[userOrg][operation] == true) {
|
||||
ops.push[operation];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ("*" in userOrgs) {
|
||||
// Aha! A wildcard user
|
||||
// Return true if at least one organisation grants access
|
||||
// to this operation
|
||||
// console.log("Checking via wildcard");
|
||||
for (const operation in userOrgs["*"]) {
|
||||
if (Object.values(itemOrgs).some( org => org[operation] )) {
|
||||
ops.push(operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = operations; // CJS export
|
||||
}
|
||||
|
||||
// ESM export
|
||||
if (typeof exports !== 'undefined' && !exports.default) {
|
||||
exports.default = operations; // ESM export
|
||||
}
|
||||
Reference in New Issue
Block a user