bugfix to permissions

This commit is contained in:
Erik 2023-07-05 13:41:19 +03:00
parent 1c3b75579b
commit 137d77acaa
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 8 additions and 8 deletions

View File

@ -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();
}

View File

@ -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];
}