fng-audit 0.12.0-beta.219 → 0.12.0-beta.220
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/dist/server/fng-audit.js +178 -93
- package/package.json +2 -2
package/dist/server/fng-audit.js
CHANGED
|
@@ -11,6 +11,42 @@ var __assign = (this && this.__assign) || function () {
|
|
|
11
11
|
};
|
|
12
12
|
return __assign.apply(this, arguments);
|
|
13
13
|
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
24
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
25
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
26
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
|
+
function step(op) {
|
|
28
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
+
while (_) try {
|
|
30
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
|
+
switch (op[0]) {
|
|
33
|
+
case 0: case 1: t = op; break;
|
|
34
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
35
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
36
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
37
|
+
default:
|
|
38
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
39
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
40
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
41
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
42
|
+
if (t[2]) _.ops.pop();
|
|
43
|
+
_.trys.pop(); continue;
|
|
44
|
+
}
|
|
45
|
+
op = body.call(thisArg, _);
|
|
46
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
47
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
|
+
}
|
|
49
|
+
};
|
|
14
50
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
51
|
exports.plugin = exports.auditAdHocEvent = exports.getSnapshot = exports.getVersion = exports.getAuditTrail = exports.clean = exports.controller = exports.Audit = void 0;
|
|
16
52
|
var jsondiffpatch = require("jsondiffpatch");
|
|
@@ -236,21 +272,36 @@ function getSnapshot(model, id, snapshotDt, doCleaning, callback) {
|
|
|
236
272
|
}
|
|
237
273
|
exports.getSnapshot = getSnapshot;
|
|
238
274
|
function auditAdHocEvent(user, description, details) {
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
275
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
276
|
+
function cleanKeys(obj) {
|
|
277
|
+
if (typeof obj === "object") {
|
|
278
|
+
Object.keys(obj).forEach(function (k) {
|
|
279
|
+
cleanKeys(obj[k]);
|
|
280
|
+
var safeKey = k.replace(/\./g, '-').replace(/^\$/, '#');
|
|
281
|
+
if (safeKey !== k) {
|
|
282
|
+
obj[safeKey] = obj[k];
|
|
283
|
+
delete obj[k];
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
249
287
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
288
|
+
var copyDets, auditRec, xtra;
|
|
289
|
+
return __generator(this, function (_a) {
|
|
290
|
+
switch (_a.label) {
|
|
291
|
+
case 0:
|
|
292
|
+
copyDets = cloneDeep(details);
|
|
293
|
+
cleanKeys(copyDets); // Make sure mongoose doesn't barf on composite keys by putting quotes round them
|
|
294
|
+
auditRec = { user: user, op: description, dets: copyDets };
|
|
295
|
+
return [4 /*yield*/, auditOptions.addPropFunc(auditRec, null, null, null, {})];
|
|
296
|
+
case 1:
|
|
297
|
+
xtra = _a.sent();
|
|
298
|
+
if (xtra) {
|
|
299
|
+
auditRec.xtra = xtra;
|
|
300
|
+
}
|
|
301
|
+
return [2 /*return*/, exports.Audit.create(auditRec)];
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
});
|
|
254
305
|
}
|
|
255
306
|
exports.auditAdHocEvent = auditAdHocEvent;
|
|
256
307
|
function getPseudoField(name, updated, orig) {
|
|
@@ -268,6 +319,7 @@ function getPseudoField(name, updated, orig) {
|
|
|
268
319
|
return retVal;
|
|
269
320
|
}
|
|
270
321
|
function auditFromObject(doc, orig, updated, options, next) {
|
|
322
|
+
var _this = this;
|
|
271
323
|
if (exports.Audit) {
|
|
272
324
|
var user_1 = getPseudoField('user', updated, orig) || getPseudoField('_usr', updated, orig);
|
|
273
325
|
var op_1 = getPseudoField('op', updated, orig) || orig.$op;
|
|
@@ -308,36 +360,47 @@ function auditFromObject(doc, orig, updated, options, next) {
|
|
|
308
360
|
exports.Audit.findOne(criteria)
|
|
309
361
|
.sort("-ver")
|
|
310
362
|
.exec()
|
|
311
|
-
.then(function (prevAudit) {
|
|
312
|
-
var auditRec
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
363
|
+
.then(function (prevAudit) { return __awaiter(_this, void 0, void 0, function () {
|
|
364
|
+
var auditRec, invariantAuditRec, _a;
|
|
365
|
+
return __generator(this, function (_b) {
|
|
366
|
+
switch (_b.label) {
|
|
367
|
+
case 0:
|
|
368
|
+
auditRec = {
|
|
369
|
+
c: c_1,
|
|
370
|
+
cId: cId_1,
|
|
371
|
+
ver: prevAudit ? prevAudit.ver + 1 : 0,
|
|
372
|
+
chg: chg_1
|
|
373
|
+
};
|
|
374
|
+
if (user_1) {
|
|
375
|
+
auditRec.user = user_1;
|
|
376
|
+
}
|
|
377
|
+
if (op_1) {
|
|
378
|
+
auditRec.op = op_1;
|
|
379
|
+
}
|
|
380
|
+
if (!auditOptions.addPropFunc) return [3 /*break*/, 2];
|
|
381
|
+
invariantAuditRec = __assign({}, auditRec);
|
|
382
|
+
_a = auditRec;
|
|
383
|
+
return [4 /*yield*/, auditOptions.addPropFunc(invariantAuditRec, doc, orig, updated, options)];
|
|
384
|
+
case 1:
|
|
385
|
+
_a.xtra = _b.sent();
|
|
386
|
+
_b.label = 2;
|
|
387
|
+
case 2:
|
|
388
|
+
exports.Audit.create(auditRec)
|
|
389
|
+
.then(function () {
|
|
390
|
+
// nothing to do
|
|
391
|
+
})
|
|
392
|
+
.catch(function (err) {
|
|
393
|
+
if (err) {
|
|
394
|
+
console.log("Error creating audit object: ".concat(err.message));
|
|
395
|
+
}
|
|
396
|
+
})
|
|
397
|
+
.finally(function () {
|
|
398
|
+
next();
|
|
399
|
+
});
|
|
400
|
+
return [2 /*return*/];
|
|
335
401
|
}
|
|
336
|
-
})
|
|
337
|
-
.finally(function () {
|
|
338
|
-
next();
|
|
339
402
|
});
|
|
340
|
-
})
|
|
403
|
+
}); })
|
|
341
404
|
.catch(function (err) {
|
|
342
405
|
return next(err);
|
|
343
406
|
});
|
|
@@ -467,58 +530,80 @@ function plugin(schema, options) {
|
|
|
467
530
|
Document middleware. "this" is the document
|
|
468
531
|
*/
|
|
469
532
|
schema.pre("save", function (next) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
533
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
534
|
+
var user, auditRec, xtra, that_1;
|
|
535
|
+
return __generator(this, function (_a) {
|
|
536
|
+
switch (_a.label) {
|
|
537
|
+
case 0:
|
|
538
|
+
if (!(this._noAudit || !exports.Audit)) return [3 /*break*/, 1];
|
|
539
|
+
next();
|
|
540
|
+
return [3 /*break*/, 6];
|
|
541
|
+
case 1:
|
|
542
|
+
if (!this.isNew) return [3 /*break*/, 5];
|
|
543
|
+
user = getPseudoField('user', this) || getPseudoField('_usr', this);
|
|
544
|
+
if (!user) return [3 /*break*/, 3];
|
|
545
|
+
auditRec = {
|
|
546
|
+
c: this.constructor.modelName,
|
|
547
|
+
cId: this._id,
|
|
548
|
+
op: 'create',
|
|
549
|
+
user: user
|
|
550
|
+
};
|
|
551
|
+
return [4 /*yield*/, auditOptions.addPropFunc(auditRec, this, null, null, {})];
|
|
552
|
+
case 2:
|
|
553
|
+
xtra = _a.sent();
|
|
554
|
+
if (xtra) {
|
|
555
|
+
auditRec.xtra = xtra;
|
|
556
|
+
}
|
|
557
|
+
exports.Audit.create(auditRec)
|
|
558
|
+
.then(function () {
|
|
559
|
+
// nothing to do
|
|
560
|
+
})
|
|
561
|
+
.catch(function (err) {
|
|
562
|
+
console.log("Error creating audit object: ".concat(err.message));
|
|
563
|
+
})
|
|
564
|
+
.finally(function () {
|
|
565
|
+
next();
|
|
566
|
+
});
|
|
567
|
+
return [3 /*break*/, 4];
|
|
568
|
+
case 3:
|
|
569
|
+
next();
|
|
570
|
+
_a.label = 4;
|
|
571
|
+
case 4: return [3 /*break*/, 6];
|
|
572
|
+
case 5:
|
|
573
|
+
that_1 = this;
|
|
574
|
+
try {
|
|
575
|
+
getHiddenFields(that_1.constructor.collection.collectionName, options);
|
|
576
|
+
that_1.constructor.findOne({ _id: that_1._id })
|
|
577
|
+
.then(function (original) {
|
|
578
|
+
auditFromObject(that_1, original, that_1, options, next);
|
|
579
|
+
})
|
|
580
|
+
.catch(function (err) {
|
|
581
|
+
if (auditOptions.errorHandler) {
|
|
582
|
+
auditOptions.errorHandler(err.message);
|
|
583
|
+
}
|
|
584
|
+
next();
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
catch (e) {
|
|
588
|
+
if (auditOptions.errorHandler) {
|
|
589
|
+
auditOptions.errorHandler(e.message);
|
|
590
|
+
}
|
|
591
|
+
next();
|
|
592
|
+
}
|
|
593
|
+
_a.label = 6;
|
|
594
|
+
case 6: return [2 /*return*/];
|
|
516
595
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
}
|
|
596
|
+
});
|
|
597
|
+
});
|
|
520
598
|
});
|
|
521
|
-
|
|
599
|
+
// prior to mongoose 7, pre("remove") was used to intercept deleted documents. in mongoose 7, remove() no longer
|
|
600
|
+
// exists, and instead, deleteOne() is used both to delete a document that has already been retrieved from the
|
|
601
|
+
// collection (i.e., doc.deleteOne()) and to delete a single document using a query (i.e., model.deleteOne({criteria})).
|
|
602
|
+
// to distinguish between these two cases in middleware (because obviously one has a document, and the other
|
|
603
|
+
// just the query conditions), we need to specify here whether we're dealing with the document case, the
|
|
604
|
+
// query case, or both.
|
|
605
|
+
// the document case is dealt with here, the query case, below (see schema.pre("deleteOne", ...) again, later)
|
|
606
|
+
schema.pre("deleteOne", { document: true, query: false }, function (next) {
|
|
522
607
|
if (this._noAudit || !exports.Audit) {
|
|
523
608
|
next();
|
|
524
609
|
}
|
|
@@ -583,7 +668,7 @@ function plugin(schema, options) {
|
|
|
583
668
|
schema.pre("updateOne", doUpdateHandling());
|
|
584
669
|
schema.pre("updateMany", doUpdateHandling());
|
|
585
670
|
schema.pre("findOneAndRemove", doUpdateHandling());
|
|
586
|
-
schema.pre("deleteOne", doUpdateHandling());
|
|
671
|
+
schema.pre("deleteOne", { document: false, query: true }, doUpdateHandling());
|
|
587
672
|
schema.pre("findOneAndDelete", doUpdateHandling());
|
|
588
673
|
}
|
|
589
674
|
exports.plugin = plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fng-audit",
|
|
3
|
-
"version": "0.12.0-beta.
|
|
3
|
+
"version": "0.12.0-beta.220",
|
|
4
4
|
"description": "Client part of forms-angular audit plugin",
|
|
5
5
|
"homepage": "https://www.forms-angular.org/#/plugins",
|
|
6
6
|
"files": [
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@types/mocha": "10.0.1",
|
|
27
27
|
"angular": "^1.8.3",
|
|
28
28
|
"chai": "4.3.7",
|
|
29
|
-
"forms-angular": "0.12.0-beta.
|
|
29
|
+
"forms-angular": "0.12.0-beta.220",
|
|
30
30
|
"grunt": "^1.5.3",
|
|
31
31
|
"grunt-angular-templates": "^1.2.0",
|
|
32
32
|
"grunt-contrib-concat": "2.1.0",
|