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.ContextRequest
buildContextMatchingPermissionTenant
(P permission) Builds aContextRequest
that has aContextRequest.tenantId
matching theAdminPermission.tenantId
.protected boolean
UsespolicyUtils
to check if currently authenticated user has tenant access.protected AdminPermissionService<P>
protected String
protected org.springframework.validation.Errors
protected com.broadleafcommerce.data.tracking.core.policy.PolicyUtils
protected com.broadleafcommerce.common.extension.TypeFactory
boolean
isMutableFromContext
(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Returns whether or not the given entity is mutable from the given context.protected String
prefixWithEntityValidationMessageKey
(String errorCode) void
setAdminPermissionService
(AdminPermissionService<P> adminPermissionService) Lazy injection since this validator is itself a service component.org.springframework.validation.Errors
validateCreate
(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a create operation is allowed in the current context.void
validateCreate
(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 void
Since permissions are a tenant-level concept, only tenant access users can perform creation/modification operations on them.org.springframework.validation.Errors
validateDelete
(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a delete operation is allowed in the current context.void
validateDelete
(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 void
validateNameUniqueInPermissionContext
(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 void
validatePermissionIsMutableFromCurrentContext
(P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates that the permission can be mutated by the current context.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).org.springframework.validation.Errors
validateUpdate
(String id, P permission, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates an update operation is allowed in the current context.void
validateUpdate
(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:AdminPermissionContextValidator
Validates a create operation is allowed in the current context.- Specified by:
validateCreate
in interfaceAdminPermissionContextValidator<P extends AdminPermission>
- Parameters:
permission
- The permission to validatecontextInfo
- The current context- Returns:
- An
Errors
object 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:AdminPermissionContextValidator
Validates an update operation is allowed in the current context.- Specified by:
validateUpdate
in 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.id
is unsupplied inpermission
.permission
- The permission to validatecontextInfo
- The current context- Returns:
- An
Errors
object 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:AdminPermissionContextValidator
Validates a delete operation is allowed in the current context.- Specified by:
validateDelete
in interfaceAdminPermissionContextValidator<P extends AdminPermission>
- Parameters:
permission
- the permission being deleted to validatecontextInfo
- The current context- Returns:
- An
Errors
object 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:AdminPermissionContextValidator
Validates a create operation is allowed in the current context.- Specified by:
validateCreate
in interfaceAdminPermissionContextValidator<P extends AdminPermission>
- Parameters:
permission
- The permission to validateerrors
- An errors object bound to thepermission
to 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()UsespolicyUtils
to check if currently authenticated user has tenant access.If
policyUtils
is 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 givenpermission
on which errors can be registered
-
buildContextMatchingPermissionTenant
protected com.broadleafcommerce.data.tracking.core.context.ContextRequest buildContextMatchingPermissionTenant(P permission) Builds aContextRequest
that has aContextRequest.tenantId
matching 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
ContextRequest
matching 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:AdminPermissionContextValidator
Validates an update operation is allowed in the current context.- Specified by:
validateUpdate
in 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.id
is unsupplied inpermission
.permission
- The permission to validateerrors
- An errors object bound to thepermission
to 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 givenpermission
on 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:AdminPermissionContextValidator
Validates a delete operation is allowed in the current context.- Specified by:
validateDelete
in interfaceAdminPermissionContextValidator<P extends AdminPermission>
- Parameters:
permission
- The permission being deleted to validateerrors
- An errors object bound to thepermission
to be validatedcontextInfo
- The current context
-
isMutableFromContext
public boolean isMutableFromContext(P permission, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminPermissionContextValidator
Returns whether or not the given entity is mutable from the given context.- Specified by:
isMutableFromContext
in 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()
-