Report templates ported to ReactJS and Knex.
Does not run yet because reports have dependencies on the old report templates.
This commit is contained in:
parent
be7da791db
commit
d4cea46f07
29 changed files with 807 additions and 688 deletions
|
@ -3,11 +3,13 @@
|
|||
const passport = require('./passport');
|
||||
const config = require('config');
|
||||
|
||||
function _getConfig() {
|
||||
function _getConfig(context) {
|
||||
return {
|
||||
authMethod: passport.authMethod,
|
||||
isAuthMethodLocal: passport.isAuthMethodLocal,
|
||||
externalPasswordResetLink: config.ldap.passwordresetlink
|
||||
externalPasswordResetLink: config.ldap.passwordresetlink,
|
||||
language: config.language || 'en',
|
||||
userId: context.user ? context.user.id : undefined
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +19,7 @@ function registerRootRoute(router, title, entryPoint) {
|
|||
title,
|
||||
reactEntryPoint: entryPoint,
|
||||
reactCsrfToken: req.csrfToken(),
|
||||
mailtrainConfig: JSON.stringify(_getConfig())
|
||||
mailtrainConfig: JSON.stringify(_getConfig(req.context))
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,161 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
const db = require('../db');
|
||||
const tableHelpers = require('../table-helpers');
|
||||
const tools = require('../tools');
|
||||
const _ = require('../translate')._;
|
||||
|
||||
const allowedKeys = ['name', 'description', 'mime_type', 'user_fields', 'js', 'hbs'];
|
||||
|
||||
module.exports.list = (start, limit, callback) => {
|
||||
tableHelpers.list('report_templates', ['*'], 'name', null, start, limit, callback);
|
||||
};
|
||||
|
||||
module.exports.quicklist = callback => {
|
||||
tableHelpers.quicklist('report_templates', ['id', 'name'], 'name', callback);
|
||||
};
|
||||
|
||||
module.exports.filter = (request, callback) => {
|
||||
tableHelpers.filter('report_templates', ['*'], request, ['#', 'name', 'description', 'created'], ['name'], 'created DESC', null, callback);
|
||||
};
|
||||
|
||||
module.exports.get = (id, callback) => {
|
||||
id = Number(id) || 0;
|
||||
|
||||
if (id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('SELECT * FROM report_templates WHERE id=?', [id], (err, rows) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!rows || !rows.length) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
const template = tools.convertKeys(rows[0]);
|
||||
|
||||
const userFields = template.userFields.trim();
|
||||
if (userFields !== '') {
|
||||
try {
|
||||
template.userFieldsObject = JSON.parse(userFields);
|
||||
} catch (err) {
|
||||
// This is to handle situation when for some reason we get corrupted JSON in the DB.
|
||||
template.userFieldsObject = {};
|
||||
template.userFields = '{}';
|
||||
}
|
||||
} else {
|
||||
template.userFieldsObject = {};
|
||||
}
|
||||
|
||||
return callback(null, template);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.createOrUpdate = (createMode, data, callback) => {
|
||||
data = data || {};
|
||||
|
||||
const id = 'id' in data ? Number(data.id) : 0;
|
||||
|
||||
if (!createMode && id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
const template = tools.convertKeys(data);
|
||||
const name = (template.name || '').toString().trim();
|
||||
|
||||
if (!name) {
|
||||
return callback(new Error(_('Report template name must be set')));
|
||||
}
|
||||
|
||||
const keys = ['name'];
|
||||
const values = [name];
|
||||
|
||||
Object.keys(template).forEach(key => {
|
||||
let value = typeof template[key] === 'number' ? template[key] : (template[key] || '').toString().trim();
|
||||
key = tools.toDbKey(key);
|
||||
|
||||
if (key === 'description') {
|
||||
value = tools.purifyHTML(value);
|
||||
}
|
||||
|
||||
if (key === 'user_fields') {
|
||||
value = value.trim();
|
||||
|
||||
if (value !== '') {
|
||||
try {
|
||||
JSON.parse(value);
|
||||
} catch (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allowedKeys.indexOf(key) >= 0 && keys.indexOf(key) < 0) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query;
|
||||
|
||||
if (createMode) {
|
||||
query = 'INSERT INTO report_templates (`' + keys.join('`, `') + '`) VALUES (' + values.map(() => '?').join(',') + ')';
|
||||
} else {
|
||||
query = 'UPDATE report_templates SET ' + keys.map(key => '`' + key + '`=?').join(', ') + ' WHERE id=? LIMIT 1';
|
||||
values.push(id);
|
||||
}
|
||||
|
||||
connection.query(query, values, (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (createMode) {
|
||||
return callback(null, result && result.insertId || false);
|
||||
} else {
|
||||
return callback(null, result && result.affectedRows || false);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.delete = (id, callback) => {
|
||||
id = Number(id) || 0;
|
||||
|
||||
if (id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('DELETE FROM report_templates WHERE id=? LIMIT 1', [id], (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const affected = result && result.affectedRows || 0;
|
||||
return callback(err, affected);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -126,7 +126,7 @@ if (config.ldap.enabled && LdapStrategy) {
|
|||
}
|
||||
})));
|
||||
|
||||
passport.serializeUser((user, done) => { /* FIXME */ console.log(user); done(null, user); });
|
||||
passport.serializeUser((user, done) => done(null, user));
|
||||
passport.deserializeUser((user, done) => done(null, user));
|
||||
|
||||
} else {
|
||||
|
|
50
lib/permissions.js
Normal file
50
lib/permissions.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
|
||||
class ListPermission {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
this.entityType = 'list';
|
||||
}
|
||||
}
|
||||
|
||||
class NamespacePermission {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
this.entityType = 'namespace';
|
||||
}
|
||||
}
|
||||
|
||||
const ListPermissions = {
|
||||
View: new ListPermissions('view')
|
||||
};
|
||||
|
||||
const NamespacePermissions = {
|
||||
View: new NamespacePermission('view'),
|
||||
Edit: new NamespacePermission('edit'),
|
||||
Create: new NamespacePermission('create'),
|
||||
Delete: new NamespacePermission('delete'),
|
||||
CreateList: new NamespacePermission('create list')
|
||||
};
|
||||
|
||||
async function can(context, operation, entityId) {
|
||||
if (!context.user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const result = await knex('permissions_' + operation.entityType).select(['id']).where({
|
||||
entity: entityId,
|
||||
user: context.user.id,
|
||||
operation: operation.name
|
||||
}).first();
|
||||
|
||||
return !!result;
|
||||
}
|
||||
|
||||
async function buildPermissions() {
|
||||
|
||||
}
|
||||
|
||||
can(ctx, ListPermissions.View, 3)
|
||||
can(ctx, NamespacePermissions.CreateList, 2)
|
Loading…
Add table
Add a link
Reference in a new issue