Skip to content

Fix readonly UI for create document user permission #19554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 30, 2025
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UmbDocumentTypeDetailRepository } from '../../document-types/repository/detail/document-type-detail.repository.js';

Check warning on line 1 in src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: String Heavy Function Arguments

In this module, 61.5% of all arguments to its 20 functions are strings. The threshold for string arguments is 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
import { UmbDocumentPropertyDatasetContext } from '../property-dataset-context/document-property-dataset.context.js';
import type { UmbDocumentDetailRepository } from '../repository/index.js';
import { UMB_DOCUMENT_DETAIL_REPOSITORY_ALIAS } from '../repository/index.js';
Expand Down Expand Up @@ -73,8 +73,6 @@

#isTrashedContext = new UmbIsTrashedEntityContext(this);
#publishingContext?: typeof UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT.TYPE;
#userCanCreate = false;
#userCanUpdate = false;

constructor(host: UmbControllerHost) {
super(host, {
Expand Down Expand Up @@ -126,52 +124,18 @@
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.',
);
}

Check notice on line 138 in src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Complex Method

UmbDocumentWorkspaceContext.constructor decreases in cyclomatic complexity from 13 to 12, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
});

this.routes.setRoutes([
Expand Down Expand Up @@ -225,6 +189,22 @@
]);
}

#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);
Expand Down Expand Up @@ -425,7 +405,7 @@
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;
Expand All @@ -434,6 +414,9 @@
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,
});
}

Expand Down
Loading