fng-audit 0.12.0-beta.355 → 0.12.0-beta.357

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.
@@ -127,12 +127,45 @@ function extractPossiblyNestedPath(tree, obj) {
127
127
  return obj;
128
128
  }
129
129
  function stripAttribFromObject(attrib, obj) {
130
- var tree = attrib.split('.');
131
- var last = tree.pop();
132
- obj = extractPossiblyNestedPath(tree, obj);
133
- if (obj) {
134
- delete obj[last];
130
+ stripPathFromObject(attrib.split('.'), obj);
131
+ }
132
+ // Removes the path from obj, looking inside any arrays on the way, and coping with the shapes auditFromUpdate
133
+ // builds from update operators: keys containing dots (from $push of 'a.b') and array positions ('a.0.b', 'a.$.b')
134
+ function stripPathFromObject(tree, obj) {
135
+ if (!obj || typeof obj !== 'object') {
136
+ return;
137
+ }
138
+ if (Array.isArray(obj)) {
139
+ obj.forEach(function (elm) { return stripPathFromObject(tree, elm); });
140
+ return;
135
141
  }
142
+ delete obj[tree.join('.')];
143
+ for (var i = 1; i < tree.length; i++) {
144
+ var key = tree.slice(0, i).join('.');
145
+ if (key in obj) {
146
+ stripPathFromObject(tree.slice(i), obj[key]);
147
+ }
148
+ }
149
+ for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
150
+ var key = _a[_i];
151
+ if (/^\d+$/.test(key) || key[0] === '$') {
152
+ stripPathFromObject(tree, obj[key]);
153
+ }
154
+ }
155
+ }
156
+ function getSecurePaths(schema, prefix) {
157
+ if (prefix === void 0) { prefix = ''; }
158
+ var retVal = [];
159
+ schema.eachPath(function (path, schemaType) {
160
+ var _a;
161
+ if ((_a = schemaType.options) === null || _a === void 0 ? void 0 : _a.secure) {
162
+ retVal.push(prefix + path);
163
+ }
164
+ else if (schemaType.schema) {
165
+ retVal.push.apply(retVal, getSecurePaths(schemaType.schema, prefix + path + '.'));
166
+ }
167
+ });
168
+ return retVal;
136
169
  }
