From 820eda01923aec5ca6d5b746315d359ed5b3224e Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 20 Aug 2024 20:43:07 +0200 Subject: [PATCH] set dictionary translation to readonly when the user does not have access --- ...orkspace-view-dictionary-editor.element.ts | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/dictionary/workspace/views/workspace-view-dictionary-editor.element.ts b/src/Umbraco.Web.UI.Client/src/packages/dictionary/workspace/views/workspace-view-dictionary-editor.element.ts index 2378ca1073..ddabdf4cc2 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/dictionary/workspace/views/workspace-view-dictionary-editor.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/dictionary/workspace/views/workspace-view-dictionary-editor.element.ts @@ -5,26 +5,50 @@ import { UUITextareaEvent } from '@umbraco-cms/backoffice/external/uui'; import { css, html, customElement, state, repeat, ifDefined, unsafeHTML } from '@umbraco-cms/backoffice/external/lit'; import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; import { UmbLanguageCollectionRepository, type UmbLanguageDetailModel } from '@umbraco-cms/backoffice/language'; +import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user'; @customElement('umb-workspace-view-dictionary-editor') export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement { @state() private _dictionary?: UmbDictionaryDetailModel; - #languageCollectionRepository = new UmbLanguageCollectionRepository(this); - @state() private _languages: Array = []; - #workspaceContext!: typeof UMB_DICTIONARY_WORKSPACE_CONTEXT.TYPE; + @state() + private _currentUserLanguageAccess?: Array = []; - override async connectedCallback() { - super.connectedCallback(); + @state() + private _currentUserHasAccessToAllLanguages?: boolean = false; + + #languageCollectionRepository = new UmbLanguageCollectionRepository(this); + #workspaceContext!: typeof UMB_DICTIONARY_WORKSPACE_CONTEXT.TYPE; + #currentUserContext?: typeof UMB_CURRENT_USER_CONTEXT.TYPE; + + constructor() { + super(); this.consumeContext(UMB_DICTIONARY_WORKSPACE_CONTEXT, (_instance) => { this.#workspaceContext = _instance; this.#observeDictionary(); }); + + this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => { + this.#currentUserContext = context; + this.#observeCurrentUserLanguageAccess(); + }); + } + + #observeCurrentUserLanguageAccess() { + if (!this.#currentUserContext) return; + + this.observe(this.#currentUserContext.languages, (languages) => { + this._currentUserLanguageAccess = languages; + }); + + this.observe(this.#currentUserContext.hasAccessToAllLanguages, (hasAccess) => { + this._currentUserHasAccessToAllLanguages = hasAccess; + }); } override async firstUpdated() { @@ -40,19 +64,11 @@ export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement { }); } - #renderTranslation(language: UmbLanguageDetailModel) { - if (!language.unique) return; - - const translation = this._dictionary?.translations?.find((x) => x.isoCode === language.unique); - - return html` - - `; + #isReadOnly(culture: string | null) { + if (!this.#currentUserContext) return true; + if (!culture) return false; + if (this._currentUserHasAccessToAllLanguages) return false; + return !this._currentUserLanguageAccess?.includes(culture); } #onTextareaChange(e: Event) { @@ -78,6 +94,22 @@ export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement { `; } + #renderTranslation(language: UmbLanguageDetailModel) { + if (!language.unique) return; + + const translation = this._dictionary?.translations?.find((x) => x.isoCode === language.unique); + + return html` + + `; + } + static override styles = [ css` :host {