Skip to content

Commit 34029ff

Browse files
committed
ensure self permissions when updating an object
1 parent 33f009b commit 34029ff

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

OpenFlow/src/DatabaseConnection.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,29 +183,22 @@ export class DatabaseConnection {
183183
item._modifiedbyid = user._id;
184184
item._modified = item._created;
185185
var hasUser: Ace = item._acl.find(e => e._id === user._id);
186-
console.log("_acl: " + item._acl.length);
187186
if (hasUser === null || hasUser === undefined) {
188-
console.log("Adding self " + user.username + " to object " + item.name);
187+
if (collectionname != "audit") { this._logger.debug("Adding self " + user.username + " to object " + (item.name || item._name)); }
189188
item.addRight(user._id, user.name, [Rights.full_control]);
190-
} else {
191-
console.log("Self " + user.username + " allready have access to object " + item.name);
192189
}
193-
console.log("_acl: " + item._acl.length);
194190
if (collectionname != "audit") { this._logger.debug("adding " + (item.name || item._name) + " to database"); }
195191

196192
item = this.encryptentity<T>(item);
197-
console.log("_acl after encrypt: " + item._acl.length);
198193
if (!item._id) { item._id = new ObjectID().toHexString(); }
199194

200195
if (collectionname === "users" && item._type === "user" && item.hasOwnProperty("newpassword")) {
201196
(item as any).passwordhash = await Crypt.hash((item as any).newpassword);
202197
delete (item as any).newpassword;
203198
}
204199

205-
console.log("_acl before insert: " + item._acl.length);
206200
var result: InsertOneWriteOpResult = await this.db.collection(collectionname).insertOne(item);
207201
item = result.ops[0];
208-
console.log("_acl after insert: " + item._acl.length);
209202

210203
if (collectionname === "users" && item._type === "user") {
211204
var users: Role = await Role.FindByNameOrId("users", jwt);
@@ -258,6 +251,12 @@ export class DatabaseConnection {
258251
this.traversejsonencode(item);
259252
item = this.encryptentity<T>(item);
260253

254+
var hasUser: Ace = item._acl.find(e => e._id === user._id);
255+
if (hasUser === null || hasUser === undefined) {
256+
if (collectionname != "audit") { this._logger.debug("Adding self " + user.username + " to object " + (item.name || item._name)); }
257+
item.addRight(user._id, user.name, [Rights.full_control]);
258+
}
259+
261260
if (collectionname === "users" && item._type === "user" && item.hasOwnProperty("newpassword")) {
262261
(item as any).passwordhash = await Crypt.hash((item as any).newpassword);
263262
delete (item as any).newpassword;
@@ -287,6 +286,13 @@ export class DatabaseConnection {
287286
else if (exists.length > 1) {
288287
throw JSON.stringify(query) + " is not uniqe, more than 1 item in collection matches this";
289288
}
289+
var user: TokenUser = Crypt.verityToken(jwt);
290+
if (!this.hasAuthorization(user, item, "update")) { throw new Error("Access denied"); }
291+
var hasUser: Ace = item._acl.find(e => e._id === user._id);
292+
if (hasUser === null || hasUser === undefined) {
293+
if (collectionname != "audit") { this._logger.debug("Adding self " + user.username + " to object " + (item.name || item._name)); }
294+
item.addRight(user._id, user.name, [Rights.full_control]);
295+
}
290296
if (item._id !== null && item._id !== undefined && item._id !== "") {
291297
item = await this.UpdateOne(item, collectionname, jwt);
292298
} else {

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.89
1+
0.0.90

0 commit comments

Comments
 (0)