MCPcopy Create free account
hub / github.com/CloudBoost/cloudboost / verifyWriteACLAndUpdateVersion

Function verifyWriteACLAndUpdateVersion

helpers/custom.js:99–168  ·  view source on GitHub ↗
(appId, collectionName, document, accessList, isMasterKey)

Source from the content-addressed store, hash-verified

97 },
98
99 async verifyWriteACLAndUpdateVersion(appId, collectionName, document, accessList, isMasterKey) {
100 const deferred = q.defer();
101
102 try {
103 mongoService.document.get(appId, collectionName, document._id, accessList, isMasterKey).then((doc) => {
104 if (doc) {
105 if (document._version > 0) {
106 if (document._version >= doc._version) {
107 document._version += 1;
108 } else {
109 document._version = doc._version + 1;
110 }
111 } else {
112 document._version = doc._version + 1;
113 }
114 const acl = doc.ACL;
115 let status = false; // eslint-disable-line no-unused-vars
116
117 if (isMasterKey) {
118 status = true;
119 } else if (acl.write.allow.user.indexOf('all') > -1) {
120 status = true;
121 } else if (Object.keys(accessList).length === 0) {
122 if (acl.write.allow.user.indexOf('all') > -1) {
123 status = true;
124 } else deferred.reject(false);
125 } else if (accessList.userId && acl.write.allow.user.indexOf(accessList.userId) > -1) {
126 status = true;
127 } else if (accessList.userId && acl.write.deny.user.indexOf(accessList.userId) > -1) deferred.reject(false);
128 else {
129 for (let i = 0; i < accessList.roles.length; i++) {
130 if (acl.write.allow.role.indexOf(accessList.roles[i]) > -1) {
131 status = true;
132 }
133 }
134 deferred.reject(false);
135 }
136
137
138 const storedKeys = Object.keys(doc);
139 const documentKeys = Object.keys(document);
140 for (let i = 0; i < storedKeys.length; i++) {
141 if (documentKeys.indexOf(storedKeys[i]) === -1) {
142 document[storedKeys[i]] = doc[storedKeys[i]];
143 }
144 }
145 const obj = {};
146 obj.newDoc = document;
147 obj.oldDoc = doc;
148 deferred.resolve(obj);
149 } else {
150 document._version = 0;
151 const obj = {};
152 obj.newDoc = document;
153 obj.oldDoc = null;
154 deferred.resolve(obj);
155 }
156 }, () => {

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected