Reports halfway through

Datatable now correctly handles the situation when user is not logged in and access protected resources
This commit is contained in:
Tomas Bures 2017-07-09 23:16:47 +02:00
parent aba42d94ac
commit 3f7b428546
28 changed files with 421 additions and 471 deletions

View file

@ -11,60 +11,74 @@ async function list() {
return await knex('namespaces');
}
function hash(ns) {
return hasher.hash(filterObject(ns, allowedKeys));
function hash(entity) {
return hasher.hash(filterObject(entity, allowedKeys));
}
async function getById(nsId) {
const ns = await knex('namespaces').where('id', nsId).first();
if (!ns) {
async function getById(id) {
const entity = await knex('namespaces').where('id', id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
return ns;
return entity;
}
async function create(ns) {
const nsId = await knex('namespaces').insert(filterObject(ns, allowedKeys));
return nsId;
async function create(entity) {
await knex.transaction(async tx => {
const id = await tx('namespaces').insert(filterObject(entity, allowedKeys));
if (entity.parent) {
if (!await tx('namespaces').select(['id']).where('id', entity.parent).first()) {
throw new interoperableErrors.DependencyNotFoundError();
}
}
return id;
});
}
async function updateWithConsistencyCheck(ns) {
enforce(ns.id !== 1 || ns.parent === null, 'Cannot assign a parent to the root namespace.');
async function updateWithConsistencyCheck(entity) {
enforce(entity.id !== 1 || entity.parent === null, 'Cannot assign a parent to the root namespace.');
await knex.transaction(async tx => {
const existingNs = await tx('namespaces').where('id', ns.id).first();
if (!ns) {
const existing = await tx('namespaces').where('id', entity.id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
const existingNsHash = hash(existingNs);
if (existingNsHash != ns.originalHash) {
const existingHash = hash(existing);
if (existingHash != entity.originalHash) {
throw new interoperableErrors.ChangedError();
}
let iter = ns;
let iter = entity;
while (iter.parent != null) {
iter = await tx('namespaces').where('id', iter.parent).first();
if (iter.id == ns.id) {
if (!iter) {
throw new interoperableErrors.DependencyNotFoundError();
}
if (iter.id == entity.id) {
throw new interoperableErrors.LoopDetectedError();
}
}
await tx('namespaces').where('id', ns.id).update(filterObject(ns, allowedKeys));
await tx('namespaces').where('id', entity.id).update(filterObject(entity, allowedKeys));
});
}
async function remove(nsId) {
enforce(nsId !== 1, 'Cannot delete the root namespace.');
async function remove(id) {
enforce(id !== 1, 'Cannot delete the root namespace.');
await knex.transaction(async tx => {
const childNs = await tx('namespaces').where('parent', nsId).first();
const childNs = await tx('namespaces').where('parent', id).first();
if (childNs) {
throw new interoperableErrors.ChildDetectedError();
}
await tx('namespaces').where('id', nsId).del();
await tx('namespaces').where('id', id).del();
});
}

View file

@ -8,37 +8,37 @@ const interoperableErrors = require('../shared/interoperable-errors');
const allowedKeys = new Set(['name', 'description', 'mime_type', 'user_fields', 'js', 'hbs']);
function hash(ns) {
return hasher.hash(filterObject(ns, allowedKeys));
function hash(entity) {
return hasher.hash(filterObject(entity, allowedKeys));
}
async function getById(templateId) {
const template = await knex('report_templates').where('id', templateId).first();
if (!template) {
async function getById(id) {
const entity = await knex('report_templates').where('id', id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
return template;
return entity;
}
async function listDTAjax(params) {
return await dtHelpers.ajaxList(params, tx => tx('report_templates'), ['report_templates.id', 'report_templates.name', 'report_templates.description', 'report_templates.created']);
}
async function create(template) {
const templateId = await knex('report_templates').insert(filterObject(template, allowedKeys));
return templateId;
async function create(entity) {
const id = await knex('report_templates').insert(filterObject(entity, allowedKeys));
return id;
}
async function updateWithConsistencyCheck(template) {
await knex.transaction(async tx => {
const existingTemplate = await tx('report_templates').where('id', template.id).first();
const existing = await tx('report_templates').where('id', template.id).first();
if (!template) {
throw new interoperableErrors.NotFoundError();
}
const existingNsHash = hash(existingTemplate);
if (existingNsHash != template.originalHash) {
const existingHash = hash(existing);
if (existingHash != template.originalHash) {
throw new interoperableErrors.ChangedError();
}
@ -46,8 +46,8 @@ async function updateWithConsistencyCheck(template) {
});
}
async function remove(templateId) {
await knex('report_templates').where('id', templateId).del();
async function remove(id) {
await knex('report_templates').where('id', id).del();
}
module.exports = {

View file

@ -6,55 +6,93 @@ const { enforce, filterObject } = require('../lib/helpers');
const dtHelpers = require('../lib/dt-helpers');
const interoperableErrors = require('../shared/interoperable-errors');
const allowedKeys = new Set(['name', 'description', 'mime_type', 'user_fields', 'js', 'hbs']);
const allowedKeys = new Set(['name', 'description', 'report_template', 'params']);
function hash(ns) {
return hasher.hash(filterObject(ns, allowedKeys));
const ReportState = {
SCHEDULED: 0,
PROCESSING: 1,
FINISHED: 2,
FAILED: 3,
MAX: 4
};
function hash(entity) {
return hasher.hash(filterObject(entity, allowedKeys));
}
async function getById(templateId) {
const template = await knex('report_templates').where('id', templateId).first();
if (!template) {
async function getById(id) {
const entity = await knex('reports').where('id', id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
return template;
return entity;
}
async function listDTAjax(params) {
return await dtHelpers.ajaxList(params, tx => tx('report_templates'), ['report_templates.id', 'report_templates.name', 'report_templates.description', 'report_templates.created']);
return await dtHelpers.ajaxList(params, tx => tx('reports').innerJoin('report_templates', 'reports.report_template', 'report_templates.id'), ['reports.id', 'reports.name', 'report_templates.name', 'reports.description', 'reports.last_run', 'reports.state']);
}
async function create(template) {
const templateId = await knex('report_templates').insert(filterObject(template, allowedKeys));
return templateId;
}
async function updateWithConsistencyCheck(template) {
async function create(entity) {
await knex.transaction(async tx => {
const existingTemplate = await tx('report_templates').where('id', template.id).first();
if (!template) {
throw new interoperableErrors.NotFoundError();
const id = await tx('reports').insert(filterObject(entity, allowedKeys));
if (!await tx('report_templates').select(['id']).where('id', entity.report_template).first()) {
throw new interoperableErrors.DependencyNotFoundError();
}
const existingNsHash = hash(existingTemplate);
if (existingNsHash != template.originalHash) {
throw new interoperableErrors.ChangedError();
}
await tx('report_templates').where('id', template.id).update(filterObject(template, allowedKeys));
return id;
});
}
async function remove(templateId) {
await knex('report_templates').where('id', templateId).del();
async function updateWithConsistencyCheck(entity) {
await knex.transaction(async tx => {
const existing = await tx('reports').where('id', entity.id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
const existingHash = hash(existing);
if (existingHash != entity.originalHash) {
throw new interoperableErrors.ChangedError();
}
if (!await tx('report_templates').select(['id']).where('id', entity.report_template).first()) {
throw new interoperableErrors.DependencyNotFoundError();
}
await tx('reports').where('id', entity.id).update(filterObject(entity, allowedKeys));
});
}
async function remove(id) {
await knex('reports').where('id', id).del();
}
async function updateFields(id, fields) {
return await knex('reports').where('id', id).update(fields);
}
async function listByState(state, limit) {
return await knex('reports').where('state', state).limit(limit);
}
async function bulkChangeState(oldState, newState) {
return await knex('reports').where('state', oldState).update('state', newState);
}
module.exports = {
ReportState,
hash,
getById,
listDTAjax,
create,
updateWithConsistencyCheck,
remove
remove,
updateFields,
listByState,
bulkChangeState
};

View file

@ -30,8 +30,8 @@ const hashKeys = new Set(['username', 'name', 'email']);
const passport = require('../lib/passport');
function hash(user) {
return hasher.hash(filterObject(user, hashKeys));
function hash(entity) {
return hasher.hash(filterObject(entity, hashKeys));
}
async function _getBy(key, value, extraColumns) {
@ -50,8 +50,8 @@ async function _getBy(key, value, extraColumns) {
return user;
}
async function getById(userId) {
return await _getBy('id', userId);
async function getById(id) {
return await _getBy('id', id);
}
async function serverValidate(data, isOwnAccount) {
@ -309,7 +309,7 @@ async function resetPassword(username, resetToken, password) {
reset_expire: null
});
} else {
throw new interoperableErrors.InvalidToken();
throw new interoperableErrors.InvalidTokenError();
}
});
}