From 4bf1e368024c4a2c48b8c3a59f8b676e126cdf40 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 22 Oct 2024 09:28:39 +0200 Subject: [PATCH 1/4] add methods to get current user data --- .../user/current-user/current-user.context.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/packages/user/current-user/current-user.context.ts b/src/packages/user/current-user/current-user.context.ts index b6836e70b7..4a18f94086 100644 --- a/src/packages/user/current-user/current-user.context.ts +++ b/src/packages/user/current-user/current-user.context.ts @@ -83,6 +83,74 @@ export class UmbCurrentUserContext extends UmbContextBase return currentUser?.isAdmin ?? false; } + getAllowedSection() { + return this.#currentUser.getValue()?.allowedSections; + } + + getAvatarUrls() { + return this.#currentUser.getValue()?.avatarUrls; + } + + getDocumentStartNodeUniques() { + return this.#currentUser.getValue()?.documentStartNodeUniques; + } + + getEmail() { + return this.#currentUser.getValue()?.email; + } + + getFallbackPermissions() { + return this.#currentUser.getValue()?.fallbackPermissions; + } + + getHasAccessToAllLanguages() { + return this.#currentUser.getValue()?.hasAccessToAllLanguages; + } + + getHasAccessToSensitiveData() { + return this.#currentUser.getValue()?.hasAccessToSensitiveData; + } + + getHasDocumentRootAccess() { + return this.#currentUser.getValue()?.hasDocumentRootAccess; + } + + getHasMediaRootAccess() { + return this.#currentUser.getValue()?.hasMediaRootAccess; + } + + getIsAdmin() { + return this.#currentUser.getValue()?.isAdmin; + } + + getLanguageIsoCode() { + return this.#currentUser.getValue()?.languageIsoCode; + } + + getLanguages() { + return this.#currentUser.getValue()?.languages; + } + + getMediaStartNodeUniques() { + return this.#currentUser.getValue()?.mediaStartNodeUniques; + } + + getName() { + return this.#currentUser.getValue()?.name; + } + + getPermissions() { + return this.#currentUser.getValue()?.permissions; + } + + getUnique() { + return this.#currentUser.getValue()?.unique; + } + + getUserName() { + return this.#currentUser.getValue()?.userName; + } + #observeIsAuthorized() { if (!this.#authContext) return; this.observe(this.#authContext.isAuthorized, (isAuthorized) => { From e8ae4c931ce6478e50b11d84105a9f03c4f3d74a Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 22 Oct 2024 09:35:26 +0200 Subject: [PATCH 2/4] add jsdocs --- .../user/current-user/current-user.context.ts | 101 +++++++++++++++--- 1 file changed, 85 insertions(+), 16 deletions(-) diff --git a/src/packages/user/current-user/current-user.context.ts b/src/packages/user/current-user/current-user.context.ts index 4a18f94086..d31467cce3 100644 --- a/src/packages/user/current-user/current-user.context.ts +++ b/src/packages/user/current-user/current-user.context.ts @@ -11,6 +11,7 @@ import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registr import { UMB_SECTION_PATH_PATTERN } from '@umbraco-cms/backoffice/section'; import { UMB_APP_CONTEXT } from '@umbraco-cms/backoffice/app'; import { ensurePathEndsWithSlash } from '@umbraco-cms/backoffice/utils'; +import type { UmbReferenceByUnique } from '@umbraco-cms/backoffice/models'; export class UmbCurrentUserContext extends UmbContextBase { #currentUser = new UmbObjectState(undefined); @@ -83,71 +84,139 @@ export class UmbCurrentUserContext extends UmbContextBase return currentUser?.isAdmin ?? false; } - getAllowedSection() { + /** + * Get the allowed sections for the current user + * @returns {Array | undefined} The allowed sections for the current user + */ + getAllowedSection(): Array | undefined { return this.#currentUser.getValue()?.allowedSections; } - getAvatarUrls() { + /** + * Get the avatar urls for the current user + * @returns {Array | undefined} The avatar urls for the current user + */ + getAvatarUrls(): Array | undefined { return this.#currentUser.getValue()?.avatarUrls; } - getDocumentStartNodeUniques() { + /** + * Get the document start node uniques for the current user + * @returns {Array | undefined} The document start node uniques for the current user + */ + getDocumentStartNodeUniques(): Array | undefined { return this.#currentUser.getValue()?.documentStartNodeUniques; } - getEmail() { + /** + * Get the email for the current user + * @returns {string | undefined} The email for the current user + */ + getEmail(): string | undefined { return this.#currentUser.getValue()?.email; } - getFallbackPermissions() { + /** + * Get the fallback permissions for the current user + * @returns {Array | undefined} The fallback permissions for the current user + */ + getFallbackPermissions(): Array | undefined { return this.#currentUser.getValue()?.fallbackPermissions; } - getHasAccessToAllLanguages() { + /** + * Get if the current user has access to all languages + * @returns {boolean | undefined} True if the current user has access to all languages, otherwise false + */ + getHasAccessToAllLanguages(): boolean | undefined { return this.#currentUser.getValue()?.hasAccessToAllLanguages; } - getHasAccessToSensitiveData() { + /** + * Get if the current user has access to sensitive data + * @returns {boolean | undefined} True if the current user has access to sensitive data, otherwise false + */ + getHasAccessToSensitiveData(): boolean | undefined { return this.#currentUser.getValue()?.hasAccessToSensitiveData; } - getHasDocumentRootAccess() { + /** + * Get if the current user has document root access + * @returns {boolean | undefined} True if the current user has document root access, otherwise false + */ + getHasDocumentRootAccess(): boolean | undefined { return this.#currentUser.getValue()?.hasDocumentRootAccess; } - getHasMediaRootAccess() { + /** + * Get if the current user has media root access + * @returns {boolean | undefined} True if the current user has media root access, otherwise false + */ + getHasMediaRootAccess(): boolean | undefined { return this.#currentUser.getValue()?.hasMediaRootAccess; } - getIsAdmin() { + /** + * Get if the current user is an admin + * @returns {boolean | undefined} True if the current user is an admin, otherwise false + */ + getIsAdmin(): boolean | undefined { return this.#currentUser.getValue()?.isAdmin; } - getLanguageIsoCode() { + /** + * Get the language iso code for the current user + * @returns {string | undefined} The language iso code for the current user + */ + getLanguageIsoCode(): string | undefined { return this.#currentUser.getValue()?.languageIsoCode; } - getLanguages() { + /** + * Get the languages for the current user + * @returns {Array | undefined} The languages for the current user + */ + getLanguages(): Array | undefined { return this.#currentUser.getValue()?.languages; } - getMediaStartNodeUniques() { + /** + * Get the media start node uniques for the current user + * @returns {Array | undefined} The media start node uniques for the current user + */ + getMediaStartNodeUniques(): Array | undefined { return this.#currentUser.getValue()?.mediaStartNodeUniques; } - getName() { + /** + * Get the name for the current user + * @returns {string | undefined} The name for the current user + */ + getName(): string | undefined { return this.#currentUser.getValue()?.name; } + /** + * Get the permissions for the current user + * @returns {Array | undefined} The permissions for the current user + */ getPermissions() { return this.#currentUser.getValue()?.permissions; } - getUnique() { + /** + * Get the unique for the current user + * @returns {string | undefined} The unique for the current user + */ + getUnique(): string | undefined { return this.#currentUser.getValue()?.unique; } - getUserName() { + /** + * Get the user name for the current user + * @returns {string | undefined} The user name for the current user + */ + getUserName(): string | undefined { return this.#currentUser.getValue()?.userName; } From e881d08645d9c74ddb86b2ab3479df6947c4a197 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 22 Oct 2024 10:07:28 +0200 Subject: [PATCH 3/4] disable readonly languages when unpublishing --- .../entity-actions/unpublish.action.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/packages/documents/documents/entity-actions/unpublish.action.ts b/src/packages/documents/documents/entity-actions/unpublish.action.ts index ff9d645a17..d0f47c9878 100644 --- a/src/packages/documents/documents/entity-actions/unpublish.action.ts +++ b/src/packages/documents/documents/entity-actions/unpublish.action.ts @@ -11,6 +11,7 @@ import { UmbVariantId } from '@umbraco-cms/backoffice/variant'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal'; import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action'; +import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user'; export class UmbUnpublishDocumentEntityAction extends UmbEntityActionBase { constructor(host: UmbControllerHost, args: UmbEntityActionArgs) { @@ -28,8 +29,16 @@ export class UmbUnpublishDocumentEntityAction extends UmbEntityActionBase if (!documentData) throw new Error('The document was not found'); - const context = await this.getContext(UMB_APP_LANGUAGE_CONTEXT); - const appCulture = context.getAppCulture(); + const appLanguageContext = await this.getContext(UMB_APP_LANGUAGE_CONTEXT); + const appCulture = appLanguageContext.getAppCulture(); + + const currentUserContext = await this.getContext(UMB_CURRENT_USER_CONTEXT); + const currentUserAllowedLanguages = currentUserContext.getLanguages(); + const currentUserHasAccessToAllLanguages = currentUserContext.getHasAccessToAllLanguages(); + + if (currentUserAllowedLanguages === undefined) throw new Error('The current user languages are missing'); + if (currentUserHasAccessToAllLanguages === undefined) + throw new Error('The current user access to all languages is missing'); const options: Array = documentData.variants.map( (variant) => ({ @@ -65,6 +74,11 @@ export class UmbUnpublishDocumentEntityAction extends UmbEntityActionBase data: { documentUnique: this.args.unique, options, + pickableFilter: (option) => { + if (!option.culture) return false; + if (currentUserHasAccessToAllLanguages) return true; + return currentUserAllowedLanguages.includes(option.culture); + }, }, value: { selection }, }) @@ -89,4 +103,5 @@ export class UmbUnpublishDocumentEntityAction extends UmbEntityActionBase } } } + export default UmbUnpublishDocumentEntityAction; From 22935de139699442873aeb8d210446ae360c4269 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 22 Oct 2024 10:10:21 +0200 Subject: [PATCH 4/4] disable readonly languages when publishing --- .../documents/entity-actions/publish.action.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/packages/documents/documents/entity-actions/publish.action.ts b/src/packages/documents/documents/entity-actions/publish.action.ts index 1175fd3361..845f47e73d 100644 --- a/src/packages/documents/documents/entity-actions/publish.action.ts +++ b/src/packages/documents/documents/entity-actions/publish.action.ts @@ -8,6 +8,7 @@ import { UmbVariantId } from '@umbraco-cms/backoffice/variant'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal'; import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action'; +import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user'; export class UmbPublishDocumentEntityAction extends UmbEntityActionBase { constructor(host: UmbControllerHost, args: UmbEntityActionArgs) { @@ -25,8 +26,16 @@ export class UmbPublishDocumentEntityAction extends UmbEntityActionBase { if (!documentData) throw new Error('The document was not found'); - const context = await this.getContext(UMB_APP_LANGUAGE_CONTEXT); - const appCulture = context.getAppCulture(); + const appLanguageContext = await this.getContext(UMB_APP_LANGUAGE_CONTEXT); + const appCulture = appLanguageContext.getAppCulture(); + + const currentUserContext = await this.getContext(UMB_CURRENT_USER_CONTEXT); + const currentUserAllowedLanguages = currentUserContext.getLanguages(); + const currentUserHasAccessToAllLanguages = currentUserContext.getHasAccessToAllLanguages(); + + if (currentUserAllowedLanguages === undefined) throw new Error('The current user languages are missing'); + if (currentUserHasAccessToAllLanguages === undefined) + throw new Error('The current user access to all languages is missing'); const options: Array = documentData.variants.map( (variant) => ({ @@ -76,6 +85,11 @@ export class UmbPublishDocumentEntityAction extends UmbEntityActionBase { .open(this, UMB_DOCUMENT_PUBLISH_MODAL, { data: { options, + pickableFilter: (option) => { + if (!option.culture) return false; + if (currentUserHasAccessToAllLanguages) return true; + return currentUserAllowedLanguages.includes(option.culture); + }, }, value: { selection }, })