From f17795c6860a67e13c34ab1154050334ef7c45fd Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:27:23 +0200 Subject: [PATCH] map up isAdmin to save a lookup on the user condition --- .../current-user.server.data-source.ts | 1 + .../repository/current-user.store.ts | 1 + .../src/packages/user/current-user/types.ts | 1 + .../user-allow-action-base.condition.ts | 40 ++++++++----------- .../user-can-perform-actions.condition.ts | 2 +- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts index 69a1bb8b58..79be8d757d 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts @@ -43,6 +43,7 @@ export class UmbCurrentUserServerDataSource { fallbackPermissions: data.fallbackPermissions, permissions: data.permissions, allowedSections: data.allowedSections, + isAdmin: data.isAdmin, }; return { data: user }; } diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.store.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.store.ts index 9d30056316..1e6a9ffa1f 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.store.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.store.ts @@ -72,6 +72,7 @@ export class UmbCurrentUserStore extends UmbContextBase { documentStartNodeUniques: updatedCurrentUser.documentStartNodeUniques, mediaStartNodeUniques: updatedCurrentUser.mediaStartNodeUniques, avatarUrls: updatedCurrentUser.avatarUrls, + isAdmin: updatedCurrentUser.isAdmin, }; this.update(mappedCurrentUser); diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/types.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/types.ts index f6c67bf129..e5e6632324 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/types.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/types.ts @@ -18,6 +18,7 @@ export interface UmbCurrentUserModel { allowedSections: Array; fallbackPermissions: Array; permissions: Array; + isAdmin: boolean; } export type UmbCurrentUserMfaProviderModel = UserTwoFactorProviderModel; diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-allow-action-base.condition.ts b/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-allow-action-base.condition.ts index f8f2bcc7c5..6bcdd483f1 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-allow-action-base.condition.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-allow-action-base.condition.ts @@ -1,4 +1,3 @@ -import { isUserAdmin } from '../utils/index.js'; import type { UmbUserStateEnum } from '../types.js'; import { UMB_USER_WORKSPACE_CONTEXT } from '../workspace/user-workspace.context-token.js'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; @@ -14,7 +13,19 @@ export abstract class UmbUserActionConditionBase extends UmbConditionBase implements UmbExtensionCondition { + /** + * The unique identifier of the user being edited + */ protected userUnique?: string; + + /** + * Whether the user being edited is an admin + */ + protected userAdmin?: boolean; + + /** + * The state of the user being edited + */ protected userState?: UmbUserStateEnum | null; constructor(host: UmbControllerHost, args: UmbConditionControllerArguments) { @@ -22,24 +33,15 @@ export abstract class UmbUserActionConditionBase this.consumeContext(UMB_USER_WORKSPACE_CONTEXT, (context) => { this.observe( - context.unique, - (unique) => { - this.userUnique = unique; + context.data, + (user) => { + this.userUnique = user?.unique; + this.userAdmin = user?.isAdmin; + this.userState = user?.state; this._onUserDataChange(); }, 'umbUserUnique', ); - this.observe( - context.state, - (state) => { - this.userState = state; - // TODO: Investigate if we can remove this observation and just use the unique change to trigger the state change. [NL] - // Can user state change over time? if not then this observation is not needed and then we just need to retrieve the state when the unique has changed. [NL] - // These two could also be combined via the observeMultiple method, that could prevent triggering onUserDataChanged twice. [NL] - this._onUserDataChange(); - }, - 'umbUserState', - ); }); } @@ -61,14 +63,6 @@ export abstract class UmbUserActionConditionBase return isCurrentUserAnAdmin(this._host); } - /** - * Check if the selected user is an admin - * @protected - */ - protected async isUserAdmin() { - return this.userUnique ? isUserAdmin(this._host, this.userUnique) : false; - } - /** * Called when the user data changes * @protected diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-can-perform-actions.condition.ts b/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-can-perform-actions.condition.ts index 9a885aa7aa..988783c49b 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-can-perform-actions.condition.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/user/conditions/user-can-perform-actions.condition.ts @@ -26,7 +26,7 @@ export class UmbUserCanPerformActionsCondition extends UmbUserActionConditionBas } // Otherwise, the current user can only perform actions on other users - if (await this.isUserAdmin()) { + if (this.userAdmin) { this.permitted = false; return; }