Class DefaultAdminRoleContextValidator<P extends AdminRole>
- All Implemented Interfaces:
AdminRoleContextValidator<P>
AdminRole
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 role
- the permissions specified for a role exist and are accessible from the role's context - a global role can assign only global permissions, while a tenant-level role can assign global permissions or permissions from the same tenant
- the tenant ID of a role can never be changed
- the name of a role is non-empty and unique within its context
- the direct parent of a role exists and is accessible from the role's context - a global role can only assign a global parent, while a tenant-level role can assign a global parent or parents from the same tenant
- Author:
- Samarth Dhruva (samarthd)
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultAdminRoleContextValidator
(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
Builds aContextRequest
that has aContextRequest.tenantId
matching theAdminRole.tenantId
.protected boolean
UsespolicyUtils
to check if currently authenticated user has tenant access.protected AdminPermissionService<AdminPermission>
protected AdminRoleService<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 role, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Returns whether or not the given entity is mutable from the given context.protected boolean
isParentRoleAccessible
(P role) Given a role that has a non-emptyAdminRole.parentRoleId
, validates that the parent exists and is accessible from the child's context.protected String
prefixWithEntityValidationMessageKey
(String errorCode) void
setAdminPermissionService
(AdminPermissionService<AdminPermission> adminPermissionService) Lazy injection since this validator is itself a service component.void
setAdminRoleService
(AdminRoleService<P> adminRoleService) Lazy injection since this validator is itself a service component.org.springframework.validation.Errors
validateCreate
(P role, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a create operation is allowed in the current context.void
validateCreate
(P role, 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 roles are a tenant-level concept, only tenant access users can perform creation/modification operations on them.org.springframework.validation.Errors
validateDelete
(P role, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates a delete operation is allowed in the current context.void
validateDelete
(P role, 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
validateDirectlyAssignedPermissions
(P role, org.springframework.validation.Errors errors) protected void
validateNameUniqueInRoleContext
(String id, P role, org.springframework.validation.Errors errors) While it is perfectly valid for two tenant-level roles 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
validateParentRole
(P role, org.springframework.validation.Errors errors) If theAdminRole.parentRoleId
is supplied, verifies that it exists and is accessible from the child's context.protected void
validatePermissions
(P role, String permissionsFieldNameOnParent, Set<AdminPermissionRef> permissions, org.springframework.validation.Errors errors) protected void
validatePermissionsAssignableFromRoleContext
(P role, String permissionsFieldNameOnParent, Set<AdminPermissionRef> permissions, org.springframework.validation.Errors errors) Validates that all of the role's assigned permission IDs belong to permissions that are accessible from the role's context.protected void
validateRoleIsMutableFromCurrentContext
(P role, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates that the role can be mutated by the current context.protected void
validateTenantIdUnchanged
(P existingRole, P role, 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 role, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates an update operation is allowed in the current context.void
validateUpdate
(String id, P role, 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
-
DefaultAdminRoleContextValidator
public DefaultAdminRoleContextValidator(com.broadleafcommerce.common.extension.TypeFactory typeFactory, @Nullable com.broadleafcommerce.data.tracking.core.policy.PolicyUtils policyUtils)
-
-
Method Details
-
setAdminRoleService
Lazy injection since this validator is itself a service component. This avoids circular dependency exceptions- Parameters:
adminRoleService
- the role service
-
setAdminPermissionService
@Autowired @Lazy public void setAdminPermissionService(AdminPermissionService<AdminPermission> 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 role, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates a create operation is allowed in the current context.- Specified by:
validateCreate
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
role
- The role to validatecontextInfo
- The current context- Returns:
- An
Errors
object bound to a {code role} - See Also:
-
getErrors
-
validateUpdate
public org.springframework.validation.Errors validateUpdate(String id, P role, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates an update operation is allowed in the current context.- Specified by:
validateUpdate
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
id
- the id of the role being updated, explicitly provided such that it can be used to find the existing record even ifAdminRole.id
is unsupplied inrole
.role
- The role to validatecontextInfo
- The current context- Returns:
- An
Errors
object bound to a {code role} - See Also:
-
validateDelete
public org.springframework.validation.Errors validateDelete(P role, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates a delete operation is allowed in the current context.- Specified by:
validateDelete
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
role
- the role being deleted to validatecontextInfo
- The current context- Returns:
- An
Errors
object bound to a {code role} - See Also:
-
validateCreate
public void validateCreate(P role, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates a create operation is allowed in the current context.- Specified by:
validateCreate
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
role
- The role to validateerrors
- An errors object bound to therole
to be validatedcontextInfo
- The current context
-
validateCurrentAuthenticationHasTenantAccess
protected void validateCurrentAuthenticationHasTenantAccess()Since roles 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
-
validateNameUniqueInRoleContext
protected void validateNameUniqueInRoleContext(@Nullable String id, P role, org.springframework.validation.Errors errors) While it is perfectly valid for two tenant-level roles 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 role's name must be unique among roles with the same tenant id and global roles (which would be accessible from any tenant-context)
- a global role's name must be unique among all roles in all contexts
- Parameters:
id
- the id of therole
, explicitly provided as it may be null on therole
. May be null as in the case of a create.role
- the role whose name should be validated for uniqueness in its contexterrors
- the errors object bound to the givenrole
on which errors can be registered
-
validateDirectlyAssignedPermissions
protected void validateDirectlyAssignedPermissions(P role, org.springframework.validation.Errors errors) -
validatePermissions
protected void validatePermissions(P role, String permissionsFieldNameOnParent, Set<AdminPermissionRef> permissions, org.springframework.validation.Errors errors) -
validatePermissionsAssignableFromRoleContext
protected void validatePermissionsAssignableFromRoleContext(P role, String permissionsFieldNameOnParent, Set<AdminPermissionRef> permissions, org.springframework.validation.Errors errors) Validates that all of the role's assigned permission IDs belong to permissions that are accessible from the role's context.This is important, because the create/update/delete operation itself may be occurring in a context different from that of the role itself and thus its context cannot be used. For example, if a role is being created in a specific tenant from a global context, the permissions accessible from the global context (ex: permissions from other tenants) do not reflect the permissions accessible by the role itself.
A key element to note is that while a tenant-level role can be assigned either global permissions or permissions from that tenant, a global role can only be assigned global permissions. This is to avoid a situation where a tenant-user is assigned a global role and ends up getting access to permissions from other tenants.
- Parameters:
role
- the role whose permissions need to be checked for context accessibility. TheAdminRole.getPermissions()
should be pre-validated to not contain any null elements or empty IDs.errors
- the errors object bound to the givenrole
on which errors can be registered
-
buildContextMatchingRoleTenant
protected com.broadleafcommerce.data.tracking.core.context.ContextRequest buildContextMatchingRoleTenant(P role) Builds aContextRequest
that has aContextRequest.tenantId
matching theAdminRole.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:
role
- the role for which to build a context request- Returns:
- a
ContextRequest
matching the given role's tenant ID
-
validateParentRole
If theAdminRole.parentRoleId
is supplied, verifies that it exists and is accessible from the child's context.Validations on the full ancestry (ex: cycle validation) are done by
AdminRoleValidator
.- Parameters:
role
- the role whoseAdminRole.parentRoleId
needs to be validatederrors
- the errors object bound to the givenrole
on which errors can be registered
-
isParentRoleAccessible
Given a role that has a non-emptyAdminRole.parentRoleId
, validates that the parent exists and is accessible from the child's context.- Parameters:
role
- the role whoseAdminRole.parentRoleId
needs to be validated- Returns:
true
if the parent role exists and is accessible by the child,false
otherwise
-
validateUpdate
public void validateUpdate(String id, P role, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates an update operation is allowed in the current context.- Specified by:
validateUpdate
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
id
- the id of the role being updated, explicitly provided such that it can be used to find the existing record even ifAdminRole.id
is unsupplied inrole
.role
- The role to validateerrors
- An errors object bound to therole
to be validatedcontextInfo
- The current context
-
validateTenantIdUnchanged
protected void validateTenantIdUnchanged(P existingRole, P role, 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:
existingRole
- the role as it exists in the data store currentlyrole
- the role after updateerrors
- the errors object bound to the givenrole
on which errors can be registered
-
validateRoleIsMutableFromCurrentContext
protected void validateRoleIsMutableFromCurrentContext(P role, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Validates that the role can be mutated by the current context.This is important, because in some cases roles are accessible by a context but not mutable from that context. For example, a global role can be accessed by a tenant context but cannot be mutated by that tenant context.
- Parameters:
role
- the role to validate mutability forcontextInfo
- context information surrounding multitenant state- Throws:
com.broadleafcommerce.data.tracking.core.exception.InvalidContextRequestException
- if the role is not mutable from the current context
-
validateDelete
public void validateDelete(P role, org.springframework.validation.Errors errors, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Validates a delete operation is allowed in the current context.- Specified by:
validateDelete
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
role
- The role being deleted to validateerrors
- An errors object bound to therole
to be validatedcontextInfo
- The current context
-
isMutableFromContext
public boolean isMutableFromContext(P role, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:AdminRoleContextValidator
Returns whether or not the given entity is mutable from the given context.- Specified by:
isMutableFromContext
in interfaceAdminRoleContextValidator<P extends AdminRole>
- Parameters:
role
- 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() -
getPolicyUtils
@Nullable protected com.broadleafcommerce.data.tracking.core.policy.PolicyUtils getPolicyUtils() -
getAdminRoleService
-
getAdminPermissionService
-