From 137d77acaa49a9441b0939d881d84615b8377ad2 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 5 Jul 2023 13:41:19 +0300 Subject: [PATCH] bugfix to permissions --- src/server/interfaces/Entity.ts | 6 +++--- src/util/PermissionManager.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/server/interfaces/Entity.ts b/src/server/interfaces/Entity.ts index 4be6e99..23a8afd 100644 --- a/src/server/interfaces/Entity.ts +++ b/src/server/interfaces/Entity.ts @@ -9,7 +9,7 @@ class Entity { static ProtectedFields = [ '_id' ]; - + #_id: string; #_name: string; #_disabled: boolean; @@ -43,7 +43,7 @@ class Entity this.#_id = id; this.#_name = name; this.#_disabled = disabled ?? false; - this.#_permissions = PermissionManager.merge(PermissionManager.DefaultPermissions, permissions || {}); + this.#_permissions = PermissionManager.merge(permissions || {}, PermissionManager.DefaultPermissions); this.#_createdTimestamp = createdTimestamp ?? Date.now(); this.#_cachedTimestamp = Date.now(); this.#_note = note ?? null; @@ -63,7 +63,7 @@ class Entity updatePermissions (perms: Permissions) { PermissionManager.validatePermissions(perms); - this.#_permissions = PermissionManager.merge(this.#_permissions, perms); + this.#_permissions = PermissionManager.merge(this.#_permissions, perms, true); return this.save(); } diff --git a/src/util/PermissionManager.ts b/src/util/PermissionManager.ts index 87031b5..0434675 100644 --- a/src/util/PermissionManager.ts +++ b/src/util/PermissionManager.ts @@ -100,25 +100,25 @@ class PermissionManager * @static * @param {Object} to Object into which to write the permissions * @param {Object} from Object from which to draw the permissions + * @param {boolean} overwrite If a permission exists in both objects, overwrite the permission in the target with the value from the source * @return {Object} Merged permissions * @memberof PermissionManager */ - static merge (to: Permissions, from: Permissions): Permissions + static merge (to: Permissions, from: Permissions, overwrite?: boolean): Permissions { const keys = Object.keys(from); for (const key of keys) { - if (typeof to[key] === 'object') + if (typeof to[key] === 'object') { PermissionManager.merge(to[key] as Permissions, from[key] as Permissions); } - else if (typeof from[key] === 'object') + else if (typeof from[key] === 'object') { to[key] = { default: to[key] || 0 }; PermissionManager.merge(to[key] as Permissions, from[key] as Permissions); - // eslint-disable-next-line no-undefined } - else // if (!(key in to)) + else if (!(key in to) || overwrite) { to[key] = from[key]; }