This commit is contained in:
parent
69255fe2fc
commit
59d2da24eb
|
@ -1,32 +1,27 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.execute = async(ctx, routine, params, schema, type, options) => {
|
Self.execute = async(ctx, query, params, options) => {
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
let caller = 'CALL';
|
|
||||||
|
|
||||||
params = params ?? [];
|
params = params ?? [];
|
||||||
schema = schema ?? 'vn';
|
|
||||||
type = type ?? 'procedure';
|
|
||||||
|
|
||||||
const myOptions = {userId: ctx.req.accessToken.userId};
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
const chain = `${schema}.${routine}`;
|
let [caller, chain] = query.split(' ');
|
||||||
|
if (!chain.includes('.')) chain = 'vn.' + chain;
|
||||||
|
|
||||||
const [canExecute] = await models.ProcsPriv.rawSql(
|
const [canExecute] = await models.ProcsPriv.rawSql(
|
||||||
'SELECT account.user_hasRoutinePriv(?,?,?)',
|
'SELECT account.user_hasRoutinePriv(?,?,?)',
|
||||||
[type.toUpperCase(), chain, userId],
|
[caller == 'CALL' ? 'PROCEDURE' : 'FUNCTION', chain, userId],
|
||||||
myOptions);
|
myOptions);
|
||||||
|
|
||||||
if (!Object.values(canExecute)[0]) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
|
if (!Object.values(canExecute)[0]) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
|
||||||
|
|
||||||
const isFunction = type == 'function';
|
|
||||||
let argString = params.map(() => '?').join(',');
|
let argString = params.map(() => '?').join(',');
|
||||||
|
query = `${query}(${argString})`;
|
||||||
if (isFunction)
|
|
||||||
caller = 'SELECT';
|
|
||||||
const query = `${caller} ${chain}(${argString})`;
|
|
||||||
|
|
||||||
const [response] = await models.ProcsPriv.rawSql(query, params, myOptions);
|
const [response] = await models.ProcsPriv.rawSql(query, params, myOptions);
|
||||||
return response;
|
return response;
|
||||||
|
|
|
@ -32,7 +32,11 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.executeFunc = async(ctx, routine, params, schema, options) => {
|
Self.executeFunc = async(ctx, routine, params, schema, options) => {
|
||||||
const response = await Self.execute(ctx, routine, params, schema, 'function', options);
|
if (schema)
|
||||||
|
routine = schema + '.' + routine;
|
||||||
|
|
||||||
|
const query = `SELECT ${routine}`;
|
||||||
|
const response = await Self.execute(ctx, query, params, options);
|
||||||
return Object.values(response)[0];
|
return Object.values(response)[0];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,6 +31,11 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.executeProc = async(ctx, routine, params, schema, options) =>
|
Self.executeProc = async(ctx, routine, params, schema, options) => {
|
||||||
Self.execute(ctx, routine, params, schema, 'procedure', options);
|
if (schema)
|
||||||
|
routine = schema + '.' + routine;
|
||||||
|
|
||||||
|
const query = `CALL ${routine}`;
|
||||||
|
return Self.execute(ctx, query, params, options);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,10 +50,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
|
||||||
|
|
||||||
await models.Application.execute(
|
await models.Application.execute(
|
||||||
ctx,
|
ctx,
|
||||||
'myProcedure',
|
'CALL myProcedure',
|
||||||
[1],
|
[1],
|
||||||
null,
|
|
||||||
null,
|
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -73,10 +71,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
|
||||||
|
|
||||||
const response = await models.Application.execute(
|
const response = await models.Application.execute(
|
||||||
ctx,
|
ctx,
|
||||||
'myProcedure',
|
'CALL myProcedure',
|
||||||
[1],
|
[1],
|
||||||
null,
|
|
||||||
null,
|
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue