Skip to content

Commit

Permalink
[ENG-1037] - Added import and export of groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phara0h committed Sep 30, 2019
1 parent a5dbbc6 commit f6e18bc
Show file tree
Hide file tree
Showing 6 changed files with 709 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .testENV
@@ -1,5 +1,5 @@

DATABASE_URL=postgres://postgres@localhost/travelling
TRAVELLING_DATABASE_URL=postgres://postgres@localhost/travelling
NODE_TLS_REJECT_UNAUTHORIZED=0


Expand Down
99 changes: 95 additions & 4 deletions include/routes/v1/groups.js
Expand Up @@ -30,7 +30,7 @@ function checkCircularGroupRef(group, groups, found = []) {

if (group.inherited) {
for (var i = 0; i < group.inherited.length; i++) {
if (isCircularPath(group.inherited[i], groups[group.inherited[i]], groups, [group.id])) {
if (isCircularPath(group.inherited[i], groups[group.inherited[i]], groups, [group.id || group.name])) {
return true;
}

Expand All @@ -39,7 +39,7 @@ function checkCircularGroupRef(group, groups, found = []) {
return false;
}

async function setGroup(req, group, router) {
async function setGroup(req, group, router, groups = null) {
if (req.body.name) {
if (regex.safeName.exec(req.body.name) == null) {
throw {
Expand Down Expand Up @@ -81,7 +81,7 @@ async function setGroup(req, group, router) {
}
}

if (checkCircularGroupRef(group, await router.getMappedGroups())) {
if (checkCircularGroupRef(group, groups || await router.getMappedGroups())) {
throw {
type: 'group-inherited-circular-error',
msg: 'Inherited groups array contains a cicular ref.',
Expand Down Expand Up @@ -533,12 +533,103 @@ module.exports = function(app, opts, done) {
// import/export Groups

app.put('/groups/import', async (req, res) => {
// Possibly should put a check or lock to stop all group editing until import is done.

var keys = Object.keys(req.body);
var groups = [];
var inheritance = {};
var savedGroups = {};

for (var i = 0; i < keys.length; i++) {
req.body[keys[i]].name = keys[i];
var group = req.body[keys[i]];

try {
if (group.inherited && group.inherited.length > 0) {
inheritance[group.name] = [];
for (var j = 0; j < group.inherited.length; j++) {
inheritance[group.name].push(group.inherited[j]);
}
}
var fgroup = await router.getGroup(group.name);

if (fgroup) {
fgroup._.inherited = null;
fgroup = fgroup._;
} else {
fgroup = {};
}
groups.push(await setGroup({body: group}, new Group(fgroup), router, req.body));
} catch (e) {
res.code(400).send(e);
}
}

for (var i = 0; i < groups.length; i++) {
if (groups[i].id) {
await groups[i].save();
} else {
await groups[i].create();
}

savedGroups[groups[i].name] = groups[i];
}

var inheritanceKeys = Object.keys(inheritance);

var failedInheritance = [];

for (var i = 0; i < inheritanceKeys.length; i++) {
savedGroups[inheritanceKeys[i]].inherited = [];
for (var j = 0; j < inheritance[inheritanceKeys[i]].length; j++) {
if (savedGroups[inheritance[inheritanceKeys[i]][j]]) {
savedGroups[inheritanceKeys[i]].inherited.push(savedGroups[inheritance[inheritanceKeys[i]][j]].id);
} else {
failedInheritance.push({group: inheritanceKeys[i], inheritance: inheritance[inheritanceKeys[i]][j]});
}
}
await savedGroups[inheritanceKeys[i]].save();
}
router.redis.needsGroupUpdate = true;

if (failedInheritance.length > 0) {
res.code(240).send({
type: 'group-export-failed-inheritances',
msg: 'All groups imported but these inheritances were not done since the group names do not exist.',
failedInheritance,
});
return;
}

res.code(200).send();
});

app.put('/groups/export', async (req, res) => {
app.get('/groups/export', async (req, res) => {
var groups = await router.getGroups();
var mappedGroups = await router.getMappedGroups();
var exported = {
// inheritance: {},
};

for (var i = 0; i < groups.length; i++) {
var group = {...groups[i]._};

if (group.inheritedGroups) {
group.inheritedGroups = undefined;
}
if (group.inherited && group.inherited.length > 0) {
// exported.inheritance[group.name] = [];
for (var j = 0; j < group.inherited.length; j++) {
group.inherited[j] = mappedGroups[group.inherited[j]].name; // exported.inheritance[group.name].push(mappedGroups[group.inherited[j]].name);
}

}
group.id = undefined;
exported[group.name] = group;
exported[group.name].name = undefined;
}

res.code(200).send(exported);
});

// Get Users
Expand Down
37 changes: 28 additions & 9 deletions include/utils/logger.js
Expand Up @@ -22,31 +22,31 @@ class Logger {
if (config.log.enable && config.log.colors) {
var color = 'magenta';

switch (level.toLowerCase()) {
case 'info':
switch (level) {
case levels.info:
color = 'white';
break;
case 'warn':
case levels.warn:
color = 'yellow';
break;
case 'error':
case levels.error:
color = 'red';
break;
case 'debug':
case levels.debug:
color = 'blue';
break;
case 'fatal':
case levels.fatal:
color = 'red';
break;
case 'trace':
case levels.trace:
color = 'green';
break;
default:
break;
}
this.logger.log(colors.cyan('Travelling:') + ' [' + colors[color](level.toUpperCase()) + '] ', ...msg);
this.logger.log(colors.cyan('Travelling:') + ' [' + colors[color](this.getLevelString(level)) + '] ', ...msg);
} else if (config.log.enable) {
this.logger.log('Travelling: [' + level.toUpperCase() + '] ', ...msg);
this.logger.log('Travelling: [' + this.getLevelString(level) + '] ', ...msg);
}
}
}
Expand All @@ -70,6 +70,25 @@ class Logger {
this.log(levels.trace, ...msg);
}

getLevelString(level) {
switch (level) {
case levels.info:
return 'INFO';
case levels.warn:
return 'WARN';
case levels.error:
return 'ERROR';
case levels.debug:
return 'DEBUG';
case levels.fatal:
return 'FATAL';
case levels.trace:
return 'TRACE';
default:
break;
}
}

}

var logger = new Logger();
Expand Down

0 comments on commit f6e18bc

Please sign in to comment.