WIP audit views
This commit is contained in:
@@ -40,16 +40,20 @@ public class AuditApiResource {
|
||||
private final AuditReadPlatformService auditReadPlatformService;
|
||||
private final ApiRequestParameterHelper apiRequestParameterHelper;
|
||||
private final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer;
|
||||
private final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate;
|
||||
|
||||
@Autowired
|
||||
public AuditApiResource(final PlatformSecurityContext context,
|
||||
public AuditApiResource(
|
||||
final PlatformSecurityContext context,
|
||||
final AuditReadPlatformService auditReadPlatformService,
|
||||
final ApiRequestParameterHelper apiRequestParameterHelper,
|
||||
final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer) {
|
||||
final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer,
|
||||
final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate) {
|
||||
this.context = context;
|
||||
this.auditReadPlatformService = auditReadPlatformService;
|
||||
this.apiRequestParameterHelper = apiRequestParameterHelper;
|
||||
this.toApiJsonSerializer = toApiJsonSerializer;
|
||||
this.toApiJsonSerializerSearchTemplate = toApiJsonSerializerSearchTemplate;
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -58,12 +62,15 @@ public class AuditApiResource {
|
||||
public String retrieveAuditEntries(@Context final UriInfo uriInfo,
|
||||
@QueryParam("apiOperation") final String apiOperation,
|
||||
@QueryParam("resource") final String resource,
|
||||
@QueryParam("resourceId") final Long resourceId) {
|
||||
@QueryParam("resourceId") final Long resourceId,
|
||||
@QueryParam("makerId") final Long makerId,
|
||||
@QueryParam("checkerId") final Long checkerId) {
|
||||
|
||||
context.authenticatedUser().validateHasReadPermission(
|
||||
resourceNameForPermissions);
|
||||
|
||||
final String extraCriteria = getExtraCriteria(apiOperation, resource, resourceId);
|
||||
final String extraCriteria = getExtraCriteria(apiOperation, resource,
|
||||
resourceId, makerId, checkerId);
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
|
||||
.process(uriInfo.getQueryParameters());
|
||||
@@ -76,15 +83,15 @@ public class AuditApiResource {
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{auditId}")
|
||||
@Path("{auditId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveAuditEntry(@PathParam("auditId") final Long auditId, @Context final UriInfo uriInfo) {
|
||||
public String retrieveAuditEntry(@PathParam("auditId") final Long auditId,
|
||||
@Context final UriInfo uriInfo) {
|
||||
|
||||
context.authenticatedUser().validateHasReadPermission(
|
||||
resourceNameForPermissions);
|
||||
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
|
||||
.process(uriInfo.getQueryParameters());
|
||||
|
||||
@@ -94,28 +101,57 @@ public class AuditApiResource {
|
||||
return this.toApiJsonSerializer.serialize(settings, auditEntry,
|
||||
RESPONSE_DATA_PARAMETERS);
|
||||
}
|
||||
|
||||
|
||||
private String getExtraCriteria(String apiOperation, String resource, Long resourceId) {
|
||||
|
||||
String extraCriteria = "";
|
||||
@GET
|
||||
@Path("/searchtemplate")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveAuditSearchTemplate(@Context final UriInfo uriInfo) {
|
||||
|
||||
if (apiOperation != null) {
|
||||
extraCriteria += " and aud.api_operation like " + ApiParameterHelper.sqlEncodeString(apiOperation);
|
||||
}
|
||||
if (resource != null) {
|
||||
extraCriteria += " and aud.api_resource like " + ApiParameterHelper.sqlEncodeString(resource);
|
||||
}
|
||||
if (resourceId != null) {
|
||||
extraCriteria += " and aud.resource_id = " + resourceId;
|
||||
}
|
||||
context.authenticatedUser().validateHasReadPermission(
|
||||
resourceNameForPermissions);
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
|
||||
.process(uriInfo.getQueryParameters());
|
||||
|
||||
if (StringUtils.isNotBlank(extraCriteria)) {
|
||||
extraCriteria = extraCriteria.substring(4);
|
||||
}
|
||||
final AuditSearchData auditSearchData = this.auditReadPlatformService
|
||||
.retrieveSearchTemplate();
|
||||
|
||||
return extraCriteria;
|
||||
}
|
||||
final Set<String> RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE = new HashSet<String>(
|
||||
Arrays.asList("appUsers", "apiOperations", "resources"));
|
||||
|
||||
return this.toApiJsonSerializerSearchTemplate.serialize(settings, auditSearchData,
|
||||
RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE);
|
||||
}
|
||||
|
||||
private String getExtraCriteria(String apiOperation, String resource,
|
||||
Long resourceId, Long makerId, Long checkerId) {
|
||||
|
||||
String extraCriteria = "";
|
||||
|
||||
if (apiOperation != null) {
|
||||
extraCriteria += " and aud.api_operation like "
|
||||
+ ApiParameterHelper.sqlEncodeString(apiOperation);
|
||||
}
|
||||
if (resource != null) {
|
||||
extraCriteria += " and aud.api_resource like "
|
||||
+ ApiParameterHelper.sqlEncodeString(resource);
|
||||
}
|
||||
if (resourceId != null) {
|
||||
extraCriteria += " and aud.resource_id = " + resourceId;
|
||||
}
|
||||
if (makerId != null) {
|
||||
extraCriteria += " and aud.maker_id = " + makerId;
|
||||
}
|
||||
if (checkerId != null) {
|
||||
extraCriteria += " and aud.checker_id = " + checkerId;
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(extraCriteria)) {
|
||||
extraCriteria = extraCriteria.substring(4);
|
||||
}
|
||||
|
||||
return extraCriteria;
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -9,4 +9,6 @@ public interface AuditReadPlatformService {
|
||||
|
||||
AuditData retrieveAuditEntry(Long auditId);
|
||||
|
||||
AuditSearchData retrieveSearchTemplate();
|
||||
|
||||
}
|
||||
+16
-2
@@ -3,12 +3,15 @@ package org.mifosplatform.audit.api;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
|
||||
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
|
||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||
import org.mifosplatform.useradministration.data.AppUserLookup;
|
||||
import org.mifosplatform.useradministration.service.AppUserReadPlatformService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
@@ -19,12 +22,13 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final PlatformSecurityContext context;
|
||||
|
||||
private final AppUserReadPlatformService appUserReadPlatformService;
|
||||
@Autowired
|
||||
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context,
|
||||
final TenantAwareRoutingDataSource dataSource) {
|
||||
final TenantAwareRoutingDataSource dataSource, final AppUserReadPlatformService appUserReadPlatformService) {
|
||||
this.context = context;
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
this.appUserReadPlatformService = appUserReadPlatformService;
|
||||
}
|
||||
|
||||
private static final class AuditMapper implements RowMapper<AuditData> {
|
||||
@@ -70,6 +74,8 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<AuditData> retrieveAuditEntries(String extraCriteria,
|
||||
boolean includeJson) {
|
||||
@@ -97,4 +103,12 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
|
||||
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuditSearchData retrieveSearchTemplate() {
|
||||
Collection<AppUserLookup> appUsers = appUserReadPlatformService.retrieveSearchTemplate();
|
||||
List<String> apiOperations = null;
|
||||
List<String> resources = null;
|
||||
return new AuditSearchData(appUsers, apiOperations, resources);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.mifosplatform.audit.api;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.mifosplatform.useradministration.data.AppUserLookup;
|
||||
|
||||
/**
|
||||
* Immutable data object representing client data.
|
||||
*/
|
||||
final public class AuditSearchData {
|
||||
@SuppressWarnings("unused")
|
||||
private final Collection<AppUserLookup> appUsers;
|
||||
@SuppressWarnings("unused")
|
||||
private final List<String> apiOperations;
|
||||
@SuppressWarnings("unused")
|
||||
private final List<String> resources;
|
||||
|
||||
public AuditSearchData(final Collection<AppUserLookup> appUsers,
|
||||
final List<String> apiOperations, final List<String> resources) {
|
||||
|
||||
this.appUsers = appUsers;
|
||||
this.apiOperations = apiOperations;
|
||||
this.resources = resources;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.mifosplatform.useradministration.data;
|
||||
|
||||
/**
|
||||
* Immutable data object for application user data.
|
||||
*/
|
||||
public class AppUserLookup {
|
||||
|
||||
private final Long id;
|
||||
private final String username;
|
||||
|
||||
public AppUserLookup(final Long id, final String username) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
}
|
||||
+3
@@ -3,10 +3,13 @@ package org.mifosplatform.useradministration.service;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.mifosplatform.useradministration.data.AppUserData;
|
||||
import org.mifosplatform.useradministration.data.AppUserLookup;
|
||||
|
||||
public interface AppUserReadPlatformService {
|
||||
|
||||
Collection<AppUserData> retrieveAllUsers();
|
||||
|
||||
Collection<AppUserLookup> retrieveSearchTemplate();
|
||||
|
||||
AppUserData retrieveNewUserDetails();
|
||||
|
||||
|
||||
+73
-54
@@ -10,10 +10,10 @@ import java.util.Set;
|
||||
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
|
||||
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
|
||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||
import org.mifosplatform.organisation.office.data.OfficeData;
|
||||
import org.mifosplatform.organisation.office.data.OfficeLookup;
|
||||
import org.mifosplatform.organisation.office.service.OfficeReadPlatformService;
|
||||
import org.mifosplatform.useradministration.data.AppUserData;
|
||||
import org.mifosplatform.useradministration.data.AppUserLookup;
|
||||
import org.mifosplatform.useradministration.data.RoleData;
|
||||
import org.mifosplatform.useradministration.domain.AppUser;
|
||||
import org.mifosplatform.useradministration.domain.AppUserRepository;
|
||||
@@ -25,7 +25,8 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformService {
|
||||
public class AppUserReadPlatformServiceImpl implements
|
||||
AppUserReadPlatformService {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final PlatformSecurityContext context;
|
||||
@@ -34,8 +35,11 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
private final AppUserRepository appUserRepository;
|
||||
|
||||
@Autowired
|
||||
public AppUserReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource,
|
||||
final OfficeReadPlatformService officeReadPlatformService, final RoleReadPlatformService roleReadPlatformService,
|
||||
public AppUserReadPlatformServiceImpl(
|
||||
final PlatformSecurityContext context,
|
||||
final TenantAwareRoutingDataSource dataSource,
|
||||
final OfficeReadPlatformService officeReadPlatformService,
|
||||
final RoleReadPlatformService roleReadPlatformService,
|
||||
final AppUserRepository appUserRepository) {
|
||||
this.context = context;
|
||||
this.officeReadPlatformService = officeReadPlatformService;
|
||||
@@ -47,37 +51,38 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
@Override
|
||||
public Collection<AppUserData> retrieveAllUsers() {
|
||||
|
||||
context.authenticatedUser();
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
String hierarchy = currentUser.getOffice().getHierarchy();
|
||||
String hierarchySearchString = hierarchy + "%";
|
||||
|
||||
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
|
||||
String officeIdsList = generateOfficeIdInClause(offices);
|
||||
AppUserMapper mapper = new AppUserMapper();
|
||||
String sql = "select " + mapper.schema() ;
|
||||
|
||||
AppUserMapper mapper = new AppUserMapper(offices);
|
||||
String sql = "select " + mapper.schema()
|
||||
+ " where u.office_id in (" + officeIdsList + ") and u.is_deleted = 0 order by u.id";
|
||||
|
||||
return this.jdbcTemplate.query(sql, mapper, new Object[] {});
|
||||
return this.jdbcTemplate.query(sql, mapper,
|
||||
new Object[] { hierarchySearchString });
|
||||
}
|
||||
|
||||
private String generateOfficeIdInClause(List<OfficeData> offices) {
|
||||
String officeIdsList = "";
|
||||
for (int i = 0; i < offices.size(); i++) {
|
||||
Long id = offices.get(i).getId();
|
||||
if (i == 0) {
|
||||
officeIdsList = id.toString();
|
||||
} else {
|
||||
officeIdsList += "," + id.toString();
|
||||
}
|
||||
}
|
||||
return officeIdsList;
|
||||
|
||||
@Override
|
||||
public Collection<AppUserLookup> retrieveSearchTemplate() {
|
||||
AppUser currentUser = context.authenticatedUser();
|
||||
String hierarchy = currentUser.getOffice().getHierarchy();
|
||||
String hierarchySearchString = hierarchy + "%";
|
||||
|
||||
AppUserLookupMapper mapper = new AppUserLookupMapper();
|
||||
String sql = "select " + mapper.schema();
|
||||
|
||||
return this.jdbcTemplate.query(sql, mapper,
|
||||
new Object[] { hierarchySearchString });
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppUserData retrieveNewUserDetails() {
|
||||
|
||||
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());
|
||||
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(
|
||||
officeReadPlatformService.retrieveAllOfficesForLookup());
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(
|
||||
this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
AppUserData userData = new AppUserData(offices, availableRoles);
|
||||
|
||||
@@ -94,8 +99,9 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
throw new UserNotFoundException(userId);
|
||||
}
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
List<RoleData> availableRoles = new ArrayList<RoleData>(
|
||||
this.roleReadPlatformService.retrieveAllRoles());
|
||||
|
||||
List<RoleData> selectedUserRoles = new ArrayList<RoleData>();
|
||||
Set<Role> userRoles = user.getRoles();
|
||||
for (Role role : userRoles) {
|
||||
@@ -103,24 +109,20 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
}
|
||||
|
||||
availableRoles.removeAll(selectedUserRoles);
|
||||
|
||||
|
||||
AppUserData userData = new AppUserData(user.getId(),
|
||||
user.getUsername(), user.getEmail(), user.getOffice().getId(), user.getOffice()
|
||||
.getName(), user.getFirstname(), user.getLastname(), availableRoles, selectedUserRoles);
|
||||
user.getUsername(), user.getEmail(), user.getOffice().getId(),
|
||||
user.getOffice().getName(), user.getFirstname(),
|
||||
user.getLastname(), availableRoles, selectedUserRoles);
|
||||
|
||||
return userData;
|
||||
}
|
||||
|
||||
|
||||
private static final class AppUserMapper implements RowMapper<AppUserData> {
|
||||
|
||||
private final List<OfficeData> offices;
|
||||
|
||||
public AppUserMapper(final List<OfficeData> offices) {
|
||||
this.offices = offices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppUserData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum)
|
||||
public AppUserData mapRow(final ResultSet rs,
|
||||
@SuppressWarnings("unused") final int rowNum)
|
||||
throws SQLException {
|
||||
|
||||
Long id = rs.getLong("id");
|
||||
@@ -129,28 +131,45 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
|
||||
String lastname = rs.getString("lastname");
|
||||
String email = rs.getString("email");
|
||||
Long officeId = JdbcSupport.getLong(rs, "officeId");
|
||||
|
||||
// FIXME - change sql query to join to get office id and name information.
|
||||
String officeName = fromOfficeList(this.offices, officeId);
|
||||
|
||||
AppUserData user = new AppUserData(id, username, email, officeId, officeName, firstname, lastname, new ArrayList<RoleData>(), new ArrayList<RoleData>());
|
||||
String officeName = rs.getString("officeName");
|
||||
|
||||
AppUserData user = new AppUserData(id, username, email, officeId,
|
||||
officeName, firstname, lastname, new ArrayList<RoleData>(),
|
||||
new ArrayList<RoleData>());
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public String schema() {
|
||||
return " u.id as id, u.username as username, u.firstname as firstname, u.lastname as lastname, u.email as email, u.office_id as officeId from m_appuser u ";
|
||||
return " u.id as id, u.username as username, u.firstname as firstname, u.lastname as lastname, u.email as email,"
|
||||
+ " u.office_id as officeId, o.name as officeName from m_appuser u "
|
||||
+ " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=0 order by u.username";
|
||||
}
|
||||
|
||||
private String fromOfficeList(final List<OfficeData> officeList, final Long officeId) {
|
||||
String match = "";
|
||||
for (OfficeData office : officeList) {
|
||||
if (office.getId().equals(officeId)) {
|
||||
match = office.getName();
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AppUserLookupMapper implements
|
||||
RowMapper<AppUserLookup> {
|
||||
|
||||
@Override
|
||||
public AppUserLookup mapRow(final ResultSet rs,
|
||||
@SuppressWarnings("unused") final int rowNum)
|
||||
throws SQLException {
|
||||
|
||||
Long id = rs.getLong("id");
|
||||
String username = rs.getString("username");
|
||||
|
||||
AppUserLookup user = new AppUserLookup(id, username);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public String schema() {
|
||||
return " u.id as id, u.username as username from m_appuser u "
|
||||
+ " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=0 order by u.username";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user