From 76f86c74ada88dec445cdd434fecce1b892d5a5c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 12 Aug 2022 22:37:52 +1200 Subject: [PATCH] Allow updating existing permission rows --- app/views/console/databases/collection.phtml | 14 ++-- public/scripts/permissions-matrix.js | 81 +++++++++++++------- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/app/views/console/databases/collection.phtml b/app/views/console/databases/collection.phtml index 1ecc699e7b..5e5d916916 100644 --- a/app/views/console/databases/collection.phtml +++ b/app/views/console/databases/collection.phtml @@ -510,6 +510,7 @@ $logs = $this->getParam('logs', null);
+
getParam('logs', null);
- + + @@ -565,16 +567,16 @@ $logs = $this->getParam('logs', null);

- + - + - + - + @@ -628,7 +630,7 @@ $logs = $this->getParam('logs', null);
Enabled -

With Document Security enabled, users will be able to access documents for which they have either Document or Collection permissions.

+

With Document Security enabled, users will be able to access documents for which they have been granted either Document or Collection permissions.

diff --git a/public/scripts/permissions-matrix.js b/public/scripts/permissions-matrix.js index a24983ae42..987f8eb666 100644 --- a/public/scripts/permissions-matrix.js +++ b/public/scripts/permissions-matrix.js @@ -7,33 +7,31 @@ this.rawPermissions = permissions; permissions.map(p => { - let parts = p.split('(') - let type = parts[0]; - let roles = parts[1] - .replace(')', '') - .replace(' ', '') - .split(','); - - roles.map(role => { - let index = -1 - let existing = this.permissions.find((p, idx) => { - if (p.role === role) { - index = idx; - return true; - } - }) - if (existing === undefined) { - this.permissions.push({ - role, - [type]: true, - }) + let { type, role } = this.parsePermission(p); + let index = -1; + let existing = this.permissions.find((p, idx) => { + if (p.role === role) { + index = idx; + return true; } - if (index !== -1) { - existing[type] = true; - this.permissions[index] = existing; - } - }); - }) + }) + if (existing === undefined) { + this.permissions.push({ + role, + [type]: true, + }); + } + if (index !== -1) { + existing[type] = true; + this.permissions[index] = existing; + } + }); + }, + parsePermission(permission) { + let parts = permission.split('('); + let type = parts[0]; + let role = parts[1].replace(')', '').replace(' ', ''); + return { type, role }; }, addPermission(role, read, create, update, xdelete) { if (read) this.rawPermissions.push(`read(${role})`); @@ -49,13 +47,41 @@ xdelete }); - this.reset() + this.reset(); + }, + updatePermission(index) { + // Because the x-model does not update before the click event, + // we setTimeout to give Alpine enough time to update the model. + setTimeout(() => { + const permission = this.permissions[index]; + for (const key of Object.keys(permission)) { + if (key === 'role') { + continue; + } + const parsedKey = this.parseKey(key); + if (permission[key]) { + if (!this.rawPermissions.includes(`${parsedKey}(${permission.role})`)) { + this.rawPermissions.push(`${parsedKey}(${permission.role})`); + } + } else { + this.rawPermissions = this.rawPermissions.filter(p => { + return !p.includes(`${parsedKey}(${permission.role})`); + }); + } + } + }); }, removePermission(index) { let row = this.permissions.splice(index, 1); if (row.length === 1) { this.rawPermissions = this.rawPermissions.filter(p => !p.includes(row[0].role)); } + }, + parseKey(key) { + if (key === 'xdelete') { + return 'delete'; + } + return key; } })); Alpine.data('permissionsRow', () => ({ @@ -64,7 +90,6 @@ create: false, update: false, xdelete: false, - reset() { this.role = ''; this.read = this.create = this.update = this.xdelete = false;