The groupPermissions
function filters and organizes code cell permissions based on provided groups and optional search queries, allowing for fine-grained access control.
npm run import -- "test get permissions"
var path = require('path')
var importer = require('../Core');
var getPermissions = importer.import("rpc permissions");
var {cellCache} = importer.import("path.join(__dirname"))
function groupPermissions(groups, search) {
if(typeof groups === 'string') groups = [groups];
var permissions = getPermissions(!search && groups.includes('Available')
? cellCache.map(i => i.id)
: search);
if(typeof groups !== 'undefined') {
return Object.keys(permissions)
.reduce((obj, p) => groups.filter(g => permissions[p].includes(g)).length === groups.length
? (obj[p] = permissions[p], obj)
: obj, {});
}
// TODO: convert this to pattern utility
return Object.keys(permissions).reduce((types, key) => {
return permissions[key].reduce((types, group) => {
if(typeof types[group] === 'undefined') types[group] = {};
types[group][key] = permissions[key];
}, types)
}, {});
}
module.exports = groupPermissions;
const { read } = require('fs').promises;
const { json } = require('node-json-db');
const { path } = require('path');
const { importModule, getPermissions } = require('../Core');
const cache = json('cache', {
filePath: path.join(__dirname, './cache.ipynb'),
autosave: true
});
async function groupPermissions(groups, search) {
if (typeof groups ==='string') {
groups = [groups];
}
const permissions = await getPermissions(!search && groups.includes('Available')
? await readSync('cache.ipynb')
.then(cache => cache.map(i => i.id))
: search);
const filteredPermissions = permissions.reduce((obj, permission) => {
const groupsFound = groups.filter(group => permission.includes(group));
if (groupsFound.length === groups.length) {
obj[permission] = permission;
}
return obj;
}, {});
return filteredPermissions;
}
async function readSync(file) {
try {
return await read(file);
} catch (error) {
return [];
}
}
module.exports = groupPermissions;
This code defines a function groupPermissions
that filters and organizes permissions for code cells based on provided groups and a search query.
Here's a breakdown:
Initialization:
path
, importer
).getPermissions
function and cellCache
from the importer
.groupPermissions
Function:
groups
(an array or string of permission groups) and search
(an optional search query).search
is not provided and groups
includes "Available", it retrieves all cell IDs from cellCache
.getPermissions
to retrieve permissions for either all cells or cells matching the search
query.groups
is provided, it filters the permissions based on the specified groups, returning only cells with permissions matching all groups.groups
is not provided, it organizes the permissions into a nested object structure, grouping cells by permission group.Output:
In essence, this code provides a way to manage and filter code cell permissions based on predefined groups and search criteria, allowing for granular control over access and functionality.