allow for both allOf and oneOf in the condition

This commit is contained in:
Mads Rasmussen
2024-04-10 10:51:44 +02:00
parent ee2f393030
commit efd514bc09
2 changed files with 29 additions and 5 deletions

View File

@@ -82,19 +82,40 @@ export class UmbDocumentUserPermissionCondition
}
#check(verbs: Array<string>) {
this.permitted = this.config.allOf.every((verb) => verbs.includes(verb));
/* we default to true se we don't require both allOf and oneOf to be defined
but they can be combined for more complex scenarios */
let allOfPermitted = true;
let oneOfPermitted = true;
if (this.config.allOf?.length) {
allOfPermitted = this.config.allOf.every((verb) => verbs.includes(verb));
}
if (this.config.oneOf?.length) {
oneOfPermitted = this.config.oneOf.some((verb) => verbs.includes(verb));
}
this.permitted = allOfPermitted && oneOfPermitted;
}
}
export type UmbDocumentUserPermissionConditionConfig =
UmbConditionConfigBase<'Umb.Condition.UserPermission.Document'> & {
/**
*
* The user must have all of the permissions in this array for the condition to be met.
*
* @example
* ["Umb.Document.Save", "Umb.Document.Publish"]
*/
allOf: Array<string>;
allOf?: Array<string>;
/**
* The user must have at least one of the permissions in this array for the condition to be met.
*
* @example
* ["Umb.Document.Save", "Umb.Document.Publish"]
*/
oneOf?: Array<string>;
};
export const manifest: ManifestCondition = {

View File

@@ -1,4 +1,7 @@
import { UMB_USER_PERMISSION_DOCUMENT_UPDATE } from '../../user-permissions/constants.js';
import {
UMB_USER_PERMISSION_DOCUMENT_CREATE,
UMB_USER_PERMISSION_DOCUMENT_UPDATE,
} from '../../user-permissions/constants.js';
import { UmbDocumentUserPermissionCondition } from '../../user-permissions/document-user-permission.condition.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbSubmitWorkspaceAction } from '@umbraco-cms/backoffice/workspace';
@@ -15,7 +18,7 @@ export class UmbDocumentSaveWorkspaceAction extends UmbSubmitWorkspaceAction {
host,
config: {
alias: 'Umb.Condition.UserPermission.Document',
allOf: [UMB_USER_PERMISSION_DOCUMENT_UPDATE],
oneOf: [UMB_USER_PERMISSION_DOCUMENT_CREATE, UMB_USER_PERMISSION_DOCUMENT_UPDATE],
},
onChange: () => {
condition.permitted ? this.enable() : this.disable();