map up isAdmin to save a lookup on the user condition

This commit is contained in:
Jacob Overgaard
2024-04-03 14:27:23 +02:00
parent bd9f493968
commit f17795c686
5 changed files with 21 additions and 24 deletions

View File

@@ -43,6 +43,7 @@ export class UmbCurrentUserServerDataSource {
fallbackPermissions: data.fallbackPermissions,
permissions: data.permissions,
allowedSections: data.allowedSections,
isAdmin: data.isAdmin,
};
return { data: user };
}

View File

@@ -72,6 +72,7 @@ export class UmbCurrentUserStore extends UmbContextBase<UmbCurrentUserStore> {
documentStartNodeUniques: updatedCurrentUser.documentStartNodeUniques,
mediaStartNodeUniques: updatedCurrentUser.mediaStartNodeUniques,
avatarUrls: updatedCurrentUser.avatarUrls,
isAdmin: updatedCurrentUser.isAdmin,
};
this.update(mappedCurrentUser);

View File

@@ -18,6 +18,7 @@ export interface UmbCurrentUserModel {
allowedSections: Array<string>;
fallbackPermissions: Array<string>;
permissions: Array<DocumentPermissionPresentationModel | UnknownTypePermissionPresentationModel>;
isAdmin: boolean;
}
export type UmbCurrentUserMfaProviderModel = UserTwoFactorProviderModel;

View File

@@ -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<UmbConditionConfigBase>
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<UmbConditionConfigBase>) {
@@ -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

View File

@@ -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;
}