Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit Report or All subscriptions page for administrators and approvers #19

Closed
wants to merge 10 commits into from
6 changes: 5 additions & 1 deletion dao/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ const model = {
client_id: {
optional: true,
property_name: 'clientId'
}
},
api_group: {
property_name: 'api_group',
optional: true
},
},
},

Expand Down
43 changes: 41 additions & 2 deletions dao/postgres/entities/pg-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,58 @@ class PgSubscriptions {
return callback(null, subsList);
});
}

getAllImpl(filter, orderBy, offset, limit, noCountCache, callback) {
debug(`getAll(filter: ${filter}, orderBy: ${orderBy}, offset: ${offset}, limit: ${limit})`);
//return callback(new Error('PG.getAllImpl: Not implemented.'));
const fields = [];
const values = [];
const operators = [];
this.pgUtils.addFilterOptions(filter, fields, values, operators);
const joinedFields = [
{
source: 'a.api_group',
as: 'api_group',
alias: 'apiGroup'
},
{
source: 'a.data->>\'approved\'',
as: 'approved',
alias: 'approved'
},
{
source: 'b.application_name',
as: 'application_name',
alias: 'applicationName'
},
{
source: 'b.owner',
as: 'owner',
alias: 'owner'
},
{
source: 'b.user',
as: 'user',
alias: 'user'
},
{
source: 'b.userid',
as: 'userid',
alias: 'userid'
}

];
this.pgUtils.addFilterOptions(filter, fields, values, operators, joinedFields);
// This may be one of the most complicated queries we have here...
const options = {
limit: limit,
offset: offset,
orderBy: orderBy ? orderBy : 'id ASC',
operators: operators,
noCountCache: noCountCache,
joinedFields: joinedFields,
joinClause: 'INNER JOIN (SELECT string_agg(o.data->>\'email\', \', \') as owner, string_agg(r.name, \', \') as user, string_agg(r.users_id, \', \') as userid, p.data->> \'name\' as application_name , p.id FROM wicked.applications p, wicked.owners o, wicked.registrations r WHERE o.applications_id = p.id AND o.users_id = r.users_id GROUP BY application_name, p.id) b ON b.id = a.applications_id'
};

return this.pgUtils.getBy('subscriptions', fields, values, options, (err, subsList, countResult) => {
if (err)
return callback(err);
Expand All @@ -119,6 +157,7 @@ class PgSubscriptions {
});
}


getIndexImpl(offset, limit, callback) {
debug(`getIndex(offset: ${offset}, limit: ${limit})`);
this.pgUtils.getBy('subscriptions', [], [], { orderBy: 'id ASC' }, (err, subsList, countResult) => {
Expand Down
10 changes: 9 additions & 1 deletion dao/postgres/pg-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const async = require('async');
const { debug, info, warn, error } = require('portal-env').Logger('portal-api:dao:pg:meta');
const path = require('path');
const utils = require('../../routes/utils');

const CURRENT_DATABASE_VERSION = 2;
const CURRENT_DATABASE_VERSION = 3;

class PgMeta {
constructor(pgUtils) {
Expand Down Expand Up @@ -68,6 +69,13 @@ class PgMeta {
}
metadata.version = stepNumber;
instance.pgUtils.setMetadata(metadata, callback);
const apis = utils.loadApis();
for (let i = 0; i < apis.apis.length; ++i) {
const api = apis.apis[i];
const group = api.requiredGroup;
const id = api.id;
instance.pgUtils.populateSubscriptionApiGroup([id, group]);
}
});
}, (err) => {
if (err)
Expand Down
21 changes: 18 additions & 3 deletions dao/postgres/pg-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ class PgUtils {
});
}

populateSubscriptionApiGroup(parameters, callback) {
debug('populateSubscriptionApiGroup()');
this.getPoolOrClient((err, pool) => {
if (err)
return callback(err);
pool.query('UPDATE wicked.subscriptions SET api_group = $2 WHERE api_id = $1', parameters, callback);
});
}

createMetadata(callback) {
debug('createMetadata()');
this.getPoolOrClient((err, pool) => {
Expand Down Expand Up @@ -359,7 +368,6 @@ class PgUtils {
joinClause = options.joinClause;
if (options.joinedFields)
joinedFields = options.joinedFields;

const instance = this;
this.getPoolOrClient(client, (err, poolOrClient) => {
if (err)
Expand All @@ -378,12 +386,18 @@ class PgUtils {
});
}


addFilterOptions(filter, fields, values, operators) {
debug(`addFilterOptions()`);
for (let fieldName in filter) {
fields.push(fieldName);
values.push(`%${filter[fieldName]}%`);
operators.push('ILIKE');
if(filter[fieldName].indexOf('|') > 0){
values.push(`%(${filter[fieldName]})%`);
operators.push('SIMILAR TO');
} else {
values.push(`%${filter[fieldName]}%`);
operators.push('ILIKE');
}
}
}

Expand Down Expand Up @@ -538,6 +552,7 @@ class PgUtils {
additionalFields += ` AS ${joinedFieldDef.as}`;
}
}

let query = `SELECT ${mainPrefix}*${additionalFields} FROM wicked.${entity} ${tableName}`;
if (joinClause)
query += ` ${joinClause}`;
Expand Down
2 changes: 1 addition & 1 deletion dao/postgres/schemas/core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ CREATE TABLE wicked.subscriptions (
plan_id character varying(128) NOT NULL,
api_id character varying(128) NOT NULL,
client_id character varying(256),
api_group character varying(128),
data jsonb
);

Expand Down Expand Up @@ -456,7 +457,6 @@ ALTER TABLE ONLY wicked.grants
ALTER TABLE ONLY wicked.webhook_events
ADD CONSTRAINT webhook_listeners_fkey FOREIGN KEY (webhook_listeners_id) REFERENCES wicked.webhook_listeners(id) ON DELETE CASCADE;


-- Completed on 2018-04-30 14:45:37 CEST

--
Expand Down
2 changes: 2 additions & 0 deletions dao/postgres/schemas/migration-3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE wicked.subscriptions
ADD COLUMN IF NOT EXISTS api_group CHARACTER VARYING(128);
3 changes: 3 additions & 0 deletions routes/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ subscriptions.getAllSubscriptions = function (app, res, loggedInUserId, filter,
return utils.fail(res, 403, 'Not allowed.');
if (!userInfo.admin && !userInfo.approver)
return utils.fail(res, 403, 'Not allowed. This is admin/approver land.');
if(userInfo.approver) //add approver groups
filter['api_group']=userInfo.groups.join('|');
if (embed) {
dao.subscriptions.getAll(filter, orderBy, offset, limit, noCountCache, (err, subsIndex, countResult) => {
if (err)
Expand Down Expand Up @@ -302,6 +304,7 @@ subscriptions.addSubscription = function (app, res, applications, loggedInUserId
id: utils.createRandomId(),
application: subsCreateInfo.application,
api: subsCreateInfo.api,
api_group: selectedApi.requiredGroup,
plan: subsCreateInfo.plan,
apikey: apiKey,
clientId: clientId,
Expand Down