From 7b94d6deb3cc23929c3b59af35f72c2aad333832 Mon Sep 17 00:00:00 2001 From: Nishant0928 Date: Thu, 3 Aug 2023 14:12:54 +0530 Subject: [PATCH] WEBUI-990: prevent replacing and removing an attachment under retention --- .../nuxeo-replace-blob-button.js | 41 +++++++++++++++++-- .../nuxeo-document-attachments.js | 11 ++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/elements/nuxeo-document-actions/nuxeo-replace-blob-button.js b/elements/nuxeo-document-actions/nuxeo-replace-blob-button.js index f7d7cc1113..73e1f479a9 100644 --- a/elements/nuxeo-document-actions/nuxeo-replace-blob-button.js +++ b/elements/nuxeo-document-actions/nuxeo-replace-blob-button.js @@ -184,9 +184,44 @@ Polymer({ !this.isImmutable(doc) && !this.hasType(doc, 'Root') && !this.isTrashed(doc) && - !(doc.isRecord && this.xpath !== 'file:content') && - !(this.isUnderRetentionOrLegalHold(doc) && this.xpath === 'file:content') && - !(this.hasFacet(doc, 'ColdStorage') && this.hasContent(doc, 'coldstorage:coldContent')) + !(this.hasFacet(doc, 'ColdStorage') && this.hasContent(doc, 'coldstorage:coldContent')) && + !this._isPropUnderRetention(doc) ); }, + + _isPropUnderRetention(doc) { + if (doc && doc.isUnderRetentionOrLegalHold && doc.retainedProperties && doc.retainedProperties.length > 0) { + const { retainedProperties } = doc; + /* if retained property is multivalued attachment, and all files are to be retained, denoted by ‘*’, + then return true. + if retained property is multivalued attachment, but only a single file is to be retained, + then return true only for that file */ + return retainedProperties.find( + (prop) => + this._transformXpathRegex(prop, this.xpath) || + prop.startsWith(this.xpath) || + (prop.includes(this.xpath.split('/')[0]) && !prop.includes('/')), + ); + } + return false; + }, + + _transformXpathRegex(prop, xpath) { + const transformedArray = []; + const splitter = '/'; + const star = '*'; + if (prop.includes(star)) { + let xpathArray = xpath.split(splitter); + + for (let i = 0; i < xpathArray.length; i++) { + if (!Number.isNaN(parseInt(xpathArray[i], 10))) { + xpathArray[i] = star; + } + transformedArray.push(xpathArray[i]); + } + xpathArray = transformedArray; + xpath = xpathArray.join(splitter); + } + return prop === xpath; + }, }); diff --git a/elements/nuxeo-document-attachments/nuxeo-document-attachments.js b/elements/nuxeo-document-attachments/nuxeo-document-attachments.js index 8bedfd2bf0..ff34bd1724 100644 --- a/elements/nuxeo-document-attachments/nuxeo-document-attachments.js +++ b/elements/nuxeo-document-attachments/nuxeo-document-attachments.js @@ -143,10 +143,19 @@ Polymer({ this.hasPermission(doc, 'WriteProperties') && !this.isImmutable(doc) && !this.hasType(doc, 'Root') && - !this.isTrashed(doc) + !this.isTrashed(doc) && + !this._isPropUnderRetention(doc) ); }, + _isPropUnderRetention(doc) { + if (doc && doc.isUnderRetentionOrLegalHold && doc.retainedProperties && doc.retainedProperties.length > 0) { + return doc.retainedProperties.some((prop) => prop.startsWith(this.xpath)); + } + + return false; + }, + _computeFiles() { if (this._hasFiles(this._attachments)) { return this._attachments;