catch error cases around users and roles.
This commit is contained in:
@@ -12,10 +12,9 @@ public class RoleCommand {
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
private String[] notSelectedPermissions;
|
||||
private String[] permissions;
|
||||
|
||||
protected RoleCommand() {
|
||||
public RoleCommand() {
|
||||
//
|
||||
}
|
||||
|
||||
@@ -49,14 +48,6 @@ public class RoleCommand {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String[] getNotSelectedPermissions() {
|
||||
return notSelectedPermissions;
|
||||
}
|
||||
|
||||
public void setNotSelectedPermissions(String[] notSelectedPermissions) {
|
||||
this.notSelectedPermissions = notSelectedPermissions;
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@@ -159,20 +159,25 @@ $(document).ready(function() {
|
||||
var o = {};
|
||||
var a = this.serializeArray();
|
||||
$.each(a, function() {
|
||||
if (o[this.name] !== undefined) {
|
||||
if (!o[this.name].push) {
|
||||
o[this.name] = [o[this.name]];
|
||||
}
|
||||
o[this.name].push(this.value || '');
|
||||
} else {
|
||||
|
||||
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 {
|
||||
o[this.name] = this.value || '';
|
||||
}
|
||||
}
|
||||
|
||||
if (this.name === 'notSelectedPermissions' || this.name === 'notSelectedRoles') {
|
||||
// do not serialize
|
||||
} else {
|
||||
if (o[this.name] !== undefined) {
|
||||
if (!o[this.name].push) {
|
||||
o[this.name] = [o[this.name]];
|
||||
}
|
||||
o[this.name].push(this.value || '');
|
||||
} else {
|
||||
|
||||
if (this.name === 'permissions' || this.name === 'roles') {
|
||||
o[this.name] = new Array();
|
||||
o[this.name].push(this.value || '');
|
||||
} else {
|
||||
o[this.name] = this.value || '';
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return o;
|
||||
};
|
||||
|
||||
@@ -86,7 +86,7 @@ public class DataValidatorBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(value.toString())) {
|
||||
if (value == null || StringUtils.isBlank(value.toString())) {
|
||||
StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(resource).append(".").append(parameter).append(".cannot.be.blank");
|
||||
StringBuilder defaultEnglishMessage = new StringBuilder("The parameter ").append(parameter).append(" cannot be blank.");
|
||||
ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), defaultEnglishMessage.toString(), parameter);
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package org.mifosng.platform.exceptions;
|
||||
|
||||
/**
|
||||
* A {@link RuntimeException} thrown when role resources are not found.
|
||||
*/
|
||||
public class RoleNotFoundException extends PlatformResourceNotFoundException {
|
||||
|
||||
public RoleNotFoundException(Long id) {
|
||||
super("error.msg.role.id.invalid", "Role with identifier " + id + " does not exist", id);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package org.mifosng.platform.exceptions;
|
||||
|
||||
/**
|
||||
* A {@link RuntimeException} thrown when user resources are not found.
|
||||
*/
|
||||
public class UserNotFoundException extends PlatformResourceNotFoundException {
|
||||
|
||||
public UserNotFoundException(Long id) {
|
||||
super("error.msg.user.id.invalid", "User with identifier " + id + " does not exist", id);
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ public class Role extends AbstractAuditableCustom<AppUser, Long> {
|
||||
if (description != null) {
|
||||
this.description = description;
|
||||
}
|
||||
if (selectedPermissions != null) {
|
||||
if (!selectedPermissions.isEmpty()) {
|
||||
this.permissions.clear();
|
||||
this.permissions = new HashSet<Permission>(selectedPermissions);
|
||||
}
|
||||
|
||||
+3
-4
@@ -15,7 +15,7 @@ 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.exceptions.RoleNotFoundException;
|
||||
import org.mifosng.platform.infrastructure.JdbcSupport;
|
||||
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
|
||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||
@@ -102,7 +102,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
|
||||
AppUser user = this.appUserRepository.findOne(usersThatMatch(currentUser.getOrganisation(), userId));
|
||||
if (user == null) {
|
||||
throw new PlatformResourceNotFoundException("error.msg.user.id.invalid", "User with identifier {0} does not exist.", userId);
|
||||
throw new RoleNotFoundException(userId);
|
||||
}
|
||||
|
||||
List<RoleData> userRoleData = new ArrayList<RoleData>();
|
||||
@@ -161,8 +161,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
return " u.id as id, u.username as username, u.firstname as firstname, u.lastname as lastname, u.email as email, u.org_id as orgId, u.office_id as officeId from admin_appuser u ";
|
||||
}
|
||||
|
||||
private String fromOfficeList(final List<OfficeData> officeList,
|
||||
final Long officeId) {
|
||||
private String fromOfficeList(final List<OfficeData> officeList, final Long officeId) {
|
||||
String match = "";
|
||||
for (OfficeData office : officeList) {
|
||||
if (office.getId().equals(officeId)) {
|
||||
|
||||
+10
-4
@@ -1,5 +1,7 @@
|
||||
package org.mifosng.platform.user.service;
|
||||
|
||||
import static org.mifosng.platform.Specifications.rolesThatMatch;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
@@ -7,6 +9,7 @@ import java.util.Collection;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.mifosng.data.RoleData;
|
||||
import org.mifosng.platform.exceptions.RoleNotFoundException;
|
||||
import org.mifosng.platform.infrastructure.JdbcSupport;
|
||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||
import org.mifosng.platform.user.domain.AppUser;
|
||||
@@ -43,10 +46,13 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService {
|
||||
|
||||
// FIXME - does it make sense to use JDBC over repository for this?
|
||||
@Override
|
||||
public RoleData retrieveRole(Long roleId) {
|
||||
|
||||
Role role = this.roleRepository.findOne(roleId);
|
||||
|
||||
public RoleData retrieveRole(final Long id) {
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
|
||||
Role role = this.roleRepository.findOne(rolesThatMatch(currentUser.getOrganisation(), id));
|
||||
if (role == null) {
|
||||
throw new RoleNotFoundException(id);
|
||||
}
|
||||
return role.toData();
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -7,6 +7,7 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.mifosng.data.command.RoleCommand;
|
||||
import org.mifosng.platform.exceptions.RoleNotFoundException;
|
||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||
import org.mifosng.platform.user.domain.AppUser;
|
||||
import org.mifosng.platform.user.domain.Permission;
|
||||
@@ -63,6 +64,9 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command.getPermissions());
|
||||
|
||||
Role role = this.roleRepository.findOne(rolesThatMatch(currentUser.getOrganisation(), command.getId()));
|
||||
if (role == null) {
|
||||
throw new RoleNotFoundException(command.getId());
|
||||
}
|
||||
role.update(command.getName(), command.getDescription(), selectedPermissions);
|
||||
|
||||
this.roleRepository.save(role);
|
||||
@@ -78,7 +82,6 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf
|
||||
for (String selectedId : selectedPermissionsArray) {
|
||||
selectedPermissionIds.add(Long.valueOf(selectedId));
|
||||
}
|
||||
|
||||
|
||||
Collection<Permission> allPermissions = this.permissionRepository.findAll();
|
||||
for (Permission permission : allPermissions) {
|
||||
|
||||
Reference in New Issue
Block a user