Skip to content

Commit

Permalink
split check on create vs update
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Jan 22, 2025
1 parent 45c7ef7 commit 54dd49e
Showing 1 changed file with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import { UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT, UmbDocumentPublishingReposit
import { UmbDocumentValidationRepository } from '../repository/validation/index.js';
import { UMB_DOCUMENT_DETAIL_MODEL_VARIANT_SCAFFOLD, UMB_DOCUMENT_WORKSPACE_ALIAS } from './constants.js';
import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity';
import {
UMB_INVARIANT_CULTURE,
UmbVariantId,
type UmbEntityVariantModel,
type UmbEntityVariantOptionModel,
} from '@umbraco-cms/backoffice/variant';
import { UMB_INVARIANT_CULTURE, UmbVariantId } from '@umbraco-cms/backoffice/variant';
import {
type UmbPublishableWorkspaceContext,
UmbWorkspaceIsNewRedirectController,
Expand Down Expand Up @@ -75,8 +70,8 @@ export class UmbDocumentWorkspaceContext

#isTrashedContext = new UmbIsTrashedEntityContext(this);
#publishingContext?: typeof UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT.TYPE;
#variantOptions?: UmbEntityVariantOptionModel<UmbEntityVariantModel>[];
#userCanSave = false;
#userCanCreate = false;
#userCanUpdate = false;

constructor(host: UmbControllerHost) {
super(host, {
Expand All @@ -97,23 +92,24 @@ export class UmbDocumentWorkspaceContext
this.#publishingContext = context;
});

this.observe(this.variantOptions, (variantOptions) => {
const previousValue = this.#variantOptions;
this.#variantOptions = variantOptions;

if (previousValue?.length !== variantOptions.length) {
this.#setReadOnlyStateForUserSavePermission();
}
});
createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [
{
config: {
allOf: [UMB_USER_PERMISSION_DOCUMENT_CREATE],
},
onChange: (permitted: boolean) => {
this.#userCanCreate = permitted;
},
},
]);

createExtensionApiByAlias(this, UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS, [
{
config: {
oneOf: [UMB_USER_PERMISSION_DOCUMENT_CREATE, UMB_USER_PERMISSION_DOCUMENT_UPDATE],
allOf: [UMB_USER_PERMISSION_DOCUMENT_UPDATE],
},
onChange: (permitted: boolean) => {
this.#userCanSave = permitted;
this.#setReadOnlyStateForUserSavePermission();
this.#userCanUpdate = permitted;
},
},
]);
Expand All @@ -134,6 +130,7 @@ export class UmbDocumentWorkspaceContext
documentTypeUnique,
blueprintUnique,
);

new UmbWorkspaceIsNewRedirectController(
this,
this,
Expand All @@ -150,6 +147,12 @@ export class UmbDocumentWorkspaceContext
const documentTypeUnique = info.match.params.documentTypeUnique;
await this.create({ entityType: parentEntityType, unique: parentUnique }, documentTypeUnique);

this.#setReadOnlyStateForUserPermission(
UMB_USER_PERMISSION_DOCUMENT_CREATE,
this.#userCanCreate,
'You do not have permission to create documents.',
);

new UmbWorkspaceIsNewRedirectController(
this,
this,
Expand All @@ -160,10 +163,15 @@ export class UmbDocumentWorkspaceContext
{
path: UMB_EDIT_DOCUMENT_WORKSPACE_PATH_PATTERN.toString(),
component: () => import('./document-workspace-editor.element.js'),
setup: (_component, info) => {
setup: async (_component, info) => {
this.removeUmbControllerByAlias(UmbWorkspaceIsNewRedirectControllerAlias);
const unique = info.match.params.unique;
this.load(unique);
await this.load(unique);
this.#setReadOnlyStateForUserPermission(
UMB_USER_PERMISSION_DOCUMENT_UPDATE,
this.#userCanUpdate,
'You do not have permission to update documents.',
);

Check warning on line 174 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 (v15/dev)

❌ New issue: Large Method

UmbDocumentWorkspaceContext.constructor has 93 lines, threshold = 70. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
},
},
]);
Expand Down Expand Up @@ -359,22 +367,22 @@ export class UmbDocumentWorkspaceContext
return new UmbDocumentPropertyDatasetContext(host, this, variantId);
}

async #setReadOnlyStateForUserSavePermission() {
const identifier = 'UMB_SAVE_USER_PERMISSION_';
const uniques = this.#variantOptions?.map((variant) => identifier + variant.culture) || [];
async #setReadOnlyStateForUserPermission(identifier: string, permitted: boolean, message: string) {
const variants = this.getVariants();
const uniques = variants?.map((variant) => identifier + variant.culture) || [];

if (this.#userCanSave) {
if (permitted) {
this.readOnlyState?.removeStates(uniques);
return;
}

const variantIds = this.#variantOptions?.map((variant) => new UmbVariantId(variant.culture, variant.segment)) || [];
const variantIds = variants?.map((variant) => new UmbVariantId(variant.culture, variant.segment)) || [];

const readOnlyStates = variantIds.map((variantId) => {
return {
unique: identifier + variantId.culture,
variantId,
message: 'You do not have permission to edit to this document',
message,
};
});

Expand Down

0 comments on commit 54dd49e

Please sign in to comment.