137
170
  export function clean(obj, delFunc) {
138
171
  delFunc = delFunc || function (obj, key) { delete obj[key]; };
@@ -335,7 +368,7 @@ function auditFromObject(doc, orig, updated, options, next) {
335
368
  var stdOrig_1 = clean(JSON.parse(JSON.stringify(orig)));
336
369
  var stdUpdated_1 = clean(JSON.parse(JSON.stringify(updated)));
337
370
  var suppressedChanges_1 = ['updatedAt', '__v'];
338
- ['strip', 'hidden'].forEach(function (prop) {
371
+ ['strip', 'hidden', 'secure'].forEach(function (prop) {
339
372
  if (options[prop] && options[prop].length > 0) {
340
373
  suppressedChanges_1 = suppressedChanges_1.concat(options[prop]);
341
374
  }
@@ -423,32 +456,28 @@ function auditFromObject(doc, orig, updated, options, next) {
423
456
  next();
424
457
  }
425
458
  }
426
- function assignPossiblyNested(dest, src, attrib) {
427
- var srcVal;
428
- var cur;
459
+ // Copies the value at attrib (which may be a dotted path) in src to the same path in dest
460
+ function copyPossiblyNested(dest, src, attrib) {
429
461
  var tree = attrib.split('.');
430
462
  var last = tree.pop();
431
- if (typeof src !== "object") {
432
- // We are assigning a value
433
- srcVal = src;
463
+ var cur = extractPossiblyNestedPath(tree, src);
464
+ if (cur && cur[last] !== undefined) {
465
+ assignPossiblyNested(dest, cur[last], attrib);
434
466
  }
435
- else {
436
- // we are copying from an analogous object
437
- cur = extractPossiblyNestedPath(tree, src);
438
- if (cur) {
439
- srcVal = cur[last];
467
+ }
468
+ // Sets attrib (which may be a dotted path) in dest to srcVal. srcVal can be anything, including falsy values,
469
+ // Dates, ObjectIds, arrays and subdocuments.
470
+ function assignPossiblyNested(dest, srcVal, attrib) {
471
+ var cur = dest;
472
+ var tree = attrib.split('.');
473
+ var last = tree.pop();
474
+ tree.forEach(function (branch) {
475
+ if (!cur[branch]) {
476
+ cur[branch] = {};
440
477
  }
441
- }
442
- if (srcVal) {
443
- cur = dest;
444
- tree.forEach(function (branch) {
445
- if (!cur[branch]) {
446
- cur[branch] = {};
447
- }
448
- cur = cur[branch];
449
- });
450
- cur[last] = srcVal;
451
- }
478
+ cur = cur[branch];
479
+ });
480
+ cur[last] = srcVal;
452
481
  }
453
482
  function auditFromUpdate(docUpdate, options, next) {
454
483
  if (!Audit) {
@@ -481,7 +510,7 @@ function auditFromUpdate(docUpdate, options, next) {
481
510
  if (key === '$set') {
482
511
  assignPossiblyNested(updated, queryObject_1._update.$set[attrib], attrib);
483
512
  }
484
- assignPossiblyNested(original, currentObject, attrib);
513
+ copyPossiblyNested(original, currentObject, attrib);
485
514
  });
486
515
  switch (key) {
487
516
  case '$set':
@@ -508,7 +537,7 @@ function auditFromUpdate(docUpdate, options, next) {
508
537
  }
509
538
  else {
510
539
  // an assignment
511
- assignPossiblyNested(original, currentObject, key);
540
+ copyPossiblyNested(original, currentObject, key);
512
541
  }
513
542
  });
514
543
  }
@@ -546,6 +575,13 @@ export function getUpdatesSince(modelName, id, since) {
546
575
  export function plugin(schema, options) {
547
576
  options = options || {};
548
577
  options.strip = options.strip || [];
578
+ // Worked out on first use rather than now, so paths added to the schema after this plugin are included
579
+ function getStrippedFields(collectionName) {
580
+ getHiddenFields(collectionName, options);
581
+ if (!options.secure) {
582
+ options.secure = getSecurePaths(schema);
583
+ }
584
+ }
549
585
  /*
550
586
  Document middleware. "this" is the document
551
587
  */
@@ -595,7 +631,7 @@ export function plugin(schema, options) {
595
631
  else {
596
632
  var that_1 = this;
597
633
  try {
598
- getHiddenFields(that_1.constructor.collection.collectionName, options);
634
+ getStrippedFields(that_1.constructor.collection.collectionName);
599
635
  that_1.constructor.findOne({ _id: that_1._id })
600
636
  .then(function (original) {
601
637
  auditFromObject(that_1, original, that_1, options, next);
@@ -629,7 +665,7 @@ export function plugin(schema, options) {
629
665
  else {
630
666
  try {
631
667
  var that = this;
632
- getHiddenFields(that.constructor.collection.collectionName, options);
668
+ getStrippedFields(that.constructor.collection.collectionName);
633
669
  auditFromObject(that, that, {}, options, next);
634
670
  }
635
671
  catch (e) {
@@ -650,7 +686,7 @@ export function plugin(schema, options) {
650
686
  }
651
687
  else {
652
688
  try {
653
- getHiddenFields(this.mongooseCollection.collectionName, options);
689
+ getStrippedFields(this.mongooseCollection.collectionName);
654
690
  auditFromUpdate(this, options, next);
655
691
  }
656
692
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fng-audit",
3
- "version": "0.12.0-beta.355",
3
+ "version": "0.12.0-beta.357",
4
4
  "type": "module",
5
5
  "description": "Client part of forms-angular audit plugin",
6
6
  "homepage": "https://www.forms-angular.org/#/plugins",
@@ -28,7 +28,7 @@
28
28
  "@types/mocha": "^10.0.7",
29
29
  "angular": "npm:@brickhouse-tech/angular-lts@^1.10.4",
30
30
  "chai": "=4.3.10",
31
- "forms-angular": "0.12.0-beta.355",
31
+ "forms-angular": "0.12.0-beta.357",
32
32
  "grunt": "^1.6.1",
33
33
  "grunt-angular-templates": "^1.2.0",
34
34
  "grunt-contrib-concat": "^2.1.0",