tidy up users resource supporting better updates. useraccounts resources has been removed along with change password template convenience api.
This commit is contained in:
@@ -41,6 +41,20 @@ public class DataValidatorBuilder {
|
||||
this.ignoreNullValue = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataValidatorBuilder equalToParameter(String linkedParameterName, Object linkedValue) {
|
||||
if (value == null && linkedValue == null && ignoreNullValue) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (value != null && !value.equals(linkedValue)) {
|
||||
StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(resource).append(".").append(linkedParameterName).append(".not.equal.to").append(parameter);
|
||||
StringBuilder defaultEnglishMessage = new StringBuilder("The parameter ").append(linkedParameterName).append(" is not equal to ").append(parameter).append(".");
|
||||
ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), linkedParameterName, linkedValue, value);
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataValidatorBuilder notNull() {
|
||||
if (value == null && !ignoreNullValue) {
|
||||
|
||||
+5
-2
@@ -12,6 +12,7 @@ import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.mifosng.data.AuthenticatedUserData;
|
||||
import org.mifosng.platform.user.domain.AppUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
@@ -33,8 +34,8 @@ public class AuthenticationApiResource {
|
||||
private DaoAuthenticationProvider customAuthenticationProvider;
|
||||
|
||||
@POST
|
||||
@Consumes({MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_XML})
|
||||
@Consumes({MediaType.APPLICATION_JSON})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response authenticate(@QueryParam("username") final String username, @QueryParam("password") final String password) {
|
||||
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
|
||||
@@ -51,6 +52,8 @@ public class AuthenticationApiResource {
|
||||
}
|
||||
authenticatedUserData.setPermissions(permissions);
|
||||
authenticatedUserData.setAuthenticated(true);
|
||||
AppUser principal = (AppUser) authenticationCheck.getPrincipal();
|
||||
authenticatedUserData.setUserId(principal.getId());
|
||||
byte[] base64EncodedAuthenticationKey = Base64.encode(username + ":" + password);
|
||||
authenticatedUserData.setBase64EncodedAuthenticationKey(new String(base64EncodedAuthenticationKey));
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
package org.mifosng.platform.api;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.mifosng.data.AppUserData;
|
||||
import org.mifosng.data.EntityIdentifier;
|
||||
import org.mifosng.data.command.ChangePasswordCommand;
|
||||
import org.mifosng.data.command.UserCommand;
|
||||
import org.mifosng.platform.user.service.AppUserReadPlatformService;
|
||||
import org.mifosng.platform.user.service.AppUserWritePlatformService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Path("/v1/useraccounts")
|
||||
@Component
|
||||
@Scope("singleton")
|
||||
public class UserAccountApiResource {
|
||||
|
||||
@Autowired
|
||||
private AppUserReadPlatformService appUserReadPlatformService;
|
||||
|
||||
@Autowired
|
||||
private AppUserWritePlatformService appUserWritePlatformService;
|
||||
|
||||
@GET
|
||||
@Path("current")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
public Response retrieveCurrentUser() {
|
||||
|
||||
AppUserData user = this.appUserReadPlatformService.retrieveCurrentUser();
|
||||
|
||||
return Response.ok().entity(user).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("current")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
public Response updateCurrentUser(UserCommand command) {
|
||||
|
||||
Long userId = this.appUserWritePlatformService.updateCurrentUser(command);
|
||||
|
||||
return Response.ok().entity(new EntityIdentifier(userId)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("current/password/template")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
public Response retrieveCurrentPasswordTemplate() {
|
||||
|
||||
return Response.ok().entity(new ChangePasswordCommand()).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("current/password")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
public Response updateCurrentUserPassword(ChangePasswordCommand command) {
|
||||
|
||||
Long userId = this.appUserWritePlatformService.updateCurrentUserPassword(command);
|
||||
|
||||
return Response.ok().entity(new EntityIdentifier(userId)).build();
|
||||
}
|
||||
}
|
||||
@@ -270,22 +270,29 @@ public class AppUser extends AbstractAuditableCustom<AppUser, Long> implements P
|
||||
return lastname;
|
||||
}
|
||||
|
||||
public void update(Set<Role> allRoles, Office office, String username,
|
||||
String firstname, String lastname, String email) {
|
||||
this.roles.clear();
|
||||
this.roles = allRoles;
|
||||
this.office = office;
|
||||
this.username = username;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void update(String username, String firstname, String lastname, String email) {
|
||||
this.username = username;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.email = email;
|
||||
/**
|
||||
* When updating details, any parameters with null values are ignored.
|
||||
*/
|
||||
public void update(Set<Role> allRoles, Office office, String username, String firstname, String lastname, String email) {
|
||||
if (!allRoles.isEmpty()) {
|
||||
this.roles.clear();
|
||||
this.roles = allRoles;
|
||||
}
|
||||
if (office != null) {
|
||||
this.office = office;
|
||||
}
|
||||
if (username != null) {
|
||||
this.username = username;
|
||||
}
|
||||
if (firstname != null) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
if (lastname != null) {
|
||||
this.lastname = lastname;
|
||||
}
|
||||
if (email != null) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canNotApproveLoanInPast() {
|
||||
|
||||
+2
-3
@@ -71,11 +71,10 @@ public class JpaUserDomainService implements UserDomainService {
|
||||
generateKeyUsedForPasswordSalting(appUser);
|
||||
|
||||
String unencodedPassword = appUser.getPassword();
|
||||
if (org.apache.commons.lang.StringUtils.isBlank(unencodedPassword)) {
|
||||
if (org.apache.commons.lang.StringUtils.isBlank(unencodedPassword) || "autogenerate".equalsIgnoreCase(unencodedPassword)) {
|
||||
unencodedPassword = new RandomPasswordGenerator(13).generate();
|
||||
appUser.updatePassword(unencodedPassword);
|
||||
}
|
||||
// update so encoding works.
|
||||
appUser.updatePassword(unencodedPassword);
|
||||
|
||||
String encodePassword = this.applicationPasswordEncoder.encode(appUser);
|
||||
appUser.updatePassword(encodePassword);
|
||||
|
||||
@@ -82,7 +82,7 @@ public class Role extends AbstractAuditableCustom<AppUser, Long> {
|
||||
}
|
||||
|
||||
/**
|
||||
* When updating role details, any parameters with null values are ignored.
|
||||
* When updating details, any parameters with null values are ignored.
|
||||
*/
|
||||
public void update(String name, String description, List<Permission> selectedPermissions) {
|
||||
if (name != null) {
|
||||
|
||||
-2
@@ -11,6 +11,4 @@ public interface AppUserReadPlatformService {
|
||||
AppUserData retrieveNewUserDetails();
|
||||
|
||||
AppUserData retrieveUser(Long userId);
|
||||
|
||||
AppUserData retrieveCurrentUser();
|
||||
}
|
||||
+3
-34
@@ -13,6 +13,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.mifosng.data.AppUserData;
|
||||
import org.mifosng.data.OfficeData;
|
||||
import org.mifosng.data.OfficeLookup;
|
||||
import org.mifosng.data.RoleData;
|
||||
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
|
||||
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
|
||||
@@ -78,7 +79,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
@Override
|
||||
public AppUserData retrieveNewUserDetails() {
|
||||
|
||||
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
|
||||
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
@@ -94,7 +95,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
|
||||
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
@@ -125,38 +126,6 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
return userData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppUserData retrieveCurrentUser() {
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
AppUser user = this.appUserRepository.findOne(usersThatMatch(currentUser.getOrganisation(), currentUser.getId()));
|
||||
|
||||
List<RoleData> userRoleData = new ArrayList<RoleData>();
|
||||
Set<Role> userRoles = user.getRoles();
|
||||
for (Role role : userRoles) {
|
||||
userRoleData.add(role.toData());
|
||||
}
|
||||
|
||||
AppUserData userData = new AppUserData(user.getId(),
|
||||
user.getUsername(), user.getEmail(), user.getOrganisation()
|
||||
.getId(), user.getOffice().getId(), user.getOffice()
|
||||
.getName());
|
||||
userData.setFirstname(user.getFirstname());
|
||||
userData.setLastname(user.getLastname());
|
||||
|
||||
userData.setAllowedOffices(offices);
|
||||
|
||||
availableRoles.removeAll(userRoleData);
|
||||
userData.setAvailableRoles(availableRoles);
|
||||
userData.setSelectedRoles(userRoleData);
|
||||
|
||||
return userData;
|
||||
}
|
||||
|
||||
private static final class AppUserMapper implements RowMapper<AppUserData> {
|
||||
|
||||
private final List<OfficeData> offices;
|
||||
|
||||
-5
@@ -1,6 +1,5 @@
|
||||
package org.mifosng.platform.user.service;
|
||||
|
||||
import org.mifosng.data.command.ChangePasswordCommand;
|
||||
import org.mifosng.data.command.UserCommand;
|
||||
|
||||
public interface AppUserWritePlatformService {
|
||||
@@ -10,8 +9,4 @@ public interface AppUserWritePlatformService {
|
||||
Long updateUser(final UserCommand command);
|
||||
|
||||
void deleteUser(final Long userId);
|
||||
|
||||
Long updateCurrentUser(UserCommand command);
|
||||
|
||||
Long updateCurrentUserPassword(ChangePasswordCommand command);
|
||||
}
|
||||
+19
-52
@@ -9,7 +9,6 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.mifosng.data.ApiParameterError;
|
||||
import org.mifosng.data.command.ChangePasswordCommand;
|
||||
import org.mifosng.data.command.UserCommand;
|
||||
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
|
||||
@@ -65,7 +64,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
UserCommandValidator validator = new UserCommandValidator(command);
|
||||
validator.validate();
|
||||
validator.validateForCreate();
|
||||
|
||||
Set<Role> allRoles = assembleSetOfRoles(command.getRoles());
|
||||
|
||||
@@ -104,21 +103,16 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
UserCommandValidator validator = new UserCommandValidator(command);
|
||||
validator.validate();
|
||||
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
if (command.getId() == null) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.id.cannot.be.blank", "The parameter id cannot be empty.", "id");
|
||||
dataValidationErrors.add(error);
|
||||
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
validator.validateForUpdate();
|
||||
|
||||
Set<Role> allRoles = assembleSetOfRoles(command.getRoles());
|
||||
|
||||
Office office = this.officeRepository.findOne(command.getOfficeId());
|
||||
if (office == null) {
|
||||
throw new PlatformResourceNotFoundException("error.msg.office.id.invalid", "Office with identifier {0} does not exist.", command.getOfficeId());
|
||||
Office office = null;
|
||||
if (command.getOfficeId() != null) {
|
||||
office = this.officeRepository.findOne(command.getOfficeId());
|
||||
if (office == null) {
|
||||
throw new PlatformResourceNotFoundException("error.msg.office.id.invalid", "Office with identifier {0} does not exist.", command.getOfficeId());
|
||||
}
|
||||
}
|
||||
|
||||
AppUser userToUpdate = this.appUserRepository.findOne(usersThatMatch(currentUser.getOrganisation(), command.getId()));
|
||||
@@ -129,6 +123,17 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
userToUpdate.update(allRoles, office, command.getUsername(), command.getFirstname(), command.getLastname(), command.getEmail());
|
||||
this.appUserRepository.saveAndFlush(userToUpdate);
|
||||
|
||||
if (command.getPassword() != null || command.getRepeatPassword() != null) {
|
||||
PlatformUser dummyPlatformUser = new BasicPasswordEncodablePlatformUser(
|
||||
userToUpdate.getId(),
|
||||
userToUpdate.getUsername(), command.getPassword());
|
||||
|
||||
String newPasswordEncoded = this.platformPasswordEncoder.encode(dummyPlatformUser);
|
||||
|
||||
userToUpdate.updatePasswordOnFirstTimeLogin(newPasswordEncoded);
|
||||
this.appUserRepository.saveAndFlush(userToUpdate);
|
||||
}
|
||||
|
||||
return userToUpdate.getId();
|
||||
} catch (DataIntegrityViolationException dve) {
|
||||
handleOfficeDataIntegrityIssues(command, dve);
|
||||
@@ -153,44 +158,6 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
this.appUserRepository.delete(userId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Long updateCurrentUser(UserCommand command) {
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
UserCommandValidator validator = new UserCommandValidator(command);
|
||||
validator.validateAccountSettingDetails();
|
||||
|
||||
AppUser userToUpdate = this.appUserRepository.findOne(currentUser.getId());
|
||||
|
||||
userToUpdate.update(command.getUsername(), command.getFirstname(), command.getLastname(), command.getEmail());
|
||||
|
||||
this.appUserRepository.save(userToUpdate);
|
||||
|
||||
return userToUpdate.getId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Long updateCurrentUserPassword(ChangePasswordCommand command) {
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
ChangePasswordCommandValidator validator = new ChangePasswordCommandValidator(command);
|
||||
validator.validate();
|
||||
|
||||
AppUser userToUpdate = this.appUserRepository.findOne(currentUser.getId());
|
||||
|
||||
PlatformUser dummyPlatformUser = new BasicPasswordEncodablePlatformUser(
|
||||
userToUpdate.getId(),
|
||||
userToUpdate.getUsername(), command.getPassword());
|
||||
|
||||
String newPasswordEncoded = this.platformPasswordEncoder.encode(dummyPlatformUser);
|
||||
|
||||
userToUpdate.updatePasswordOnFirstTimeLogin(newPasswordEncoded);
|
||||
|
||||
return userToUpdate.getId();
|
||||
}
|
||||
|
||||
/*
|
||||
* Guaranteed to throw an exception no matter what the data integrity issue is.
|
||||
*/
|
||||
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package org.mifosng.platform.user.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.mifosng.data.ApiParameterError;
|
||||
import org.mifosng.data.command.ChangePasswordCommand;
|
||||
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||
|
||||
public class ChangePasswordCommandValidator {
|
||||
|
||||
private final ChangePasswordCommand command;
|
||||
|
||||
public ChangePasswordCommandValidator(ChangePasswordCommand command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
if (StringUtils.isBlank(command.getPassword())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.changepassword.password.cannot.be.blank", "The parameter password cannot be blank.", "password");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(command.getPasswordrepeat())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.changepassword.passwordrepeat.cannot.be.blank", "The parameter passwordrepeat cannot be blank.", "passwordrepeat");
|
||||
dataValidationErrors.add(error);
|
||||
} else {
|
||||
if (!command.getPasswordrepeat().equals(command.getPassword())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.changepassword.passwordrepeat.not.the.same", "The parameter passwordrepeat cannot be blank.", "passwordrepeat");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
}
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
-47
@@ -3,12 +3,10 @@ package org.mifosng.platform.user.service;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.mifosng.data.ApiParameterError;
|
||||
import org.mifosng.data.command.UserCommand;
|
||||
import org.mifosng.platform.DataValidatorBuilder;
|
||||
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
public class UserCommandValidator {
|
||||
|
||||
@@ -21,72 +19,43 @@ public class UserCommandValidator {
|
||||
public void validateForUpdate() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
|
||||
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();
|
||||
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 validate() {
|
||||
public void validateForCreate() {
|
||||
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
validateAccountSettings(dataValidationErrors);
|
||||
|
||||
if (command.getOfficeId() == null || command.getOfficeId().equals(Long.valueOf(-1))) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.office.cannot.be.blank", "The parameter officeId cannot be blank.", "officeId");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (ObjectUtils.isEmpty(command.getRoles())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.roles.cannot.be.blank", "The parameter selectedItems cannot be blank.", "selectedItems");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
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();
|
||||
baseDataValidator.reset().parameter("roles").value(command.getRoles()).arrayNotEmpty();
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
public void validateAccountSettingDetails() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
validateAccountSettings(dataValidationErrors);
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateAccountSettings(List<ApiParameterError> dataValidationErrors) {
|
||||
if (StringUtils.isBlank(command.getUsername())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.username.cannot.be.blank", "The parameter username cannot be blank.", "username");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(command.getFirstname())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.firstname.cannot.be.blank", "The parameter firstname cannot be blank.", "firstname");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(command.getLastname())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.lastname.cannot.be.blank", "The parameter lastname cannot be blank.", "lastname");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(command.getEmail())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.user.email.cannot.be.blank", "The parameter email cannot be blank.", "email");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user