From f88685342320d8d9be38e11ea6df1ed7c3a0cca1 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 13 Jun 2025 13:45:33 +0200 Subject: [PATCH 1/3] Update document-workspace.context.ts --- .../workspace/document-workspace.context.ts | 81 ++++++++----------- 1 file changed, 32 insertions(+), 49 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts index 009d2698a840..081f5eaf9545 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts @@ -73,8 +73,6 @@ export class UmbDocumentWorkspaceContext #isTrashedContext = new UmbIsTrashedEntityContext(this); #publishingContext?: typeof UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT.TYPE; - #userCanCreate = false; - #userCanUpdate = false; constructor(host: UmbControllerHost) { super(host, { @@ -126,52 +124,18 @@ export class UmbDocumentWorkspaceContext this.#publishingContext = context; }); - createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [ - { - config: { - allOf: [UMB_USER_PERMISSION_DOCUMENT_CREATE], - }, - onChange: (permitted: boolean) => { - if (permitted === this.#userCanCreate) return; - this.#userCanCreate = permitted; - this.#setReadOnlyStateForUserPermission( - UMB_USER_PERMISSION_DOCUMENT_CREATE, - this.#userCanCreate, - 'You do not have permission to create documents.', - ); - }, - }, - ]); - - createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [ - { - config: { - allOf: [UMB_USER_PERMISSION_DOCUMENT_UPDATE], - }, - onChange: (permitted: boolean) => { - if (permitted === this.#userCanUpdate) return; - this.#userCanUpdate = permitted; - this.#setReadOnlyStateForUserPermission( - UMB_USER_PERMISSION_DOCUMENT_UPDATE, - this.#userCanUpdate, - 'You do not have permission to update documents.', - ); - }, - }, - ]); - - this.observe(this.variants, () => { - this.#setReadOnlyStateForUserPermission( - UMB_USER_PERMISSION_DOCUMENT_CREATE, - this.#userCanCreate, - 'You do not have permission to create documents.', - ); - - this.#setReadOnlyStateForUserPermission( - UMB_USER_PERMISSION_DOCUMENT_UPDATE, - this.#userCanUpdate, - 'You do not have permission to update documents.', - ); + this.observe(this.isNew, (isNew) => { + if (isNew) { + this.#enforceUserPermission( + UMB_USER_PERMISSION_DOCUMENT_CREATE, + 'You do not have permission to create documents.', + ); + } else { + this.#enforceUserPermission( + UMB_USER_PERMISSION_DOCUMENT_UPDATE, + 'You do not have permission to update documents.', + ); + } }); this.routes.setRoutes([ @@ -225,6 +189,22 @@ export class UmbDocumentWorkspaceContext ]); } + #enforceUserPermission(verb: string, message: string) { + // We set the initial permission state to false because the condition is false by default and only execute the callback if it changes. + this.#handleUserPermissionChange(verb, false, message); + + createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [ + { + config: { + allOf: [verb], + }, + onChange: (permitted: boolean) => { + this.#handleUserPermissionChange(verb, permitted, message); + }, + }, + ]); + } + override resetState(): void { super.resetState(); this.#isTrashedContext.setIsTrashed(false); @@ -425,7 +405,7 @@ export class UmbDocumentWorkspaceContext return new UmbDocumentPropertyDatasetContext(host, this, variantId); } - async #setReadOnlyStateForUserPermission(identifier: string, permitted: boolean, message: string) { + async #handleUserPermissionChange(identifier: string, permitted: boolean, message: string) { if (permitted) { this.readOnlyGuard?.removeRule(identifier); return; @@ -434,6 +414,9 @@ export class UmbDocumentWorkspaceContext this.readOnlyGuard?.addRule({ unique: identifier, message, + /* This guard is a bit backwards. The rule is permitted to be read-only. + If the user do not have permission, we set it to true = permitted to be read-only. */ + permitted: true, }); } From 4579add4bbc69c7e9ab4c4935c434dd249d7b9a3 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 13 Jun 2025 13:51:27 +0200 Subject: [PATCH 2/3] Update src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../documents/documents/workspace/document-workspace.context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts index 081f5eaf9545..00b7d17992d0 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts @@ -415,7 +415,7 @@ export class UmbDocumentWorkspaceContext unique: identifier, message, /* This guard is a bit backwards. The rule is permitted to be read-only. - If the user do not have permission, we set it to true = permitted to be read-only. */ + If the user does not have permission, we set it to true = permitted to be read-only. */ permitted: true, }); } From a90e2eb9d9f14a41cde53928dd5dc9993e067ff9 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 13 Jun 2025 13:55:56 +0200 Subject: [PATCH 3/3] add check for undefined --- .../documents/documents/workspace/document-workspace.context.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts index 081f5eaf9545..18a7f289a09d 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts @@ -125,6 +125,7 @@ export class UmbDocumentWorkspaceContext }); this.observe(this.isNew, (isNew) => { + if (isNew === undefined) return; if (isNew) { this.#enforceUserPermission( UMB_USER_PERMISSION_DOCUMENT_CREATE,