filtering users
This commit is contained in:
@@ -5,7 +5,12 @@ import java.util.List;
|
|||||||
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
import org.codehaus.jackson.map.annotate.JsonFilter;
|
||||||
|
|
||||||
@XmlRootElement(name = "user")
|
@XmlRootElement(name = "user")
|
||||||
|
@JsonIgnoreProperties({ "orgId" })
|
||||||
|
@JsonFilter("userFilter")
|
||||||
public class AppUserData {
|
public class AppUserData {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ public class PermissionData implements Serializable {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionData(final Long id, final Long orgId,
|
public PermissionData(final Long id, final Long orgId, final String name,
|
||||||
final String name, final String description, final String code, int groupType) {
|
final String description, final String code, int groupType) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.orgId = orgId;
|
this.orgId = orgId;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|||||||
@@ -10,13 +10,15 @@ import javax.ws.rs.PUT;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
import org.mifosng.data.AppUserData;
|
import org.mifosng.data.AppUserData;
|
||||||
import org.mifosng.data.EntityIdentifier;
|
import org.mifosng.data.EntityIdentifier;
|
||||||
import org.mifosng.data.UserList;
|
|
||||||
import org.mifosng.data.command.UserCommand;
|
import org.mifosng.data.command.UserCommand;
|
||||||
|
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
|
||||||
import org.mifosng.platform.user.service.AppUserReadPlatformService;
|
import org.mifosng.platform.user.service.AppUserReadPlatformService;
|
||||||
import org.mifosng.platform.user.service.AppUserWritePlatformService;
|
import org.mifosng.platform.user.service.AppUserWritePlatformService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -28,47 +30,62 @@ import org.springframework.stereotype.Component;
|
|||||||
@Scope("singleton")
|
@Scope("singleton")
|
||||||
public class UserApiResource {
|
public class UserApiResource {
|
||||||
|
|
||||||
@Autowired
|
private String allowedFieldList = "allowedOffices,availableRoles";
|
||||||
|
private String filterName = "userFilter";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
private AppUserReadPlatformService appUserReadPlatformService;
|
private AppUserReadPlatformService appUserReadPlatformService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppUserWritePlatformService appUserWritePlatformService;
|
private AppUserWritePlatformService appUserWritePlatformService;
|
||||||
|
|
||||||
@GET
|
@Autowired
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
private ApiJSONFormattingService jsonFormattingService;
|
||||||
@Produces({MediaType.APPLICATION_JSON})
|
|
||||||
public Response retrieveUsers() {
|
|
||||||
|
|
||||||
Collection<AppUserData> users = this.appUserReadPlatformService.retrieveAllUsers();
|
@GET
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public String retrieveUsers(@Context UriInfo uriInfo) {
|
||||||
|
|
||||||
return Response.ok().entity(new UserList(users)).build();
|
Collection<AppUserData> users = this.appUserReadPlatformService
|
||||||
|
.retrieveAllUsers();
|
||||||
|
|
||||||
|
String selectedFields = "id,officeId,officeName,username,firstname,lastname,email";
|
||||||
|
return this.jsonFormattingService.convertRequest(users, filterName,
|
||||||
|
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("{userId}")
|
@Path("{userId}")
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
@Produces({ MediaType.APPLICATION_JSON})
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
public Response retrieveUser(@PathParam("userId") final Long userId) {
|
public String retrieveUser(@PathParam("userId") final Long userId,
|
||||||
|
@Context UriInfo uriInfo) {
|
||||||
|
|
||||||
AppUserData user = this.appUserReadPlatformService.retrieveUser(userId);
|
AppUserData user = this.appUserReadPlatformService.retrieveUser(userId);
|
||||||
|
|
||||||
return Response.ok().entity(user).build();
|
String selectedFields = "";
|
||||||
|
return this.jsonFormattingService.convertRequest(user, filterName,
|
||||||
|
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("template")
|
@Path("template")
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
@Produces({ MediaType.APPLICATION_JSON})
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
public Response newUserDetails() {
|
public String newUserDetails(@Context UriInfo uriInfo) {
|
||||||
|
|
||||||
AppUserData newUser = this.appUserReadPlatformService.retrieveNewUserDetails();
|
AppUserData newUser = this.appUserReadPlatformService
|
||||||
|
.retrieveNewUserDetails();
|
||||||
|
|
||||||
return Response.ok().entity(newUser).build();
|
String selectedFields = allowedFieldList;
|
||||||
|
return this.jsonFormattingService.convertRequest(newUser, filterName,
|
||||||
|
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
@Produces({ MediaType.APPLICATION_JSON})
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
public Response createUser(UserCommand command) {
|
public Response createUser(UserCommand command) {
|
||||||
|
|
||||||
Long userId = this.appUserWritePlatformService.createUser(command);
|
Long userId = this.appUserWritePlatformService.createUser(command);
|
||||||
@@ -77,8 +94,8 @@ public class UserApiResource {
|
|||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("{userId}")
|
@Path("{userId}")
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
@Produces({ MediaType.APPLICATION_JSON})
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
public Response deleteUser(@PathParam("userId") final Long userId) {
|
public Response deleteUser(@PathParam("userId") final Long userId) {
|
||||||
|
|
||||||
this.appUserWritePlatformService.deleteUser(userId);
|
this.appUserWritePlatformService.deleteUser(userId);
|
||||||
@@ -88,9 +105,10 @@ public class UserApiResource {
|
|||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("{userId}")
|
@Path("{userId}")
|
||||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
@Produces({ MediaType.APPLICATION_JSON})
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
public Response updateUser(@PathParam("userId") final Long userId, UserCommand command) {
|
public Response updateUser(@PathParam("userId") final Long userId,
|
||||||
|
UserCommand command) {
|
||||||
|
|
||||||
command.setId(userId);
|
command.setId(userId);
|
||||||
|
|
||||||
|
|||||||
+109
-57
@@ -86,62 +86,8 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
|||||||
String filterType, String fields, boolean prettyOutput) {
|
String filterType, String fields, boolean prettyOutput) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String json = "";
|
|
||||||
// String myFilter = "myFilter";
|
|
||||||
FilterProvider filters = null;
|
|
||||||
Set<String> filterFields = new HashSet<String>();
|
|
||||||
|
|
||||||
StringTokenizer st = new StringTokenizer(fields, ",");
|
FilterProvider filters = buildFilter(filterName, filterType, fields);
|
||||||
while (st.hasMoreTokens()) {
|
|
||||||
filterFields.add(st.nextToken().trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filterName.equals("roleFilter")) {
|
|
||||||
|
|
||||||
Set<String> permissionFields = new HashSet<String>();
|
|
||||||
//Ask keith which ones to show
|
|
||||||
permissionFields.add("id");
|
|
||||||
permissionFields.add("name");
|
|
||||||
permissionFields.add("description");
|
|
||||||
permissionFields.add("code");
|
|
||||||
permissionFields.add("groupType");
|
|
||||||
|
|
||||||
if (filterType.equals("I")) {
|
|
||||||
filters = new SimpleFilterProvider()
|
|
||||||
.addFilter(
|
|
||||||
filterName,
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.filterOutAllExcept(filterFields))
|
|
||||||
.addFilter(
|
|
||||||
"permissionFilter",
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.filterOutAllExcept(permissionFields));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
filters = new SimpleFilterProvider()
|
|
||||||
.addFilter(
|
|
||||||
filterName,
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.serializeAllExcept(filterFields))
|
|
||||||
.addFilter(
|
|
||||||
"permissionFilter",
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.filterOutAllExcept(permissionFields));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (filterType.equals("I")) {
|
|
||||||
filters = new SimpleFilterProvider().addFilter(filterName,
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.filterOutAllExcept(filterFields));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
filters = new SimpleFilterProvider().addFilter(filterName,
|
|
||||||
SimpleBeanPropertyFilter
|
|
||||||
.serializeAllExcept(filterFields));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ObjectWriter jsonWriter = null;
|
ObjectWriter jsonWriter = null;
|
||||||
if (prettyOutput) {
|
if (prettyOutput) {
|
||||||
jsonWriter = new ObjectMapper()
|
jsonWriter = new ObjectMapper()
|
||||||
@@ -150,9 +96,8 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
|||||||
jsonWriter = new ObjectMapper().writer();
|
jsonWriter = new ObjectMapper().writer();
|
||||||
}
|
}
|
||||||
|
|
||||||
json = jsonWriter.withFilters(filters).writeValueAsString(
|
return jsonWriter.withFilters(filters).writeValueAsString(
|
||||||
dataObject);
|
dataObject);
|
||||||
return json;
|
|
||||||
} catch (JsonGenerationException e) {
|
} catch (JsonGenerationException e) {
|
||||||
throw new PlatformInternalServerException(
|
throw new PlatformInternalServerException(
|
||||||
"error.msg.platform.json.generation",
|
"error.msg.platform.json.generation",
|
||||||
@@ -171,6 +116,113 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private FilterProvider buildFilter(String filterName, String filterType,
|
||||||
|
String fields) {
|
||||||
|
FilterProvider filters = null;
|
||||||
|
|
||||||
|
Set<String> filterFields = new HashSet<String>();
|
||||||
|
StringTokenizer st = new StringTokenizer(fields, ",");
|
||||||
|
while (st.hasMoreTokens()) {
|
||||||
|
filterFields.add(st.nextToken().trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filterName.equals("myFilter")) {
|
||||||
|
|
||||||
|
if (filterType.equals("I")) {
|
||||||
|
return new SimpleFilterProvider().addFilter(filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(filterFields));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new SimpleFilterProvider().addFilter(filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.serializeAllExcept(filterFields));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filterName.equals("roleFilter")) {
|
||||||
|
|
||||||
|
Set<String> permissionFields = new HashSet<String>();
|
||||||
|
// Ask keith which ones to show
|
||||||
|
permissionFields.add("id");
|
||||||
|
permissionFields.add("name");
|
||||||
|
permissionFields.add("description");
|
||||||
|
permissionFields.add("code");
|
||||||
|
permissionFields.add("groupType");
|
||||||
|
|
||||||
|
if (filterType.equals("I")) {
|
||||||
|
return new SimpleFilterProvider().addFilter(
|
||||||
|
filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(filterFields)).addFilter(
|
||||||
|
"permissionFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(permissionFields));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new SimpleFilterProvider().addFilter(
|
||||||
|
filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.serializeAllExcept(filterFields)).addFilter(
|
||||||
|
"permissionFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(permissionFields));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filterName.equals("userFilter")) {
|
||||||
|
|
||||||
|
Set<String> roleFields = new HashSet<String>();
|
||||||
|
roleFields.add("id");
|
||||||
|
roleFields.add("name");
|
||||||
|
roleFields.add("description");
|
||||||
|
roleFields.add("availablePermissions");
|
||||||
|
roleFields.add("selectedPermissions");
|
||||||
|
// Ask keith which ones to show
|
||||||
|
Set<String> permissionFields = new HashSet<String>();
|
||||||
|
// Ask keith which ones to show
|
||||||
|
permissionFields.add("id");
|
||||||
|
permissionFields.add("name");
|
||||||
|
permissionFields.add("description");
|
||||||
|
permissionFields.add("code");
|
||||||
|
permissionFields.add("groupType");
|
||||||
|
|
||||||
|
if (filterType.equals("I")) {
|
||||||
|
return new SimpleFilterProvider()
|
||||||
|
.addFilter(
|
||||||
|
filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(filterFields))
|
||||||
|
.addFilter(
|
||||||
|
"roleFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(roleFields))
|
||||||
|
.addFilter(
|
||||||
|
"permissionFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(permissionFields));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new SimpleFilterProvider()
|
||||||
|
.addFilter(
|
||||||
|
filterName,
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.serializeAllExcept(filterFields))
|
||||||
|
.addFilter(
|
||||||
|
"roleFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(roleFields))
|
||||||
|
.addFilter(
|
||||||
|
"permissionFilter",
|
||||||
|
SimpleBeanPropertyFilter
|
||||||
|
.filterOutAllExcept(permissionFields));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
|
||||||
private Boolean isTrue(String param) {
|
private Boolean isTrue(String param) {
|
||||||
if (param != null && param.equalsIgnoreCase("true"))
|
if (param != null && param.equalsIgnoreCase("true"))
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user