Skip to content

Commit e135599

Browse files
Fix readonly UI for create document user permission (#19554)
* Update document-workspace.context.ts * Update src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts Co-authored-by: Copilot <[email protected]> * add check for undefined --------- Co-authored-by: Copilot <[email protected]>
1 parent d6a66bc commit e135599

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ export class UmbDocumentWorkspaceContext
7373

7474
#isTrashedContext = new UmbIsTrashedEntityContext(this);
7575
#publishingContext?: typeof UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT.TYPE;
76-
#userCanCreate = false;
77-
#userCanUpdate = false;
7876

7977
constructor(host: UmbControllerHost) {
8078
super(host, {
@@ -126,52 +124,19 @@ export class UmbDocumentWorkspaceContext
126124
this.#publishingContext = context;
127125
});
128126

129-
createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [
130-
{
131-
config: {
132-
allOf: [UMB_USER_PERMISSION_DOCUMENT_CREATE],
133-
},
134-
onChange: (permitted: boolean) => {
135-
if (permitted === this.#userCanCreate) return;
136-
this.#userCanCreate = permitted;
137-
this.#setReadOnlyStateForUserPermission(
138-
UMB_USER_PERMISSION_DOCUMENT_CREATE,
139-
this.#userCanCreate,
140-
'You do not have permission to create documents.',
141-
);
142-
},
143-
},
144-
]);
145-
146-
createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [
147-
{
148-
config: {
149-
allOf: [UMB_USER_PERMISSION_DOCUMENT_UPDATE],
150-
},
151-
onChange: (permitted: boolean) => {
152-
if (permitted === this.#userCanUpdate) return;
153-
this.#userCanUpdate = permitted;
154-
this.#setReadOnlyStateForUserPermission(
155-
UMB_USER_PERMISSION_DOCUMENT_UPDATE,
156-
this.#userCanUpdate,
157-
'You do not have permission to update documents.',
158-
);
159-
},
160-
},
161-
]);
162-
163-
this.observe(this.variants, () => {
164-
this.#setReadOnlyStateForUserPermission(
165-
UMB_USER_PERMISSION_DOCUMENT_CREATE,
166-
this.#userCanCreate,
167-
'You do not have permission to create documents.',
168-
);
169-
170-
this.#setReadOnlyStateForUserPermission(
171-
UMB_USER_PERMISSION_DOCUMENT_UPDATE,
172-
this.#userCanUpdate,
173-
'You do not have permission to update documents.',
174-
);
127+
this.observe(this.isNew, (isNew) => {
128+
if (isNew === undefined) return;
129+
if (isNew) {
130+
this.#enforceUserPermission(
131+
UMB_USER_PERMISSION_DOCUMENT_CREATE,
132+
'You do not have permission to create documents.',
133+
);
134+
} else {
135+
this.#enforceUserPermission(
136+
UMB_USER_PERMISSION_DOCUMENT_UPDATE,
137+
'You do not have permission to update documents.',
138+
);
139+
}
175140
});
176141

177142
this.routes.setRoutes([
@@ -225,6 +190,22 @@ export class UmbDocumentWorkspaceContext
225190
]);
226191
}
227192

193+
#enforceUserPermission(verb: string, message: string) {
194+
// We set the initial permission state to false because the condition is false by default and only execute the callback if it changes.
195+
this.#handleUserPermissionChange(verb, false, message);
196+
197+
createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [
198+
{
199+
config: {
200+
allOf: [verb],
201+
},
202+
onChange: (permitted: boolean) => {
203+
this.#handleUserPermissionChange(verb, permitted, message);
204+
},
205+
},
206+
]);
207+
}
208+
228209
override resetState(): void {
229210
super.resetState();
230211
this.#isTrashedContext.setIsTrashed(false);
@@ -425,7 +406,7 @@ export class UmbDocumentWorkspaceContext
425406
return new UmbDocumentPropertyDatasetContext(host, this, variantId);
426407
}
427408

428-
async #setReadOnlyStateForUserPermission(identifier: string, permitted: boolean, message: string) {
409+
async #handleUserPermissionChange(identifier: string, permitted: boolean, message: string) {
429410
if (permitted) {
430411
this.readOnlyGuard?.removeRule(identifier);
431412
return;
@@ -434,6 +415,9 @@ export class UmbDocumentWorkspaceContext
434415
this.readOnlyGuard?.addRule({
435416
unique: identifier,
436417
message,
418+
/* This guard is a bit backwards. The rule is permitted to be read-only.
419+
If the user does not have permission, we set it to true = permitted to be read-only. */
420+
permitted: true,
437421
});
438422
}
439423

0 commit comments

Comments
 (0)