2018-07-09 16:06:25 +00:00
|
|
|
|
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-06-06 13:07:54 +00:00
|
|
|
const utils = require('loopback/lib/utils');
|
2018-07-09 16:06:25 +00:00
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
module.exports = function(Self) {
|
2018-10-15 09:43:57 +00:00
|
|
|
Self.ParameterizedSQL = ParameterizedSQL;
|
2024-07-30 07:40:34 +00:00
|
|
|
let isSelect;
|
2018-10-15 09:43:57 +00:00
|
|
|
|
2018-10-30 12:58:02 +00:00
|
|
|
require('../methods/vn-model/getSetValues')(Self);
|
2020-05-27 11:11:41 +00:00
|
|
|
require('../methods/vn-model/getEnumValues')(Self);
|
2023-03-13 14:05:19 +00:00
|
|
|
require('../methods/vn-model/printService')(Self);
|
2018-10-30 12:58:02 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
Object.assign(Self, {
|
|
|
|
setup() {
|
|
|
|
Self.super_.setup.call(this);
|
2019-06-12 15:31:45 +00:00
|
|
|
/**
|
|
|
|
* Setting a global transaction timeout to find out if the service
|
|
|
|
* is blocked because the connection pool is empty.
|
|
|
|
*/
|
|
|
|
this.once('dataSourceAttached', () => {
|
|
|
|
let orgBeginTransaction = this.beginTransaction;
|
|
|
|
this.beginTransaction = function(options, cb) {
|
|
|
|
options = options || {};
|
2023-04-21 10:49:03 +00:00
|
|
|
if (options.timeout === undefined)
|
|
|
|
options.timeout = 120 * 1000;
|
2019-06-12 15:31:45 +00:00
|
|
|
return orgBeginTransaction.call(this, options, cb);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-08-06 15:08:27 +00:00
|
|
|
this.beforeRemote('find', async ctx => {
|
2024-08-07 08:12:54 +00:00
|
|
|
const defaultLimit = this.app.orm.selectLimit;
|
|
|
|
const filter = ctx.args.filter || {limit: defaultLimit};
|
|
|
|
|
|
|
|
if (filter.limit > defaultLimit) {
|
|
|
|
filter.limit = defaultLimit;
|
|
|
|
ctx.args.filter = filter;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.afterRemote('find', async({result}) => {
|
|
|
|
const length = Array.isArray(result) ? result.length : result ? 1 : 0;
|
|
|
|
if (length >= this.app.orm.selectLimit) throw new UserError('Too many records');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.beforeRemote('filter', async ctx => {
|
|
|
|
const defaultLimit = this.app.orm.selectLimit;
|
|
|
|
const filter = ctx.args.filter || {limit: defaultLimit};
|
|
|
|
|
|
|
|
if (filter.limit > defaultLimit) {
|
|
|
|
filter.limit = defaultLimit;
|
2024-07-30 07:40:34 +00:00
|
|
|
ctx.args.filter = filter;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-07 08:12:54 +00:00
|
|
|
this.afterRemote('filter', async({result}) => {
|
|
|
|
const length = Array.isArray(result) ? result.length : result ? 1 : 0;
|
2024-08-06 15:08:27 +00:00
|
|
|
if (length >= this.app.orm.selectLimit) throw new UserError('Too many records');
|
2024-07-30 07:40:34 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
// Register field ACL validation
|
2023-04-24 17:10:04 +00:00
|
|
|
/*
|
|
|
|
this.beforeRemote('prototype.patchAttributes', ctx => this.checkUpdateAcls(ctx));
|
2018-11-02 14:44:27 +00:00
|
|
|
this.beforeRemote('updateAll', ctx => this.checkUpdateAcls(ctx));
|
|
|
|
this.beforeRemote('patchOrCreate', ctx => this.checkInsertAcls(ctx));
|
|
|
|
this.beforeRemote('create', ctx => this.checkInsertAcls(ctx));
|
|
|
|
this.beforeRemote('replaceById', ctx => this.checkInsertAcls(ctx));
|
2023-04-24 17:10:04 +00:00
|
|
|
this.beforeRemote('replaceOrCreate', ctx => this.checkInsertAcls(ctx));
|
|
|
|
*/
|
2018-11-02 14:44:27 +00:00
|
|
|
|
|
|
|
this.remoteMethod('crud', {
|
|
|
|
description: `Create, update or/and delete instances from model with a single request`,
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'deletes',
|
|
|
|
description: `Identifiers of instances to delete`,
|
|
|
|
type: ['Integer']
|
|
|
|
}, {
|
|
|
|
arg: 'updates',
|
|
|
|
description: `Instances to update with it's identifier {where, data}`,
|
|
|
|
type: ['Object']
|
|
|
|
}, {
|
|
|
|
arg: 'creates',
|
|
|
|
description: `Instances to create`,
|
|
|
|
type: ['Object']
|
|
|
|
}
|
2020-02-06 06:41:51 +00:00
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async crud(deletes, updates, creates) {
|
2019-06-12 15:31:45 +00:00
|
|
|
let tx = await this.beginTransaction({});
|
2018-11-02 14:44:27 +00:00
|
|
|
|
|
|
|
try {
|
2019-06-12 15:31:45 +00:00
|
|
|
let options = {transaction: tx};
|
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
if (deletes) {
|
|
|
|
let promises = [];
|
|
|
|
for (let id of deletes)
|
|
|
|
promises.push(this.destroyById(id, options));
|
2020-02-06 12:38:46 +00:00
|
|
|
await Promise.all(promises);
|
2018-10-22 07:29:58 +00:00
|
|
|
}
|
2020-02-06 06:41:51 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
if (updates) {
|
|
|
|
let promises = [];
|
|
|
|
for (let update of updates)
|
|
|
|
promises.push(this.upsertWithWhere(update.where, update.data, options));
|
2020-02-06 12:38:46 +00:00
|
|
|
await Promise.all(promises);
|
2018-11-02 14:44:27 +00:00
|
|
|
}
|
2020-02-06 06:41:51 +00:00
|
|
|
|
|
|
|
let created;
|
2018-12-13 08:09:07 +00:00
|
|
|
if (creates && creates.length) {
|
2018-11-02 14:44:27 +00:00
|
|
|
try {
|
2020-02-06 06:41:51 +00:00
|
|
|
created = await this.create(creates, options);
|
2018-11-02 14:44:27 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw error[error.length - 1];
|
|
|
|
}
|
2018-12-13 08:09:07 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.commit();
|
2020-02-06 06:41:51 +00:00
|
|
|
|
2020-02-06 09:43:46 +00:00
|
|
|
return created;
|
2018-11-02 14:44:27 +00:00
|
|
|
} catch (error) {
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.rollback();
|
2018-11-02 14:44:27 +00:00
|
|
|
throw error;
|
2018-10-22 07:29:58 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for remoteMethod() but adding the context as
|
|
|
|
* extra argument at the beginning of arguments list.
|
|
|
|
*
|
|
|
|
* @param {String} methodName The method name
|
|
|
|
* @param {Object} options The method options
|
|
|
|
*/
|
|
|
|
remoteMethodCtx(methodName, options) {
|
|
|
|
if (options.accepts === undefined)
|
|
|
|
options.accepts = [];
|
|
|
|
else if (!Array.isArray(options.accepts))
|
|
|
|
options.accepts = [options.accepts];
|
|
|
|
|
|
|
|
options.accepts.unshift({
|
|
|
|
arg: 'ctx',
|
|
|
|
type: 'Object',
|
|
|
|
http: {source: 'context'}
|
|
|
|
});
|
|
|
|
this.remoteMethod(methodName, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a validation, marking it as exportable to the browser.
|
|
|
|
* Exportable validation functions should be synchronous and totally
|
|
|
|
* independent from other code because they are parsed in the browser
|
|
|
|
* using eval().
|
|
|
|
*
|
|
|
|
* @param {String} propertyName The property name
|
|
|
|
* @param {Function} validatorFn The validation function
|
|
|
|
* @param {Object} options The validation options
|
|
|
|
*/
|
|
|
|
validateBinded(propertyName, validatorFn, options) {
|
|
|
|
let customValidator = function(err) {
|
|
|
|
if (!validatorFn(this[propertyName])) err();
|
|
|
|
};
|
|
|
|
options.isExportable = true;
|
|
|
|
options.bindedFunction = validatorFn;
|
|
|
|
this.validate(propertyName, customValidator, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Catches database errors overriding create() and upsert() methods.
|
|
|
|
*
|
|
|
|
* @param {Function} replaceErrFunc - Callback
|
|
|
|
*/
|
|
|
|
rewriteDbError(replaceErrFunc) {
|
|
|
|
function replaceErr(err, replaceErrFunc) {
|
|
|
|
if (Array.isArray(err)) {
|
2020-12-29 12:39:10 +00:00
|
|
|
const errors = err.filter(error => {
|
|
|
|
return error != undefined && error != null;
|
|
|
|
});
|
2020-12-29 12:44:12 +00:00
|
|
|
let errs = [];
|
2020-12-29 12:39:10 +00:00
|
|
|
for (let e of errors) {
|
|
|
|
if (!(e instanceof UserError))
|
|
|
|
errs.push(replaceErrFunc(e));
|
|
|
|
else errs.push(e);
|
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
return errs;
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
return replaceErrFunc(err);
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 14:04:09 +00:00
|
|
|
function rewriteMethod(methodName) {
|
|
|
|
const realMethod = this[methodName];
|
2023-06-06 13:07:54 +00:00
|
|
|
return function(...args) {
|
|
|
|
let cb;
|
|
|
|
const lastArg = args[args.length - 1];
|
|
|
|
if (lastArg instanceof Function) {
|
|
|
|
cb = lastArg;
|
|
|
|
args.pop();
|
|
|
|
} else
|
|
|
|
cb = utils.createPromiseCallback();
|
|
|
|
|
|
|
|
args.push(function(err, res) {
|
|
|
|
if (err) err = replaceErr(err, replaceErrFunc);
|
|
|
|
cb(err, res);
|
|
|
|
});
|
|
|
|
realMethod.apply(this, args);
|
|
|
|
return cb.promise;
|
2018-11-02 14:44:27 +00:00
|
|
|
};
|
2019-03-12 14:04:09 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
|
2019-03-12 14:04:09 +00:00
|
|
|
this.once('attached', () => {
|
|
|
|
this.remove =
|
|
|
|
this.deleteAll =
|
|
|
|
this.destroyAll = rewriteMethod.call(this, 'remove');
|
|
|
|
this.upsert = rewriteMethod.call(this, 'upsert');
|
|
|
|
this.create = rewriteMethod.call(this, 'create');
|
2017-10-13 14:22:45 +00:00
|
|
|
});
|
2018-11-02 14:44:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shortcut to VnMySQL.executeP()
|
|
|
|
*/
|
2023-06-12 08:46:21 +00:00
|
|
|
async rawSql(query, params, options) {
|
|
|
|
const userId = options?.userId;
|
|
|
|
const connector = this.dataSource.connector;
|
|
|
|
let conn;
|
|
|
|
let res;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (userId) {
|
2023-06-19 12:54:32 +00:00
|
|
|
if (!options.transaction) {
|
|
|
|
options = Object.assign({}, options);
|
|
|
|
conn = await new Promise((resolve, reject) => {
|
|
|
|
connector.client.getConnection(function(err, conn) {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve(conn);
|
|
|
|
});
|
2023-06-12 08:46:21 +00:00
|
|
|
});
|
2023-06-19 12:54:32 +00:00
|
|
|
options.transaction = {
|
2023-06-12 08:46:21 +00:00
|
|
|
connection: conn,
|
|
|
|
connector
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
await connector.executeP(
|
|
|
|
'CALL account.myUser_loginWithName((SELECT name FROM account.user WHERE id = ?))',
|
2023-06-19 12:54:32 +00:00
|
|
|
[userId], options
|
2023-06-12 08:46:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-19 12:54:32 +00:00
|
|
|
res = await connector.executeP(query, params, options);
|
2023-06-12 08:46:21 +00:00
|
|
|
|
2023-06-19 12:54:32 +00:00
|
|
|
if (userId)
|
|
|
|
await connector.executeP('CALL account.myUser_logout()', null, options);
|
2023-06-12 08:46:21 +00:00
|
|
|
} finally {
|
|
|
|
if (conn) conn.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2018-11-02 14:44:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shortcut to VnMySQL.executeStmt()
|
|
|
|
*/
|
|
|
|
rawStmt(stmt, options) {
|
|
|
|
return this.dataSource.connector.executeStmt(stmt, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shortcut to VnMySQL.makeLimit()
|
|
|
|
*/
|
|
|
|
makeLimit(filter) {
|
|
|
|
return this.dataSource.connector.makeLimit(filter);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shortcut to VnMySQL.makeSuffix()
|
|
|
|
*/
|
|
|
|
makeSuffix(filter) {
|
|
|
|
return this.dataSource.connector.makeSuffix(filter);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shortcut to VnMySQL.buildModelSuffix()
|
|
|
|
*/
|
|
|
|
buildSuffix(filter, tableAlias) {
|
|
|
|
return this.dataSource.connector.buildModelSuffix(this.modelName, filter, tableAlias);
|
|
|
|
},
|
|
|
|
|
|
|
|
async checkAcls(ctx, actionType) {
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
let models = this.app.models;
|
2023-01-23 14:24:00 +00:00
|
|
|
let userRoles = await models.VnUser.getRoles(userId);
|
2018-11-02 14:44:27 +00:00
|
|
|
let data = ctx.args.data;
|
|
|
|
let modelAcls;
|
|
|
|
|
|
|
|
function modifiedProperties(data) {
|
|
|
|
let properties = [];
|
|
|
|
|
|
|
|
for (property in data)
|
|
|
|
properties.push(property);
|
|
|
|
|
|
|
|
return properties;
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
|
|
|
|
modelAcls = await models.FieldAcl.find({
|
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{model: this.modelName},
|
|
|
|
{role: {inq: userRoles}},
|
|
|
|
{property: '*'},
|
|
|
|
{or: [{actionType: '*'}, {actionType: actionType}]}
|
|
|
|
]
|
2018-08-30 06:50:03 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let allowedAll = modelAcls.find(acl => {
|
|
|
|
return acl.property == '*';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (allowedAll)
|
|
|
|
return;
|
|
|
|
|
|
|
|
modelAcls = await models.FieldAcl.find({
|
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{model: this.modelName},
|
|
|
|
{role: {inq: userRoles}},
|
|
|
|
{property: {inq: modifiedProperties(data)}},
|
|
|
|
{or: [{actionType: '*'}, {actionType: actionType}]}
|
|
|
|
]
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
2018-11-02 14:44:27 +00:00
|
|
|
});
|
2018-05-31 09:52:39 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
let propsHash = {};
|
|
|
|
for (let acl of modelAcls)
|
|
|
|
propsHash[acl.property] = true;
|
2018-10-23 09:25:51 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
let allowedProperties = Object.keys(data).every(property => {
|
|
|
|
return propsHash[property];
|
|
|
|
});
|
2018-10-23 09:25:51 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
if (!allowedProperties)
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
},
|
2018-10-23 09:25:51 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
checkUpdateAcls(ctx) {
|
|
|
|
return this.checkAcls(ctx, 'update');
|
|
|
|
},
|
2018-10-23 09:25:51 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
checkInsertAcls(ctx) {
|
|
|
|
return this.checkAcls(ctx, 'insert');
|
|
|
|
}
|
|
|
|
});
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|