larvituser 0.19.0 → 0.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -99
- package/dbmigration/2.js +18 -57
- package/dist/dataWriter.d.ts +27 -0
- package/dist/dataWriter.d.ts.map +1 -0
- package/dist/dataWriter.js +204 -0
- package/dist/dataWriter.js.map +1 -0
- package/dist/helpers.d.ts +19 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +60 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +418 -0
- package/dist/index.js.map +1 -0
- package/dist/userBase.d.ts +53 -0
- package/dist/userBase.d.ts.map +1 -0
- package/dist/userBase.js +120 -0
- package/dist/userBase.js.map +1 -0
- package/dist/users.d.ts +43 -0
- package/dist/users.d.ts.map +1 -0
- package/dist/users.js +186 -0
- package/dist/users.js.map +1 -0
- package/package.json +28 -12
- package/.eslintignore +0 -1
- package/.eslintrc +0 -35
- package/.travis.yml +0 -22
- package/config/db_travis.json +0 -9
- package/dataWriter.js +0 -716
- package/helpers.js +0 -110
- package/index.js +0 -953
- package/renovate.json +0 -9
- package/test/00test.js +0 -850
- package/test/98lint.js +0 -9
- package/users.js +0 -281
package/dataWriter.js
DELETED
|
@@ -1,716 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EventEmitter = require('events').EventEmitter,
|
|
4
|
-
topLogPrefix = 'larvituser: dataWriter.js - ',
|
|
5
|
-
DbMigration = require('larvitdbmigration'),
|
|
6
|
-
Helpers = require(__dirname + '/helpers.js'),
|
|
7
|
-
uuidLib = require('uuid'),
|
|
8
|
-
LUtils = require('larvitutils'),
|
|
9
|
-
amsync = require('larvitamsync'),
|
|
10
|
-
async = require('async');
|
|
11
|
-
|
|
12
|
-
function DataWriter(options, cb) {
|
|
13
|
-
const that = this;
|
|
14
|
-
|
|
15
|
-
that.readyInProgress = false;
|
|
16
|
-
that.isReady = false;
|
|
17
|
-
|
|
18
|
-
if ( ! options.log) {
|
|
19
|
-
const tmpLUtils = new LUtils();
|
|
20
|
-
options.log = new tmpLUtils.Log();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
that.options = options;
|
|
24
|
-
|
|
25
|
-
for (const key of Object.keys(options)) {
|
|
26
|
-
that[key] = options[key];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
that.lUtils = new LUtils({'log': that.log});
|
|
30
|
-
|
|
31
|
-
that.emitter = new EventEmitter();
|
|
32
|
-
|
|
33
|
-
that.listenToQueue(function (err) {
|
|
34
|
-
if (err) return cb(err);
|
|
35
|
-
|
|
36
|
-
that.helpers = new Helpers({
|
|
37
|
-
'dataWriter': that,
|
|
38
|
-
'log': that.log,
|
|
39
|
-
'db': that.db
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
cb();
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
DataWriter.prototype.addUserDataFields = function addUserDataFields(params, deliveryTag, msgUuid, cb) {
|
|
47
|
-
const userUuidBuffer = this.lUtils.uuidToBuffer(params.userUuid),
|
|
48
|
-
logPrefix = topLogPrefix + 'addUserDataFields() - ',
|
|
49
|
-
dbValues = [],
|
|
50
|
-
tasks = [],
|
|
51
|
-
that = this;
|
|
52
|
-
|
|
53
|
-
let sql = 'INSERT INTO user_users_data (userUuid, fieldUuid, data) VALUES';
|
|
54
|
-
|
|
55
|
-
if (typeof deliveryTag === 'function') {
|
|
56
|
-
cb = deliveryTag;
|
|
57
|
-
deliveryTag = false;
|
|
58
|
-
msgUuid = false;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
62
|
-
cb = function () {};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (userUuidBuffer === false) {
|
|
66
|
-
const err = new Error('Invalid userUuid');
|
|
67
|
-
that.log.warn(logPrefix + err.message);
|
|
68
|
-
if (msgUuid !== false) that.emitter.emit(msgUuid, err);
|
|
69
|
-
return cb(err);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
for (let key in params.fields) {
|
|
73
|
-
tasks.push(function (cb) {
|
|
74
|
-
that.helpers.getFieldUuid(key, function (err, fieldUuid) {
|
|
75
|
-
const fieldUuidBuffer = that.lUtils.uuidToBuffer(fieldUuid);
|
|
76
|
-
|
|
77
|
-
if (err) {
|
|
78
|
-
that.log.warn(logPrefix + err.message);
|
|
79
|
-
return cb(err);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (fieldUuidBuffer === false) {
|
|
83
|
-
const err = new Error('Invalid fieldUuid');
|
|
84
|
-
that.log.warn(logPrefix + err.message);
|
|
85
|
-
return cb(err);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (params.fields[key] === null || params.fields[key] === undefined) {
|
|
89
|
-
sql += '(?,?,\'\'),';
|
|
90
|
-
dbValues.push(userUuidBuffer, fieldUuidBuffer);
|
|
91
|
-
} else {
|
|
92
|
-
if (Array.isArray(params.fields[key])) {
|
|
93
|
-
for (let i = 0; i < params.fields[key].length; i ++) {
|
|
94
|
-
sql += '(?,?,?),';
|
|
95
|
-
dbValues.push(userUuidBuffer, fieldUuidBuffer, params.fields[key][i]);
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
sql += '(?,?,?),';
|
|
99
|
-
dbValues.push(userUuidBuffer, fieldUuidBuffer, params.fields[key]);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
cb(err);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async.parallel(tasks, function (err) {
|
|
109
|
-
if (err) {
|
|
110
|
-
that.log.warn(logPrefix + err.message);
|
|
111
|
-
if (msgUuid !== false) that.emitter.emit(msgUuid, err);
|
|
112
|
-
return cb(err);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
sql = sql.substring(0, sql.length - 1);
|
|
116
|
-
|
|
117
|
-
if (dbValues.length === 0) {
|
|
118
|
-
that.log.info(logPrefix + 'No fields or field data specifed');
|
|
119
|
-
|
|
120
|
-
// We need to setImmediate here since a listener on the other side must be done async to obtain a msgUuid
|
|
121
|
-
setImmediate(function () {
|
|
122
|
-
if (msgUuid !== false) that.emitter.emit(msgUuid);
|
|
123
|
-
return cb();
|
|
124
|
-
});
|
|
125
|
-
return; // Make sure no more execution is taking place
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
that.db.query(sql, dbValues, function (err) {
|
|
129
|
-
if (err) {
|
|
130
|
-
that.log.warn(topLogPrefix + 'addUserDataFields() - ' + err.message);
|
|
131
|
-
}
|
|
132
|
-
if (msgUuid !== false) that.emitter.emit(msgUuid, err);
|
|
133
|
-
cb(err);
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
DataWriter.prototype.addUserField = function addUserField(params, deliveryTag, msgUuid, cb) {
|
|
139
|
-
const uuidBuffer = this.lUtils.uuidToBuffer(params.uuid),
|
|
140
|
-
logPrefix = topLogPrefix + 'addUserField() - ',
|
|
141
|
-
that = this,
|
|
142
|
-
name = params.name,
|
|
143
|
-
sql = 'INSERT IGNORE INTO user_data_fields (uuid, name) VALUES(?,?)';
|
|
144
|
-
|
|
145
|
-
if (typeof cb !== 'function') {
|
|
146
|
-
cb = function () {};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (uuidBuffer === false) {
|
|
150
|
-
const err = new Error('Invalid field uuid');
|
|
151
|
-
that.log.warn(logPrefix + err.message);
|
|
152
|
-
|
|
153
|
-
that.emitter.emit(msgUuid, err);
|
|
154
|
-
that.emitter.emit('addedField_' + name, err);
|
|
155
|
-
return cb(err);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
that.db.query(sql, [uuidBuffer, name], function (err) {
|
|
159
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
160
|
-
|
|
161
|
-
that.emitter.emit(msgUuid, err);
|
|
162
|
-
that.emitter.emit('addedField_' + name, err);
|
|
163
|
-
cb(err);
|
|
164
|
-
});
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
DataWriter.prototype.addUserFieldReq = function addUserFieldReq(params, deliveryTag, msgUuid, cb) {
|
|
168
|
-
const logPrefix = topLogPrefix + 'addUserFieldReq() - ',
|
|
169
|
-
fieldName = params.name,
|
|
170
|
-
that = this;
|
|
171
|
-
|
|
172
|
-
if (typeof cb !== 'function') {
|
|
173
|
-
cb = function () {};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (that.mode === 'master' || that.mode === 'noSync') {
|
|
177
|
-
function run() {
|
|
178
|
-
if (that.addUserFieldReqRunning === true) {
|
|
179
|
-
setTimeout(run, 10);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
that.addUserFieldReqRunning = true;
|
|
184
|
-
|
|
185
|
-
// Check if this is already set in the database
|
|
186
|
-
that.db.query('SELECT uuid FROM user_data_fields WHERE name = ?', [fieldName], function (err, rows) {
|
|
187
|
-
const options = {'exchange': that.exchangeName},
|
|
188
|
-
sendObj = {};
|
|
189
|
-
|
|
190
|
-
if (err) return cb(err);
|
|
191
|
-
|
|
192
|
-
sendObj.action = 'addUserField';
|
|
193
|
-
sendObj.params = {};
|
|
194
|
-
sendObj.params.name = fieldName;
|
|
195
|
-
sendObj.params.uuid = (rows.length) ? that.lUtils.formatUuid(rows[0].uuid) : uuidLib.v1();
|
|
196
|
-
|
|
197
|
-
that.emitter.once('addedField_' + fieldName, function (err) {
|
|
198
|
-
if (err) return cb(err);
|
|
199
|
-
that.addUserFieldReqRunning = false;
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
that.intercom.send(sendObj, options, function (err, msgUuid2) {
|
|
203
|
-
if (err) return cb(err);
|
|
204
|
-
|
|
205
|
-
that.emitter.once(msgUuid2, function (err) {
|
|
206
|
-
if (err) return cb(err);
|
|
207
|
-
|
|
208
|
-
that.emitter.emit(msgUuid, err);
|
|
209
|
-
|
|
210
|
-
cb(err);
|
|
211
|
-
});
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
run();
|
|
216
|
-
} else {
|
|
217
|
-
that.log.debug(logPrefix + 'Ignoring since we are not master or noSync');
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
DataWriter.prototype.create = function create(params, deliveryTag, msgUuid, cb) {
|
|
222
|
-
const uuidBuffer = this.lUtils.uuidToBuffer(params.uuid),
|
|
223
|
-
logPrefix = topLogPrefix + 'create() - ',
|
|
224
|
-
dbFields = [],
|
|
225
|
-
that = this,
|
|
226
|
-
sql = 'INSERT IGNORE INTO user_users (uuid, username, password) VALUES(?,?,?);';
|
|
227
|
-
|
|
228
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
229
|
-
cb = function () {};
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
dbFields.push(uuidBuffer);
|
|
233
|
-
dbFields.push(params.username);
|
|
234
|
-
dbFields.push(params.password);
|
|
235
|
-
|
|
236
|
-
if (uuidBuffer === false) {
|
|
237
|
-
const err = new Error('Invalid user uuid supplied: "' + params.uuid + '", deliveryTag: "' + deliveryTag + '", msgUuid: "' + msgUuid + '"');
|
|
238
|
-
|
|
239
|
-
that.log.warn(logPrefix + err.message);
|
|
240
|
-
that.emitter.emit(msgUuid, err);
|
|
241
|
-
return cb(err);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
that.db.query(sql, dbFields, function (err, results) {
|
|
245
|
-
const fieldsParams = {};
|
|
246
|
-
|
|
247
|
-
if (results.affectedRows === 0) {
|
|
248
|
-
const err = new Error('No user created, duplicate key on uuid: "' + params.uuid + '" or username: "' + params.username + '"');
|
|
249
|
-
that.log.warn(logPrefix + err.message);
|
|
250
|
-
that.emitter.emit(msgUuid, err);
|
|
251
|
-
return cb(err);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (err) {
|
|
255
|
-
that.log.warn(logPrefix + err.message);
|
|
256
|
-
that.emitter.emit(msgUuid, err);
|
|
257
|
-
return cb(err);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
fieldsParams.userUuid = params.uuid;
|
|
261
|
-
fieldsParams.fields = params.fields;
|
|
262
|
-
|
|
263
|
-
that.addUserDataFields(fieldsParams, function (err) {
|
|
264
|
-
if (err) {
|
|
265
|
-
that.log.warn(logPrefix + err.message);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
that.emitter.emit(msgUuid, err);
|
|
269
|
-
cb(err);
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
DataWriter.prototype.listenToQueue = function listenToQueue(cb) {
|
|
275
|
-
const logPrefix = topLogPrefix + 'listenToQueue() - ',
|
|
276
|
-
options = {'exchange': this.exchangeName},
|
|
277
|
-
tasks = [],
|
|
278
|
-
that = this;
|
|
279
|
-
|
|
280
|
-
let listenMethod;
|
|
281
|
-
|
|
282
|
-
if (typeof cb !== 'function') {
|
|
283
|
-
cb = function () {};
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
tasks.push(function (cb) {
|
|
287
|
-
if (that.mode === 'master') {
|
|
288
|
-
listenMethod = 'consume';
|
|
289
|
-
options.exclusive = true; // It is important no other client tries to sneak
|
|
290
|
-
// // out messages from us, and we want "consume"
|
|
291
|
-
// // since we want the queue to persist even if this
|
|
292
|
-
// // minion goes offline.
|
|
293
|
-
} else if (that.mode === 'slave' || that.mode === 'noSync') {
|
|
294
|
-
listenMethod = 'subscribe';
|
|
295
|
-
} else {
|
|
296
|
-
const err = new Error('Invalid that.mode. Must be either "master", "slave" or "noSync"');
|
|
297
|
-
that.log.error(logPrefix + err.message);
|
|
298
|
-
return cb(err);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
that.log.info(logPrefix + 'listenMethod: ' + listenMethod);
|
|
302
|
-
cb();
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
tasks.push(function (cb) {
|
|
306
|
-
that.intercom.ready(function (err) {
|
|
307
|
-
if (err) {
|
|
308
|
-
that.log.error(logPrefix + 'intercom.ready() err: ' + err.message);
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
that.intercom[listenMethod](options, function (message, ack, deliveryTag) {
|
|
313
|
-
that.ready(function (err) {
|
|
314
|
-
ack(err); // Ack first, if something goes wrong we log it and handle it manually
|
|
315
|
-
|
|
316
|
-
if (err) {
|
|
317
|
-
that.log.error(logPrefix + 'intercom.' + listenMethod + '() - that.ready() returned err: ' + err.message);
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (typeof message !== 'object') {
|
|
322
|
-
that.log.error(logPrefix + 'intercom.' + listenMethod + '() - Invalid message received, is not an object! deliveryTag: "' + deliveryTag + '"');
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
if (typeof that[message.action] === 'function') {
|
|
327
|
-
that[message.action](message.params, deliveryTag, message.uuid);
|
|
328
|
-
} else {
|
|
329
|
-
that.log.warn(logPrefix + 'intercom.' + listenMethod + '() - Unknown message.action received: "' + message.action + '"');
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
}, cb);
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
// Run the ready function
|
|
337
|
-
tasks.push(function (cb) {
|
|
338
|
-
that.ready(cb);
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
async.series(tasks, cb);
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
DataWriter.prototype.ready = function ready(cb) {
|
|
345
|
-
const that = this,
|
|
346
|
-
logPrefix = topLogPrefix + 'ready() - ',
|
|
347
|
-
tasks = [];
|
|
348
|
-
|
|
349
|
-
if (typeof cb !== 'function') {
|
|
350
|
-
cb = function () {};
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
if (that.isReady === true) return cb();
|
|
354
|
-
|
|
355
|
-
if (that.readyInProgress === true) {
|
|
356
|
-
that.emitter.on('ready', cb);
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
that.readyInProgress = true;
|
|
361
|
-
|
|
362
|
-
tasks.push(function (cb) {
|
|
363
|
-
that.log.debug(logPrefix + 'Waiting for intercom.ready()');
|
|
364
|
-
that.intercom.ready(cb);
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
tasks.push(function (cb) {
|
|
368
|
-
that.log.debug(logPrefix + 'Waiting for db.ready()');
|
|
369
|
-
that.db.ready(cb);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
tasks.push(function (cb) {
|
|
373
|
-
if (that.mode === 'slave') {
|
|
374
|
-
that.log.verbose(logPrefix + 'that.mode: "' + that.mode + '", so read');
|
|
375
|
-
amsync.mariadb({
|
|
376
|
-
'exchange': that.exchangeName + '_dataDump',
|
|
377
|
-
'intercom': that.intercom,
|
|
378
|
-
'log': that.log,
|
|
379
|
-
'db': that.db
|
|
380
|
-
}, cb);
|
|
381
|
-
} else {
|
|
382
|
-
cb();
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
// Change tablename from larvituser_db_version to users_db_version
|
|
387
|
-
// In case larvituser_db_version exists, that is.
|
|
388
|
-
// This is due to a breaking change in 0.17.0 and this was added as
|
|
389
|
-
// a patch in 0.17.1 to make applications not crash
|
|
390
|
-
tasks.push(function (cb) {
|
|
391
|
-
that.db.query('SHOW TABLES LIKE \'larvituser_db_version\'', function (err, rows) {
|
|
392
|
-
if (err) return cb(err);
|
|
393
|
-
|
|
394
|
-
if (rows.length) {
|
|
395
|
-
that.db.query('RENAME TABLE larvituser_db_version TO users_db_version', cb);
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
cb();
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
// Migrate database
|
|
404
|
-
tasks.push(function (cb) {
|
|
405
|
-
const options = {};
|
|
406
|
-
|
|
407
|
-
let dbMigration;
|
|
408
|
-
|
|
409
|
-
options.dbType = 'mariadb';
|
|
410
|
-
options.dbDriver = that.db;
|
|
411
|
-
options.tableName = 'users_db_version';
|
|
412
|
-
options.migrationScriptsPath = __dirname + '/dbmigration';
|
|
413
|
-
options.log = that.log;
|
|
414
|
-
dbMigration = new DbMigration(options);
|
|
415
|
-
|
|
416
|
-
dbMigration.run(function (err) {
|
|
417
|
-
if (err) {
|
|
418
|
-
that.log.error(logPrefix + 'Database error: ' + err.message);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
cb(err);
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
async.series(tasks, function (err) {
|
|
426
|
-
if (err) return cb(err);
|
|
427
|
-
|
|
428
|
-
that.isReady = true;
|
|
429
|
-
that.emitter.emit('ready');
|
|
430
|
-
|
|
431
|
-
if (that.mode === 'master') {
|
|
432
|
-
that.runDumpServer(cb);
|
|
433
|
-
} else {
|
|
434
|
-
cb();
|
|
435
|
-
}
|
|
436
|
-
});
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
DataWriter.prototype.replaceFields = function replaceFields(params, deliveryTag, msgUuid, cb) {
|
|
440
|
-
const fieldNamesToUuidBufs = {},
|
|
441
|
-
userUuidBuf = this.lUtils.uuidToBuffer(params.userUuid),
|
|
442
|
-
logPrefix = topLogPrefix + 'replaceFields() - ',
|
|
443
|
-
tasks = [],
|
|
444
|
-
that = this;
|
|
445
|
-
|
|
446
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
447
|
-
cb = function () {};
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (userUuidBuf === false) {
|
|
451
|
-
const err = new Error('Invalid user uuid supplied: "' + params.userUuid + '", deliveryTag: "' + deliveryTag + '", msgUuid: "' + msgUuid + '"');
|
|
452
|
-
|
|
453
|
-
that.log.warn(logPrefix + err.message);
|
|
454
|
-
that.emitter.emit(msgUuid, err);
|
|
455
|
-
return cb(err);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// Check so the user uuid is valid
|
|
459
|
-
tasks.push(function (cb) {
|
|
460
|
-
that.db.query('SELECT * FROM user_users WHERE uuid = ?', userUuidBuf, function (err, rows) {
|
|
461
|
-
if (err) return cb(err);
|
|
462
|
-
|
|
463
|
-
if (rows.length === 0) {
|
|
464
|
-
const err = new Error('Invalid user uuid: "' + params.userUuid + '", no records found in database of this user');
|
|
465
|
-
that.log.warn(logPrefix + err.message);
|
|
466
|
-
return cb(err);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return cb();
|
|
470
|
-
});
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
// Clean out previous data
|
|
474
|
-
tasks.push(function (cb) {
|
|
475
|
-
that.db.query('DELETE FROM user_users_data WHERE userUuid = ?', [userUuidBuf], cb);
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
// Get field uuids
|
|
479
|
-
tasks.push(function (cb) {
|
|
480
|
-
const tasks = [];
|
|
481
|
-
|
|
482
|
-
for (const fieldName of Object.keys(params.fields)) {
|
|
483
|
-
tasks.push(function (cb) {
|
|
484
|
-
that.helpers.getFieldUuid(fieldName, function (err, fieldUuid) {
|
|
485
|
-
fieldNamesToUuidBufs[fieldName] = that.lUtils.uuidToBuffer(fieldUuid);
|
|
486
|
-
|
|
487
|
-
if (fieldNamesToUuidBufs[fieldName] === false) {
|
|
488
|
-
const err = new Error('Invalid field uuid');
|
|
489
|
-
that.log.warn(logPrefix + err.message);
|
|
490
|
-
return cb(err);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
cb(err);
|
|
494
|
-
});
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
async.parallel(tasks, cb);
|
|
499
|
-
});
|
|
500
|
-
|
|
501
|
-
// Add new data
|
|
502
|
-
tasks.push(function (cb) {
|
|
503
|
-
const dbFields = [];
|
|
504
|
-
|
|
505
|
-
let sql = 'INSERT INTO user_users_data (userUuid, fieldUuid, data) VALUES';
|
|
506
|
-
|
|
507
|
-
if ( ! params.fields) return cb();
|
|
508
|
-
|
|
509
|
-
for (const fieldName of Object.keys(params.fields)) {
|
|
510
|
-
if ( ! (params.fields[fieldName] instanceof Array)) {
|
|
511
|
-
params.fields[fieldName] = [params.fields[fieldName]];
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
for (let i = 0; params.fields[fieldName][i] !== undefined; i ++) {
|
|
515
|
-
const fieldValue = params.fields[fieldName][i];
|
|
516
|
-
|
|
517
|
-
sql += '(?,?,?),';
|
|
518
|
-
dbFields.push(userUuidBuf);
|
|
519
|
-
dbFields.push(fieldNamesToUuidBufs[fieldName]);
|
|
520
|
-
dbFields.push(fieldValue);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
sql = sql.substring(0, sql.length - 1) + ';';
|
|
525
|
-
|
|
526
|
-
if (dbFields.length === 0) return cb();
|
|
527
|
-
|
|
528
|
-
that.db.query(sql, dbFields, cb);
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
async.series(tasks, function (err) {
|
|
532
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
533
|
-
that.emitter.emit(msgUuid, err);
|
|
534
|
-
cb(err);
|
|
535
|
-
});
|
|
536
|
-
};
|
|
537
|
-
|
|
538
|
-
DataWriter.prototype.rmUser = function rmUser(params, deliveryTag, msgUuid, cb) {
|
|
539
|
-
const uuidBuffer = this.lUtils.uuidToBuffer(params.userUuid),
|
|
540
|
-
logPrefix = topLogPrefix + 'rmUser() - ',
|
|
541
|
-
tasks = [],
|
|
542
|
-
that = this;
|
|
543
|
-
|
|
544
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
545
|
-
cb = function () {};
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
if (uuidBuffer === false) {
|
|
549
|
-
const err = new Error('Invalid user uuid');
|
|
550
|
-
that.log.warn(logPrefix + err.message);
|
|
551
|
-
that.emitter.emit(msgUuid, err);
|
|
552
|
-
return cb(err);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
tasks.push(function (cb) {
|
|
556
|
-
const sql = 'DELETE FROM user_users_data WHERE userUuid = ?;';
|
|
557
|
-
|
|
558
|
-
that.db.query(sql, [uuidBuffer], cb);
|
|
559
|
-
});
|
|
560
|
-
|
|
561
|
-
tasks.push(function (cb) {
|
|
562
|
-
const sql = 'DELETE FROM user_users WHERE uuid = ?;';
|
|
563
|
-
|
|
564
|
-
that.db.query(sql, [uuidBuffer], cb);
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
async.series(tasks, function (err) {
|
|
568
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
569
|
-
that.emitter.emit(msgUuid, err);
|
|
570
|
-
cb(err);
|
|
571
|
-
});
|
|
572
|
-
};
|
|
573
|
-
|
|
574
|
-
DataWriter.prototype.rmUserField = function rmUserField(params, deliveryTag, msgUuid, cb) {
|
|
575
|
-
const logPrefix = topLogPrefix + 'rmUserField() - ',
|
|
576
|
-
that = this;
|
|
577
|
-
|
|
578
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
579
|
-
cb = function () {};
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
that.helpers.getFieldUuid(params.fieldName, function (err, fieldUuid) {
|
|
583
|
-
const userUuidBuffer = that.lUtils.uuidToBuffer(params.userUuid),
|
|
584
|
-
fieldUuidBuffer = that.lUtils.uuidToBuffer(fieldUuid),
|
|
585
|
-
sql = 'DELETE FROM user_users_data WHERE userUuid = ? AND fieldUuid = ?';
|
|
586
|
-
|
|
587
|
-
if (err) {
|
|
588
|
-
that.emitter.emit(msgUuid, err);
|
|
589
|
-
return;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
if (userUuidBuffer === false) {
|
|
593
|
-
const err = new Error('Invalid user uuid');
|
|
594
|
-
that.log.warn(logPrefix + err.message);
|
|
595
|
-
that.emitter.emit(msgUuid, err);
|
|
596
|
-
return cb(err);
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
if (fieldUuidBuffer === false) {
|
|
600
|
-
const err = new Error('Invalid field uuid');
|
|
601
|
-
that.log.warn(logPrefix + err.message);
|
|
602
|
-
that.emitter.emit(msgUuid, err);
|
|
603
|
-
return cb(err);
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
that.db.query(sql, [userUuidBuffer, fieldUuidBuffer], function (err) {
|
|
607
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
608
|
-
that.emitter.emit(msgUuid, err);
|
|
609
|
-
cb(err);
|
|
610
|
-
});
|
|
611
|
-
});
|
|
612
|
-
};
|
|
613
|
-
|
|
614
|
-
DataWriter.prototype.runDumpServer = function runDumpServer(cb) {
|
|
615
|
-
const that = this,
|
|
616
|
-
args = [],
|
|
617
|
-
options = {
|
|
618
|
-
'Content-Type': 'application/sql',
|
|
619
|
-
'exchange': that.exchangeName + '_dataDump',
|
|
620
|
-
'intercom': that.intercom,
|
|
621
|
-
'minPort': (that.amsync && that.amsync.minPort) ? that.amsync.minPort : null,
|
|
622
|
-
'maxPort': (that.amsync && that.amsync.maxPort) ? that.amsync.maxPort : null,
|
|
623
|
-
'host': (that.amsync && that.amsync.host) ? that.amsync.host : null,
|
|
624
|
-
'log': that.log,
|
|
625
|
-
};
|
|
626
|
-
|
|
627
|
-
if (that.db.conf.host) {
|
|
628
|
-
args.push('-h');
|
|
629
|
-
args.push(that.db.conf.host);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
args.push('-u');
|
|
633
|
-
args.push(that.db.conf.user);
|
|
634
|
-
|
|
635
|
-
if (that.db.conf.password) {
|
|
636
|
-
args.push('-p' + that.db.conf.password);
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
args.push('--single-transaction');
|
|
640
|
-
args.push('--hex-blob');
|
|
641
|
-
args.push(that.db.conf.database);
|
|
642
|
-
|
|
643
|
-
// Tables
|
|
644
|
-
args.push('users_db_version');
|
|
645
|
-
args.push('user_data_fields');
|
|
646
|
-
args.push('user_roles_rights');
|
|
647
|
-
args.push('user_users');
|
|
648
|
-
args.push('user_users_data');
|
|
649
|
-
|
|
650
|
-
options.dataDumpCmd = {
|
|
651
|
-
'command': 'mysqldump',
|
|
652
|
-
'args': args,
|
|
653
|
-
'log': that.log
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
new amsync.SyncServer(options, cb);
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
DataWriter.prototype.setPassword = function setPassword(params, deliveryTag, msgUuid, cb) {
|
|
660
|
-
const logPrefix = topLogPrefix + 'setPassword() - ',
|
|
661
|
-
dbFields = [],
|
|
662
|
-
userUuidBuffer = this.lUtils.uuidToBuffer(params.userUuid),
|
|
663
|
-
that = this,
|
|
664
|
-
sql = 'UPDATE user_users SET password = ? WHERE uuid = ?;';
|
|
665
|
-
|
|
666
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
667
|
-
cb = function () {};
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
if (userUuidBuffer === false) {
|
|
671
|
-
const err = new Error('Invalid user uuid');
|
|
672
|
-
that.log.warn(logPrefix + err.message);
|
|
673
|
-
that.emitter.emit(msgUuid, err);
|
|
674
|
-
return cb(err);
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
if (params.password === false) {
|
|
678
|
-
dbFields.push('');
|
|
679
|
-
} else {
|
|
680
|
-
dbFields.push(params.password);
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
dbFields.push(userUuidBuffer);
|
|
684
|
-
that.db.query(sql, dbFields, function (err) {
|
|
685
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
686
|
-
that.emitter.emit(msgUuid, err);
|
|
687
|
-
cb(err);
|
|
688
|
-
});
|
|
689
|
-
};
|
|
690
|
-
|
|
691
|
-
DataWriter.prototype.setUsername = function setUsername(params, deliveryTag, msgUuid, cb) {
|
|
692
|
-
const logPrefix = topLogPrefix + 'setUsername() - ',
|
|
693
|
-
userUuidBuffer = this.lUtils.uuidToBuffer(params.userUuid),
|
|
694
|
-
dbFields = [params.username, userUuidBuffer],
|
|
695
|
-
that = this,
|
|
696
|
-
sql = 'UPDATE user_users SET username = ? WHERE uuid = ?;';
|
|
697
|
-
|
|
698
|
-
if (cb === undefined || typeof cb !== 'function') {
|
|
699
|
-
cb = function () {};
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
if (userUuidBuffer === false) {
|
|
703
|
-
const err = new Error('Invalid user uuid');
|
|
704
|
-
that.log.warn(logPrefix + err.message);
|
|
705
|
-
that.emitter.emit(msgUuid, err);
|
|
706
|
-
return cb(err);
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
that.db.query(sql, dbFields, function (err) {
|
|
710
|
-
if (err) that.log.warn(logPrefix + err.message);
|
|
711
|
-
that.emitter.emit(msgUuid, err);
|
|
712
|
-
cb(err);
|
|
713
|
-
});
|
|
714
|
-
};
|
|
715
|
-
|
|
716
|
-
exports = module.exports = DataWriter;
|