some work on role and user validation and updates.
This commit is contained in:
@@ -12,8 +12,8 @@ public class RoleCommand {
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
private String[] notSelectedItems;
|
||||
private String[] selectedItems;
|
||||
private String[] notSelectedPermissions;
|
||||
private String[] permissions;
|
||||
|
||||
protected RoleCommand() {
|
||||
//
|
||||
@@ -22,7 +22,7 @@ public class RoleCommand {
|
||||
public RoleCommand(final String name, final String description, final String[] permissionIds) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.selectedItems = permissionIds;
|
||||
this.permissions = permissionIds;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -49,19 +49,19 @@ public class RoleCommand {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String[] getNotSelectedItems() {
|
||||
return notSelectedItems;
|
||||
public String[] getNotSelectedPermissions() {
|
||||
return notSelectedPermissions;
|
||||
}
|
||||
|
||||
public void setNotSelectedItems(String... notSelectedItems) {
|
||||
this.notSelectedItems = notSelectedItems;
|
||||
public void setNotSelectedPermissions(String[] notSelectedPermissions) {
|
||||
this.notSelectedPermissions = notSelectedPermissions;
|
||||
}
|
||||
|
||||
public String[] getSelectedItems() {
|
||||
return selectedItems;
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setSelectedItems(String... selectedItems) {
|
||||
this.selectedItems = selectedItems;
|
||||
public void setPermissions(String[] permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,8 @@ public class UserCommand implements Serializable {
|
||||
private String email;
|
||||
private Long officeId;
|
||||
|
||||
private String[] notSelectedItems;
|
||||
private String[] selectedItems;
|
||||
private String[] notSelectedRoles;
|
||||
private String[] roles;
|
||||
|
||||
public UserCommand() {
|
||||
//
|
||||
@@ -85,19 +85,19 @@ public class UserCommand implements Serializable {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String[] getNotSelectedItems() {
|
||||
return notSelectedItems;
|
||||
public String[] getNotSelectedRoles() {
|
||||
return notSelectedRoles;
|
||||
}
|
||||
|
||||
public void setNotSelectedItems(String... notSelectedItems) {
|
||||
this.notSelectedItems = notSelectedItems;
|
||||
public void setNotSelectedRoles(String... notSelectedRoles) {
|
||||
this.notSelectedRoles = notSelectedRoles;
|
||||
}
|
||||
|
||||
public String[] getSelectedItems() {
|
||||
return selectedItems;
|
||||
public String[] getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setSelectedItems(String... selectedItems) {
|
||||
this.selectedItems = selectedItems;
|
||||
public void setRoles(String... roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,6 @@
|
||||
<head>
|
||||
<jsp:include page="../common-head.jsp" />
|
||||
|
||||
<c:url value="/admin/user/all" var="allUsersUrl" />
|
||||
<c:url value="/admin/role/all" var="allRolesUrl" />
|
||||
<c:url value="/admin/permission/all" var="allPermissionsUrl" />
|
||||
<c:url value="/" var="rootContext" />
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
@@ -169,7 +166,7 @@ $(document).ready(function() {
|
||||
o[this.name].push(this.value || '');
|
||||
} else {
|
||||
|
||||
if (this.name === 'selectedItems' || this.name === 'notSelectedItems') {
|
||||
if (this.name === 'permissions' || this.name === 'notSelectedPermissions' || this.name === 'roles' || this.name === 'notSelectedRoles') {
|
||||
o[this.name] = new Array();
|
||||
o[this.name].push(this.value || '');
|
||||
} else {
|
||||
@@ -180,7 +177,7 @@ $(document).ready(function() {
|
||||
return o;
|
||||
};
|
||||
|
||||
function popupDialogWithFormView(getUrl, postUrl, submitType, titleCode, templateSelector, width, height, saveSuccessFunction) {
|
||||
function popupDialogWithFormView(getUrl, postUrl, submitType, titleCode, templateSelector, width, height, saveSuccessFunction, roles) {
|
||||
var dialogDiv = $("<div id='dialog-form'></div>");
|
||||
var jqxhr = $.ajax({
|
||||
url: getUrl,
|
||||
@@ -204,13 +201,24 @@ $(document).ready(function() {
|
||||
var buttonsOpts = {};
|
||||
buttonsOpts[saveButton] = function() {
|
||||
|
||||
$('#notSelectedItems option').each(function(i) {
|
||||
if (roles) {
|
||||
// used for role form
|
||||
$('#notSelectedPermissions option').each(function(i) {
|
||||
$(this).attr("selected", "selected");
|
||||
});
|
||||
|
||||
$('#permissions option').each(function(i) {
|
||||
$(this).attr("selected", "selected");
|
||||
});
|
||||
|
||||
$('#selectedItems option').each(function(i) {
|
||||
$(this).attr("selected", "selected");
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$('#notSelectedRoles option').each(function(i) {
|
||||
$(this).attr("selected", "selected");
|
||||
});
|
||||
|
||||
$('#roles option').each(function(i) {
|
||||
$(this).attr("selected", "selected");
|
||||
});
|
||||
}
|
||||
|
||||
var newFormData = JSON.stringify($('#entityform').serializeObject());
|
||||
console.log(newFormData);
|
||||
@@ -223,7 +231,6 @@ $(document).ready(function() {
|
||||
data: newFormData,
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: saveSuccessFunction,
|
||||
@@ -247,13 +254,23 @@ $(document).ready(function() {
|
||||
},
|
||||
open: function (event, ui) {
|
||||
|
||||
$('#add').click(function() {
|
||||
return !$('#notSelectedItems option:selected').remove().appendTo('#selectedItems');
|
||||
});
|
||||
|
||||
$('#remove').click(function() {
|
||||
return !$('#selectedItems option:selected').remove().appendTo('#notSelectedItems');
|
||||
});
|
||||
if (roles) {
|
||||
$('#add').click(function() {
|
||||
return !$('#notSelectedPermissions option:selected').remove().appendTo('#permissions');
|
||||
});
|
||||
|
||||
$('#remove').click(function() {
|
||||
return !$('#permissions option:selected').remove().appendTo('#notSelectedPermissions');
|
||||
});
|
||||
} else {
|
||||
$('#add').click(function() {
|
||||
return !$('#notSelectedRoles option:selected').remove().appendTo('#roles');
|
||||
});
|
||||
|
||||
$('#remove').click(function() {
|
||||
return !$('#roles option:selected').remove().appendTo('#notSelectedRoles');
|
||||
});
|
||||
}
|
||||
|
||||
$('.datepickerfield').datepicker({constrainInput: true, maxDate: 0, dateFormat: 'dd MM yy'});
|
||||
|
||||
@@ -278,7 +295,6 @@ $(document).ready(function() {
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: function(data, textStatus, jqXHR) {
|
||||
@@ -312,7 +328,6 @@ $(document).ready(function() {
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: function(data, textStatus, jqXHR) {
|
||||
@@ -353,7 +368,7 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
$('#adduser').click(function(e) {
|
||||
var url = baseApiUrl + 'template';
|
||||
var url = baseApiUrl + 'users/template';
|
||||
var postUrl = baseApiUrl + "users";
|
||||
var templateSelector = "#userFormTemplate";
|
||||
var width = 1000;
|
||||
@@ -364,7 +379,7 @@ $(document).ready(function() {
|
||||
refreshUsersView();
|
||||
}
|
||||
|
||||
popupDialogWithFormView(url, postUrl, 'POST', 'dialog.title.add.user', templateSelector, width, height, saveSuccessFunction);
|
||||
popupDialogWithFormView(url, postUrl, 'POST', 'dialog.title.add.user', templateSelector, width, height, saveSuccessFunction, false);
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
@@ -375,7 +390,7 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
$('#addrole').click(function(e) {
|
||||
var url = baseApiUrl +'template';
|
||||
var url = baseApiUrl + 'roles/template';
|
||||
var postUrl = baseApiUrl + "roles";
|
||||
var templateSelector = "#roleFormTemplate";
|
||||
var width = 1000;
|
||||
@@ -386,7 +401,7 @@ $(document).ready(function() {
|
||||
refreshRolesView();
|
||||
}
|
||||
|
||||
popupDialogWithFormView(url, postUrl, 'POST', 'dialog.title.add.role', templateSelector, width, height, saveSuccessFunction);
|
||||
popupDialogWithFormView(url, postUrl, 'POST', 'dialog.title.add.role', templateSelector, width, height, saveSuccessFunction, true);
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
@@ -409,7 +424,6 @@ $(document).ready(function() {
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: function(data, textStatus, jqXHR) {
|
||||
@@ -432,7 +446,7 @@ $(document).ready(function() {
|
||||
refreshUsersView();
|
||||
}
|
||||
|
||||
popupDialogWithFormView(getUrl, putUrl, 'PUT', "dialog.title.edit.details", templateSelector, width, height, saveSuccessFunction);
|
||||
popupDialogWithFormView(getUrl, putUrl, 'PUT', "dialog.title.edit.details", templateSelector, width, height, saveSuccessFunction, false);
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
@@ -476,7 +490,6 @@ $(document).ready(function() {
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: function(data, textStatus, jqXHR) {
|
||||
@@ -499,7 +512,7 @@ $(document).ready(function() {
|
||||
refreshUsersView();
|
||||
}
|
||||
|
||||
popupDialogWithFormView(getUrl, putUrl, 'PUT', "dialog.title.edit.details", templateSelector, width, height, saveSuccessFunction);
|
||||
popupDialogWithFormView(getUrl, putUrl, 'PUT', "dialog.title.edit.details", templateSelector, width, height, saveSuccessFunction, true);
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
@@ -543,7 +556,6 @@ $(document).ready(function() {
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: function(xhr) {
|
||||
console.log("base64: " + base64);
|
||||
xhr.setRequestHeader("Authorization", "Basic " + base64);
|
||||
},
|
||||
success: function(data, textStatus, jqXHR) {
|
||||
|
||||
+8
-8
@@ -490,8 +490,8 @@
|
||||
<br/>
|
||||
|
||||
<div class="multiselectwidget">
|
||||
<label for="notSelectedItems"><spring:message code="form.label.user.available.roles"/></label>
|
||||
<select id="notSelectedItems" name="notSelectedItems" title="Available application roles." class="multiselectwidget multiNotSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
<label for="notSelectedRoles"><spring:message code="form.label.user.available.roles"/></label>
|
||||
<select id="notSelectedRoles" name="notSelectedRoles" title="Available application roles." class="multiselectwidget multiNotSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
{{#each availableRoles}}
|
||||
<option value="{{=id}}">{{=name}}</option>
|
||||
{{/each}}
|
||||
@@ -500,8 +500,8 @@
|
||||
</div>
|
||||
|
||||
<div class="multiselectwidget">
|
||||
<label for="selectedItems"><spring:message code="form.label.user.selected.roles"/></label>
|
||||
<select id="selectedItems" name="selectedItems" title="User selected roles." class="multiselectwidget multiSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
<label for="roles"><spring:message code="form.label.user.selected.roles"/></label>
|
||||
<select id="roles" name="roles" title="User selected roles." class="multiselectwidget multiSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
{{#each selectedRoles}}
|
||||
<option value="{{=id}}" selected="selected">{{=name}}</option>
|
||||
{{/each}}
|
||||
@@ -526,8 +526,8 @@
|
||||
<br/>
|
||||
|
||||
<div class="multiselectwidget">
|
||||
<label for="notSelectedItems"><spring:message code="form.label.role.available.permissions"/></label>
|
||||
<select id="notSelectedItems" name="notSelectedItems" title="" class="multiselectwidget multiNotSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
<label for="notSelectedPermissions"><spring:message code="form.label.role.available.permissions"/></label>
|
||||
<select id="notSelectedPermissions" name="notSelectedPermissions" title="" class="multiselectwidget multiNotSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
{{#each availablePermissions}}
|
||||
<option value="{{=id}}" title="{{=description}}">{{=name}}</option>
|
||||
{{/each}}
|
||||
@@ -536,8 +536,8 @@
|
||||
</div>
|
||||
|
||||
<div class="multiselectwidget">
|
||||
<label for="selectedItems"><spring:message code="form.label.role.selected.permissions"/></label>
|
||||
<select id="selectedItems" name="selectedItems" title="" class="multiselectwidget multiSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
<label for="permissions"><spring:message code="form.label.role.selected.permissions"/></label>
|
||||
<select id="permissions" name="permissions" title="" class="multiselectwidget multiSelectedItems" multiple="multiple" style="width:450px;height:125px;">
|
||||
{{#each selectedPermissions}}
|
||||
<option value="{{=id}}" title="{{=description}}">{{=name}}</option>
|
||||
{{/each}}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.mifosng.data.ApiParameterError;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
public class DataValidatorBuilder {
|
||||
|
||||
@@ -112,6 +113,21 @@ public class DataValidatorBuilder {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataValidatorBuilder arrayNotEmpty() {
|
||||
if (value == null && ignoreNullValue) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Object[] array = (Object[]) value;
|
||||
if (ObjectUtils.isEmpty(array)) {
|
||||
StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(resource).append(".").append(parameter).append(".cannot.be.empty");
|
||||
StringBuilder defaultEnglishMessage = new StringBuilder("The parameter ").append(parameter).append(" cannot be empty. You must select at least one permission.");
|
||||
ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), parameter);
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataValidatorBuilder anyOfNotNull(Object... object) {
|
||||
boolean hasData = false;
|
||||
|
||||
@@ -39,7 +39,7 @@ public class RoleApiResource {
|
||||
private RoleWritePlatformService roleWritePlatformService;
|
||||
|
||||
@GET
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response retrieveAllRoles() {
|
||||
|
||||
@@ -49,7 +49,7 @@ public class RoleApiResource {
|
||||
|
||||
@GET
|
||||
@Path("template")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response retrieveNewRoleDetails() {
|
||||
|
||||
@@ -62,8 +62,8 @@ public class RoleApiResource {
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response createRole(final RoleCommand command) {
|
||||
|
||||
Long roleId = this.roleWritePlatformService.createRole(command);
|
||||
@@ -72,7 +72,7 @@ public class RoleApiResource {
|
||||
|
||||
@GET
|
||||
@Path("{roleId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response retrieveRole(@PathParam("roleId") final Long roleId) {
|
||||
|
||||
@@ -87,8 +87,8 @@ public class RoleApiResource {
|
||||
|
||||
@PUT
|
||||
@Path("{roleId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({ MediaType.APPLICATION_JSON})
|
||||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response updateRole(@PathParam("roleId") final Long roleId, final RoleCommand command) {
|
||||
|
||||
command.setId(roleId);
|
||||
|
||||
@@ -81,10 +81,19 @@ public class Role extends AbstractAuditableCustom<AppUser, Long> {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* When updating role details, any parameters with null values are ignored.
|
||||
*/
|
||||
public void update(String name, String description, List<Permission> selectedPermissions) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.permissions.clear();
|
||||
this.permissions = new HashSet<Permission>(selectedPermissions);
|
||||
if (name != null) {
|
||||
this.name = name;
|
||||
}
|
||||
if (description != null) {
|
||||
this.description = description;
|
||||
}
|
||||
if (selectedPermissions != null) {
|
||||
this.permissions.clear();
|
||||
this.permissions = new HashSet<Permission>(selectedPermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-11
@@ -32,6 +32,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
@Service
|
||||
public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWritePlatformService {
|
||||
@@ -66,11 +67,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
UserCommandValidator validator = new UserCommandValidator(command);
|
||||
validator.validate();
|
||||
|
||||
Set<Role> allRoles = new HashSet<Role>();
|
||||
for (String roleId : command.getSelectedItems()) {
|
||||
Role role = this.roleRepository.findOne(Long.valueOf(roleId));
|
||||
allRoles.add(role);
|
||||
}
|
||||
Set<Role> allRoles = assembleSetOfRoles(command.getRoles());
|
||||
|
||||
Office office = this.officeRepository.findOne(command.getOfficeId());
|
||||
|
||||
@@ -98,7 +95,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Long updateUser(UserCommand command) {
|
||||
@@ -117,11 +114,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
|
||||
Set<Role> allRoles = new HashSet<Role>();
|
||||
for (String roleId : command.getSelectedItems()) {
|
||||
Role role = this.roleRepository.findOne(Long.valueOf(roleId));
|
||||
allRoles.add(role);
|
||||
}
|
||||
Set<Role> allRoles = assembleSetOfRoles(command.getRoles());
|
||||
|
||||
Office office = this.officeRepository.findOne(command.getOfficeId());
|
||||
if (office == null) {
|
||||
@@ -143,6 +136,17 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Role> assembleSetOfRoles(final String[] rolesArray) {
|
||||
Set<Role> allRoles = new HashSet<Role>();
|
||||
if (!ObjectUtils.isEmpty(rolesArray)) {
|
||||
for (String roleId : rolesArray) {
|
||||
Role role = this.roleRepository.findOne(Long.valueOf(roleId));
|
||||
allRoles.add(role);
|
||||
}
|
||||
}
|
||||
return allRoles;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public void deleteUser(Long userId) {
|
||||
|
||||
+16
-31
@@ -3,11 +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.RoleCommand;
|
||||
import org.mifosng.platform.DataValidatorBuilder;
|
||||
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
public class RoleCommandValidator {
|
||||
|
||||
@@ -20,7 +19,13 @@ public class RoleCommandValidator {
|
||||
public void validateForCreate() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
validate(dataValidationErrors);
|
||||
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);
|
||||
baseDataValidator.reset().parameter("permissions").value(command.getPermissions()).arrayNotEmpty();
|
||||
|
||||
baseDataValidator.reset().anyOfNotNull(command.getName(), command.getDescription(), command.getPermissions());
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
@@ -30,37 +35,17 @@ public class RoleCommandValidator {
|
||||
public void validateForUpdate() {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
if (command.getId() == null || command.getId() < 1) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.role.id.not.provided", "The parameter id cannot be blank.", "id");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("role");
|
||||
|
||||
validate(dataValidationErrors);
|
||||
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().parameter("permissions").value(command.getPermissions()).ignoreIfNull().arrayNotEmpty();
|
||||
|
||||
baseDataValidator.reset().anyOfNotNull(command.getName(), command.getDescription(), command.getPermissions());
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
private void validate(List<ApiParameterError> dataValidationErrors) {
|
||||
if (StringUtils.isBlank(command.getName())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.role.name.cannot.be.blank", "The parameter name cannot be blank.", "name");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(command.getDescription())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.role.description.cannot.be.blank", "The parameter description cannot be blank.", "description");
|
||||
dataValidationErrors.add(error);
|
||||
} else {
|
||||
if (command.getDescription().trim().length() > 500) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.role.description.exceeds.max.length", "The parameter description has data that exceeds its max allowed length of {0}.", "description", 500);
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
}
|
||||
|
||||
if (ObjectUtils.isEmpty(command.getSelectedItems())) {
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.role.permissions.cannot.be.empty", "The parameter selectedItems cannot be blank. You must select at least one permission.", "selectedItems");
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
-26
@@ -16,6 +16,7 @@ import org.mifosng.platform.user.domain.RoleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
@Service
|
||||
public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatformService {
|
||||
@@ -41,18 +42,7 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
RoleCommandValidator validator = new RoleCommandValidator(command);
|
||||
validator.validateForCreate();
|
||||
|
||||
List<Long> selectedPermissionIds = new ArrayList<Long>();
|
||||
for (String selectedId : command.getSelectedItems()) {
|
||||
selectedPermissionIds.add(Long.valueOf(selectedId));
|
||||
}
|
||||
|
||||
List<Permission> selectedPermissions = new ArrayList<Permission>();
|
||||
Collection<Permission> allPermissions = this.permissionRepository.findAll();
|
||||
for (Permission permission : allPermissions) {
|
||||
if (selectedPermissionIds.contains(permission.getId())) {
|
||||
selectedPermissions.add(permission);
|
||||
}
|
||||
}
|
||||
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command.getPermissions());
|
||||
|
||||
Role entity = new Role(currentUser.getOrganisation(), command.getName(), command.getDescription(), selectedPermissions);
|
||||
|
||||
@@ -60,7 +50,7 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Long updateRole(RoleCommand command) {
|
||||
@@ -70,19 +60,7 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
RoleCommandValidator validator = new RoleCommandValidator(command);
|
||||
validator.validateForUpdate();
|
||||
|
||||
List<Long> selectedPermissionIds = new ArrayList<Long>();
|
||||
for (String selectedId : command.getSelectedItems()) {
|
||||
selectedPermissionIds.add(Long.valueOf(selectedId));
|
||||
}
|
||||
|
||||
List<Permission> selectedPermissions = new ArrayList<Permission>();
|
||||
Collection<Permission> allPermissions = this.permissionRepository
|
||||
.findAll();
|
||||
for (Permission permission : allPermissions) {
|
||||
if (selectedPermissionIds.contains(permission.getId())) {
|
||||
selectedPermissions.add(permission);
|
||||
}
|
||||
}
|
||||
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command.getPermissions());
|
||||
|
||||
Role role = this.roleRepository.findOne(rolesThatMatch(currentUser.getOrganisation(), command.getId()));
|
||||
role.update(command.getName(), command.getDescription(), selectedPermissions);
|
||||
@@ -91,4 +69,24 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
|
||||
return role.getId();
|
||||
}
|
||||
|
||||
private List<Permission> assembleListOfSelectedPermissions(final String[] selectedPermissionsArray) {
|
||||
List<Long> selectedPermissionIds = new ArrayList<Long>();
|
||||
List<Permission> selectedPermissions = new ArrayList<Permission>();
|
||||
|
||||
if (!ObjectUtils.isEmpty(selectedPermissionsArray)) {
|
||||
for (String selectedId : selectedPermissionsArray) {
|
||||
selectedPermissionIds.add(Long.valueOf(selectedId));
|
||||
}
|
||||
|
||||
|
||||
Collection<Permission> allPermissions = this.permissionRepository.findAll();
|
||||
for (Permission permission : allPermissions) {
|
||||
if (selectedPermissionIds.contains(permission.getId())) {
|
||||
selectedPermissions.add(permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
return selectedPermissions;
|
||||
}
|
||||
}
|
||||
+21
-1
@@ -6,6 +6,7 @@ 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;
|
||||
|
||||
@@ -16,6 +17,25 @@ public class UserCommandValidator {
|
||||
public UserCommandValidator(UserCommand command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
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("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("roles").value(command.getRoles()).ignoreIfNull().arrayNotEmpty();
|
||||
|
||||
baseDataValidator.reset().parameter("password").value(command.getPassword()).ignoreIfNull().notBlank();
|
||||
baseDataValidator.reset().parameter("passwordRepeat").value(command.getPassword()).ignoreIfNull().notBlank();
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
|
||||
@@ -28,7 +48,7 @@ public class UserCommandValidator {
|
||||
dataValidationErrors.add(error);
|
||||
}
|
||||
|
||||
if (ObjectUtils.isEmpty(command.getSelectedItems())) {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user