Class DefaultAdminPermissionContextValidator<P extends AdminPermission>
java.lang.Object
com.broadleafcommerce.adminuser.user.service.DefaultAdminPermissionContextValidator<P>
- All Implemented Interfaces:
AdminPermissionContextValidator<P>
public class DefaultAdminPermissionContextValidator<P extends AdminPermission>
extends Object
implements AdminPermissionContextValidator<P>
Default validator for use on
AdminPermission creation/modification/deletion. Verifies the
following:
- the current authentication is not application restricted (and can therefore modify these tenant-level entities)
- the current context is able to create/modify the permission
- the tenant ID of a permission can never be changed
- the name of a permission is non-empty and unique within its context
- Author:
- Samarth Dhruva (samarthd)
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultAdminPermissionContextValidator(com.broadleafcommerce.common.extension.TypeFactory typeFactory, com.broadleafcommerce.data.tracking.core.policy.PolicyUtils policyUtils) -
Method Summary
Modifier and TypeMethodDescriptionprotected com.broadleafcommerce.data.tracking.core.context.ContextRequestbuildContextMatchingPermissionTenant(P permission) Builds aContextRequestthat has aContextRequest.tenantIdmatching theAdminPermission.tenantId.protected booleanUsespolicyUtilsto check if currently authenticated user has tenant access.protected AdminPermissionService<P>protected Stringprotected org.springframework.validation.Errorsprotected com.broadleafcommerce.data.tracking.core.policy.PolicyUtilsprotected com.broadleafcommerce.common.extension.TypeFactorybooleanisMutableFromContext(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Returns whether or not the given entity is mutable from the given context.protected StringprefixWithEntityValidationMessageKey(String errorCode) voidsetAdminPermissionService(AdminPermissionService<P> adminPermissionService) Lazy injection since this validator is itself a service component.org.springframework.validation.ErrorsvalidateCreate(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a create operation is allowed in the current context.voidvalidateCreate(P permission, org.springframework.validation.Errors errors, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a create operation is allowed in the current context.protected voidSince permissions are a tenant-level concept, only tenant access users can perform creation/modification operations on them.org.springframework.validation.ErrorsvalidateDelete(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a delete operation is allowed in the current context.voidvalidateDelete(P permission, org.springframework.validation.Errors errors, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a delete operation is allowed in the current context.protected voidvalidateNameUniqueInPermissionContext(String id, P permission, org.springframework.validation.Errors errors) While it is perfectly valid for two tenant-level permissions in different tenants to have the same name, we want to avoid having duplicate names from the perspective of a particular tenant context (to prevent confusion).protected voidvalidatePermissionIsMutableFromCurrentContext(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates that the permission can be mutated by the current context.protected voidvalidateTenantIdUnchanged(P existingPermission, P permission, org.springframework.validation.Errors errors) Regardless of what context this operation is being performed in, tenant ID can never be changed (as it would compromise accessibility to/from related entities).org.springframework.validation.ErrorsvalidateUpdate(String id, P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates an update operation is allowed in the current context.voidvalidateUpdate(String id, P permission, org.springframework.validation.Errors errors, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates an update operation is allowed in the current context.
-
Constructor Details
-
DefaultAdminPermissionContextValidator
public DefaultAdminPermissionContextValidator(com.broadleafcommerce.common.extension.TypeFactory typeFactory, @Nullable com.broadleafcommerce.data.tracking.core.policy.PolicyUtils policyUtils)
-
-
Method Details
-
setAdminPermissionService
@Autowired @Lazy public void setAdminPermissionService(AdminPermissionService<P> adminPermissionService) Lazy injection since this validator is itself a service component. This avoids circular dependency exceptions- Parameters:
adminPermissionService- the permission service
-
validateCreate
public org.springframework.validation.Errors validateCreate(P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates a create operation is allowed in the current context.- Specified by:
validateCreatein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
permission- The permission to validatecontextInfo- The current context- Returns:
- An
Errorsobject bound to a {code permission} - See Also:
-
getErrors
-
validateUpdate
public org.springframework.validation.Errors validateUpdate(String id, P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates an update operation is allowed in the current context.- Specified by:
validateUpdatein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
id- the id of the permission being updated, explicitly provided such that it can be used to find the existing record even ifAdminPermission.idis unsupplied inpermission.permission- The permission to validatecontextInfo- The current context- Returns:
- An
Errorsobject bound to a {code permission} - See Also:
-
validateDelete
public org.springframework.validation.Errors validateDelete(P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates a delete operation is allowed in the current context.- Specified by:
validateDeletein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
permission- the permission being deleted to validatecontextInfo- The current context- Returns:
- An
Errorsobject bound to a {code permission} - See Also:
-
validateCreate
public void validateCreate(P permission, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates a create operation is allowed in the current context.- Specified by:
validateCreatein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
permission- The permission to validateerrors- An errors object bound to thepermissionto be validatedcontextInfo- The current context
-
validateCurrentAuthenticationHasTenantAccess
protected void validateCurrentAuthenticationHasTenantAccess()Since permissions are a tenant-level concept, only tenant access users can perform creation/modification operations on them.- Throws:
com.broadleafcommerce.data.tracking.core.exception.NotPermittedException- if the current authentication does not have tenant access
-
currentUserHasTenantAccess
protected boolean currentUserHasTenantAccess()UsespolicyUtilsto check if currently authenticated user has tenant access.If
policyUtilsis null or it's not an instance ofTrackablePolicyUtils, that means policy validation is turned off or trackable considerations should be ignored. Therefore it will automatically report the currently authenticated user has tenant access, since we want to allow everything in those cases.- Returns:
- true if currently authenticated user has tenant access, otherwise false
- See Also:
-
TrackablePolicyUtils.isUserTenantLevelAccess()
-
getAuthenticationDetails
-
prefixWithEntityValidationMessageKey
-
getCurrentlyAuthenticatedUser
-
validateNameUniqueInPermissionContext
protected void validateNameUniqueInPermissionContext(@Nullable String id, P permission, org.springframework.validation.Errors errors) While it is perfectly valid for two tenant-level permissions in different tenants to have the same name, we want to avoid having duplicate names from the perspective of a particular tenant context (to prevent confusion).This means that:
- a tenant-level permission's name must be unique among permissions with the same tenant id and global permissions (which would be accessible from any tenant-context)
- a global permission's name must be unique among all permissions in all contexts
- Parameters:
id- the id of thepermission, explicitly provided as it may be null on thepermission. May be null as in the case of a create.permission- the permission whose name should be validated for uniqueness in its contexterrors- the errors object bound to the givenpermissionon which errors can be registered
-
buildContextMatchingPermissionTenant
protected com.broadleafcommerce.data.tracking.core.context.ContextRequest buildContextMatchingPermissionTenant(P permission) Builds aContextRequestthat has aContextRequest.tenantIdmatching theAdminPermission.tenantId. Useful in situations where it is necessary to make validations from the perspective of the entity's context rather than the context of the current request itself, which may have different accessibility.- Parameters:
permission- the permission for which to build a context request- Returns:
- a
ContextRequestmatching the given permission's tenant ID
-
validateUpdate
public void validateUpdate(String id, P permission, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates an update operation is allowed in the current context.- Specified by:
validateUpdatein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
id- the id of the permission being updated, explicitly provided such that it can be used to find the existing record even ifAdminPermission.idis unsupplied inpermission.permission- The permission to validateerrors- An errors object bound to thepermissionto be validatedcontextInfo- The current context
-
validateTenantIdUnchanged
protected void validateTenantIdUnchanged(P existingPermission, P permission, org.springframework.validation.Errors errors) Regardless of what context this operation is being performed in, tenant ID can never be changed (as it would compromise accessibility to/from related entities).- Parameters:
existingPermission- the permission as it exists in the data store currentlypermission- the permission after updateerrors- the errors object bound to the givenpermissionon which errors can be registered
-
validatePermissionIsMutableFromCurrentContext
protected void validatePermissionIsMutableFromCurrentContext(P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates that the permission can be mutated by the current context.This is important, because in some cases permissions are accessible by a context but not mutable from that context. For example, a global permission can be accessed by a tenant context but cannot be mutated by that tenant context.
- Parameters:
permission- the permission to validate mutability forcontextInfo- context information surrounding multitenant state- Throws:
com.broadleafcommerce.data.tracking.core.exception.InvalidContextRequestException- if the permission is not mutable from the current context
-
validateDelete
public void validateDelete(P permission, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorValidates a delete operation is allowed in the current context.- Specified by:
validateDeletein interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
permission- The permission being deleted to validateerrors- An errors object bound to thepermissionto be validatedcontextInfo- The current context
-
isMutableFromContext
public boolean isMutableFromContext(P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidatorReturns whether or not the given entity is mutable from the given context.- Specified by:
isMutableFromContextin interfaceAdminPermissionContextValidator<P extends AdminPermission>- Parameters:
permission- the entity whose mutability should be checkedcontextInfo- the context in which mutability should be checked- Returns:
- true if the entity is mutable from the given context, false otherwise
-
getTypeFactory
protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory() -
getAdminPermissionService
-
getPolicyUtils
@Nullable protected com.broadleafcommerce.data.tracking.core.policy.PolicyUtils getPolicyUtils()
-