MIFOSX-97: move validation login to where the data exists - the command.

This commit is contained in:
Keith Woodlock
2012-12-01 14:44:14 +00:00
parent 9f65ff3cbd
commit df283eb533
6 changed files with 92 additions and 130 deletions
@@ -1,7 +1,13 @@
package org.mifosplatform.infrastructure.user.command;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
/**
* Immutable command for creating or updating details of a role.
*
@@ -52,4 +58,32 @@ public class RoleCommand {
public boolean isApprovedByChecker() {
return this.makerCheckerApproval;
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
baseDataValidator.reset().parameter("name").value(this.name).notBlank();
baseDataValidator.reset().parameter("description").value(this.description).notBlank().notExceedingLengthOf(500);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
public void validateForUpdate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
baseDataValidator.reset().parameter("id").value(this.id).notNull();
baseDataValidator.reset().parameter("name").value(this.name).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("description").value(this.description).ignoreIfNull().notBlank()
.notExceedingLengthOf(500);
baseDataValidator.reset().anyOfNotNull(this.name, this.description);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
}
@@ -1,46 +0,0 @@
package org.mifosplatform.infrastructure.user.command;
import java.util.ArrayList;
import java.util.List;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
public class RoleCommandValidator {
private final RoleCommand command;
public RoleCommandValidator(RoleCommand command) {
this.command = command;
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
baseDataValidator.reset().parameter("name").value(command.getName()).notBlank();
baseDataValidator.reset().parameter("description").value(command.getDescription()).notBlank().notExceedingLengthOf(500);
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
public void validateForUpdate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
baseDataValidator.reset().parameter("id").value(command.getId()).notNull();
baseDataValidator.reset().parameter("name").value(command.getName()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("description").value(command.getDescription()).ignoreIfNull().notBlank().notExceedingLengthOf(500);
baseDataValidator.reset().anyOfNotNull(command.getName(), command.getDescription());
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
}
@@ -1,7 +1,13 @@
package org.mifosplatform.infrastructure.user.command;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
/**
* Immutable command for creating or updating details of a User.
*/
@@ -21,11 +27,10 @@ public class UserCommand {
private transient final boolean makerCheckerApproval;
private transient final Long id;
private transient final Set<String> modifiedParameters;
public UserCommand(final Set<String> modifiedParameters, final boolean makerCheckerApproval,
final Long id, final String username, final String firstname,
final String lastname, final String password, final String repeatPassword, final String email, final Long officeId,
final String[] notSelectedRoles, final String[] roles) {
public UserCommand(final Set<String> modifiedParameters, final boolean makerCheckerApproval, final Long id, final String username,
final String firstname, final String lastname, final String password, final String repeatPassword, final String email,
final Long officeId, final String[] notSelectedRoles, final String[] roles) {
this.modifiedParameters = modifiedParameters;
this.makerCheckerApproval = makerCheckerApproval;
this.id = id;
@@ -107,8 +112,49 @@ public class UserCommand {
public boolean isRolesChanged() {
return this.modifiedParameters.contains("roles");
}
public boolean isApprovedByChecker() {
return this.makerCheckerApproval;
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("user");
baseDataValidator.reset().parameter("username").value(this.username).notBlank();
baseDataValidator.reset().parameter("firstname").value(this.firstname).notBlank();
baseDataValidator.reset().parameter("lastname").value(this.lastname).notBlank();
baseDataValidator.reset().parameter("email").value(this.email).notBlank();
baseDataValidator.reset().parameter("officeId").value(this.officeId).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("roles").value(this.roles).arrayNotEmpty();
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
public void validateForUpdate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("user");
baseDataValidator.reset().parameter("id").value(this.id).notNull();
baseDataValidator.reset().parameter("username").value(this.username).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("firstname").value(this.firstname).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("lastname").value(this.lastname).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("email").value(this.email).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("officeId").value(this.officeId).ignoreIfNull().notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("roles").value(this.roles).ignoreIfNull().arrayNotEmpty();
baseDataValidator.reset().parameter("password").value(this.password).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("passwordRepeat").value(this.repeatPassword).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("password").value(this.password).ignoreIfNull()
.equalToParameter("repeatPassword", this.repeatPassword);
baseDataValidator.reset().anyOfNotNull(this.username, this.firstname, this.lastname, this.email, this.officeId,
this.roles, this.password, this.repeatPassword);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
}
@@ -1,60 +0,0 @@
package org.mifosplatform.infrastructure.user.command;
import java.util.ArrayList;
import java.util.List;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
public class UserCommandValidator {
private final UserCommand command;
public UserCommandValidator(UserCommand command) {
this.command = command;
}
public void validateForUpdate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("user");
baseDataValidator.reset().parameter("id").value(command.getId()).notNull();
baseDataValidator.reset().parameter("username").value(command.getUsername()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("firstname").value(command.getFirstname()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("lastname").value(command.getLastname()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("email").value(command.getEmail()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).ignoreIfNull().notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("roles").value(command.getRoles()).ignoreIfNull().arrayNotEmpty();
baseDataValidator.reset().parameter("password").value(command.getPassword()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("passwordRepeat").value(command.getPassword()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("password").value(command.getPassword()).ignoreIfNull().equalToParameter("repeatPassword", command.getRepeatPassword());
baseDataValidator.reset().anyOfNotNull(command.getUsername(), command.getFirstname(), command.getLastname(), command.getEmail(), command.getOfficeId(),
command.getRoles(), command.getPassword(), command.getRepeatPassword());
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("user");
baseDataValidator.reset().parameter("username").value(command.getUsername()).notBlank();
baseDataValidator.reset().parameter("firstname").value(command.getFirstname()).notBlank();
baseDataValidator.reset().parameter("lastname").value(command.getLastname()).notBlank();
baseDataValidator.reset().parameter("email").value(command.getEmail()).notBlank();
baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("roles").value(command.getRoles()).arrayNotEmpty();
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
}
@@ -19,7 +19,6 @@ import org.mifosplatform.infrastructure.security.domain.PlatformUser;
import org.mifosplatform.infrastructure.security.service.PlatformPasswordEncoder;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.infrastructure.user.command.UserCommand;
import org.mifosplatform.infrastructure.user.command.UserCommandValidator;
import org.mifosplatform.infrastructure.user.domain.AppUser;
import org.mifosplatform.infrastructure.user.domain.AppUserRepository;
import org.mifosplatform.infrastructure.user.domain.Role;
@@ -68,9 +67,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
try {
context.authenticatedUser();
final UserCommandValidator validator = new UserCommandValidator(command);
validator.validateForCreate();
command.validateForCreate();
final Set<Role> allRoles = assembleSetOfRoles(command);
@@ -108,9 +105,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
try {
context.authenticatedUser();
UserCommandValidator validator = new UserCommandValidator(command);
validator.validateForUpdate();
command.validateForUpdate();
final Set<Role> allRoles = assembleSetOfRoles(command);
@@ -156,9 +151,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
try {
context.authenticatedUser();
UserCommandValidator validator = new UserCommandValidator(command);
validator.validateForUpdate();
command.validateForUpdate();
final Set<Role> allRoles = assembleSetOfRoles(command);
@@ -6,7 +6,6 @@ import java.util.Map;
import org.mifosplatform.infrastructure.configuration.service.ConfigurationDomainService;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.infrastructure.user.command.RoleCommand;
import org.mifosplatform.infrastructure.user.command.RoleCommandValidator;
import org.mifosplatform.infrastructure.user.command.RolePermissionCommand;
import org.mifosplatform.infrastructure.user.domain.Permission;
import org.mifosplatform.infrastructure.user.domain.PermissionRepository;
@@ -42,9 +41,7 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
public Long createRole(final RoleCommand command) {
context.authenticatedUser();
final RoleCommandValidator validator = new RoleCommandValidator(command);
validator.validateForCreate();
command.validateForCreate();
final Role entity = new Role(command.getName(), command.getDescription());
@@ -57,12 +54,10 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
@Transactional
@Override
public Long updateRole(RoleCommand command) {
public Long updateRole(final RoleCommand command) {
context.authenticatedUser();
final RoleCommandValidator validator = new RoleCommandValidator(command);
validator.validateForUpdate();
command.validateForUpdate();
final Role role = this.roleRepository.findOne(command.getId());
if (role == null) { throw new RoleNotFoundException(command.getId()); }