return InvitationForm.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
InvitationForm invitationForm = (InvitationForm) target;
invitationForm.setEmail(trimWhitespace(invitationForm.getEmail()));
User user = userService.getUserFromSecurityContext();
if (user == null || user.getActiveOrganization() == null) {
errors.reject("invitationValidator.error.generic");
return;
}
// Check if email is valid
validateEmail(invitationForm.getEmail(), errors);
if (errors.hasErrors()) {
// No need to continue validating since the email is incorrect
return;
}
// Validate organization user limit
Set<String> emails = new HashSet<String>();
emails.add(invitationForm.getEmail());
boolean isValid = validateOrganizationUserLimit(emails, user.getActiveOrganization(), errors);
if (!isValid) {
return;
}
// Check if User Roles are valid
if ((invitationForm.getOrganizationUserRole() == null || UserRole.ROLE_ORG_GUEST.equals(invitationForm.getOrganizationUserRole())) && invitationForm.getGroupUserRole() == null) {
if (user.isActiveOrganizationAdmin()) {
errors.reject("invitationValidator.userRole.both.empty");
} else {
errors.rejectValue(GROUP_USER_ROLE_FIELD, "invitationValidator.userRole.group.empty");
}
}
User invitedUser = userService.getByEmail(invitationForm.getEmail());
validateInvitationForOrganization(invitationForm, user, invitedUser, errors);
validateInvitationForGroups(invitationForm, user, invitedUser, errors);
// Domain domain = domainService.get(invitationForm.getDomainId());