mifosx-270 api docs audit/makerchecker/permissions + wip mifosx-97 make functionality match api docs

This commit is contained in:
John Woodlock
2013-04-05 02:09:04 +01:00
parent 0ade90b70c
commit d98360dd39
8 changed files with 725 additions and 264 deletions
@@ -34,7 +34,6 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.exception.UnrecognizedQueryParamException;
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -50,9 +49,10 @@ public class MakercheckersApiResource {
"checkedOnDate", "processingResult", "commandAsJson",
"officeName", "groupLevelName", "groupName", "clientName",
"loanAccountNo", "savingsAccountNo"));
private final String resourceNameForPermissions = "MAKERCHECKER";
//Next line removed because read permissions checks part of read services
//private final String resourceNameForPermissions = "MAKERCHECKER";
private final PlatformSecurityContext context;
private final AuditReadPlatformService readPlatformService;
private final DefaultToApiJsonSerializer<CommandSourceData> toApiJsonSerializer;
private final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializerAudit;
@@ -62,14 +62,12 @@ public class MakercheckersApiResource {
@Autowired
public MakercheckersApiResource(
final PlatformSecurityContext context,
final AuditReadPlatformService readPlatformService,
final DefaultToApiJsonSerializer<CommandSourceData> toApiJsonSerializer,
final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializerAudit,
final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate,
final ApiRequestParameterHelper apiRequestParameterHelper,
final PortfolioCommandSourceWritePlatformService writePlatformService) {
this.context = context;
this.readPlatformService = readPlatformService;
this.toApiJsonSerializer = toApiJsonSerializer;
this.apiRequestParameterHelper = apiRequestParameterHelper;
@@ -94,9 +92,6 @@ public class MakercheckersApiResource {
@QueryParam("loanid") final Integer loanId,
@QueryParam("savingsAccountId") final Integer savingsAccountId) {
context.authenticatedUser().validateHasReadPermission(
resourceNameForPermissions);
final String extraCriteria = getExtraCriteria(actionName, entityName,
resourceId, makerId, makerDateTimeFrom, makerDateTimeTo,
officeId, groupId, clientId, loanId, savingsAccountId);
@@ -118,9 +113,6 @@ public class MakercheckersApiResource {
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAuditSearchTemplate(@Context final UriInfo uriInfo) {
context.authenticatedUser().validateHasReadPermission(
resourceNameForPermissions);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
.process(uriInfo.getQueryParameters());
@@ -25,159 +25,175 @@ import org.springframework.data.jpa.domain.AbstractPersistable;
@Table(name = "m_portfolio_command_source")
public class CommandSource extends AbstractPersistable<Long> {
@Column(name = "action_name", nullable = true, length = 100)
private String actionName;
@Column(name = "entity_name", nullable = true, length = 100)
private String entityName;
@SuppressWarnings("unused")
@Column(name = "office_id")
private Long officeId;
@Column(name = "action_name", nullable = true, length = 100)
private String actionName;
@SuppressWarnings("unused")
@Column(name = "group_id")
private Long groupId;
@SuppressWarnings("unused")
@Column(name = "client_id")
private Long clientId;
@Column(name = "entity_name", nullable = true, length = 100)
private String entityName;
@SuppressWarnings("unused")
@Column(name = "loan_id")
private Long loanId;
@SuppressWarnings("unused")
@Column(name = "savings_account_id")
private Long savingsId;
@Column(name = "api_get_url", length = 100)
private String resourceGetUrl;
@SuppressWarnings("unused")
@Column(name = "office_id")
private Long officeId;
@Column(name = "resource_id")
private Long resourceId;
@Column(name = "subresource_id")
private Long subresourceId;
@SuppressWarnings("unused")
@Column(name = "group_id")
private Long groupId;
@Column(name = "command_as_json", length = 1000)
private String commandAsJson;
@SuppressWarnings("unused")
@Column(name = "client_id")
private Long clientId;
@SuppressWarnings("unused")
@ManyToOne
@JoinColumn(name = "maker_id", nullable = false)
private AppUser maker;
@SuppressWarnings("unused")
@Column(name = "loan_id")
private Long loanId;
@SuppressWarnings("unused")
@Column(name = "made_on_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date madeOnDate;
@SuppressWarnings("unused")
@Column(name = "savings_account_id")
private Long savingsId;
@SuppressWarnings("unused")
@ManyToOne
@JoinColumn(name = "checker_id", nullable = true)
private AppUser checker;
@Column(name = "api_get_url", length = 100)
private String resourceGetUrl;
@SuppressWarnings("unused")
@Column(name = "checked_on_date", nullable = true)
@Temporal(TemporalType.TIMESTAMP)
private Date checkedOnDate;
@Column(name = "resource_id")
private Long resourceId;
@SuppressWarnings("unused")
@Column(name = "processing_result_enum", nullable = false)
private Integer processingResult;
@Column(name = "subresource_id")
private Long subresourceId;
public static CommandSource fullEntryFrom(final CommandWrapper wrapper, final JsonCommand command, final AppUser maker) {
return new CommandSource(wrapper.actionName(), wrapper.entityName(), wrapper.getHref(), command.entityId(), command.subentityId(), command.json(), maker, DateTime.now());
}
@Column(name = "command_as_json", length = 1000)
private String commandAsJson;
protected CommandSource() {
//
}
@SuppressWarnings("unused")
@ManyToOne
@JoinColumn(name = "maker_id", nullable = false)
private AppUser maker;
private CommandSource(
final String actionName, final String entityName, final String href,
final Long resourceId, final Long subresourceId,
final String commandSerializedAsJson, final AppUser maker,
final DateTime madeOnDateTime) {
this.actionName = actionName;
this.entityName = entityName;
this.resourceGetUrl = href;
this.resourceId = resourceId;
this.subresourceId = subresourceId;
this.commandAsJson = commandSerializedAsJson;
this.maker = maker;
this.madeOnDate = madeOnDateTime.toDate();
this.processingResult = CommandProcessingResultType.PROCESSED.getValue();
}
@SuppressWarnings("unused")
@Column(name = "made_on_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date madeOnDate;
public void markAsChecked(final AppUser checker, final DateTime checkedOnDate) {
this.checker = checker;
this.checkedOnDate = checkedOnDate.toDate();
this.processingResult = CommandProcessingResultType.PROCESSED.getValue();
}
@SuppressWarnings("unused")
@ManyToOne
@JoinColumn(name = "checker_id", nullable = true)
private AppUser checker;
public void updateResourceId(final Long resourceId) {
this.resourceId = resourceId;
}
public void updateSubresourceId(final Long subresourceId) {
this.subresourceId = subresourceId;
}
@SuppressWarnings("unused")
@Column(name = "checked_on_date", nullable = true)
@Temporal(TemporalType.TIMESTAMP)
private Date checkedOnDate;
public void updateJsonTo(final String json) {
this.commandAsJson = json;
}
@SuppressWarnings("unused")
@Column(name = "processing_result_enum", nullable = false)
private Integer processingResult;
public Long resourceId() {
return this.resourceId;
}
public Long subresourceId() {
return this.subresourceId;
}
public static CommandSource fullEntryFrom(final CommandWrapper wrapper,
final JsonCommand command, final AppUser maker) {
return new CommandSource(wrapper.actionName(), wrapper.entityName(),
wrapper.getHref(), command.entityId(), command.subentityId(),
command.json(), maker, DateTime.now());
}
public boolean hasJson() {
return StringUtils.isNotBlank(this.commandAsJson);
}
protected CommandSource() {
//
}
public String json() {
return this.commandAsJson;
}
private CommandSource(final String actionName, final String entityName,
final String href, final Long resourceId, final Long subresourceId,
final String commandSerializedAsJson, final AppUser maker,
final DateTime madeOnDateTime) {
this.actionName = actionName;
this.entityName = entityName;
this.resourceGetUrl = href;
this.resourceId = resourceId;
this.subresourceId = subresourceId;
this.commandAsJson = commandSerializedAsJson;
this.maker = maker;
this.madeOnDate = madeOnDateTime.toDate();
this.processingResult = CommandProcessingResultType.PROCESSED
.getValue();
}
public String getActionName() {
return this.actionName;
}
public String getEntityName() {
return this.entityName;
}
public void markAsChecked(final AppUser checker,
final DateTime checkedOnDate) {
this.checker = checker;
this.checkedOnDate = checkedOnDate.toDate();
this.processingResult = CommandProcessingResultType.PROCESSED
.getValue();
}
public String getPermissionCode() {
return this.actionName + "_" + this.entityName;
}
public void updateResourceId(final Long resourceId) {
this.resourceId = resourceId;
}
public Long getResourceId() {
return this.resourceId;
}
public Long getSubresourceId() {
return this.subresourceId;
}
public void updateSubresourceId(final Long subresourceId) {
this.subresourceId = subresourceId;
}
public void markAsAwaitingApproval() {
this.processingResult = CommandProcessingResultType.AWAITING_APPROVAL.getValue();
}
public void updateJsonTo(final String json) {
this.commandAsJson = json;
}
public void updateForAudit(final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId) {
this.officeId = officeId;
this.groupId = groupId;
this.clientId = clientId;
this.loanId = loanId;
this.savingsId = savingsId;
}
public Long resourceId() {
return this.resourceId;
}
public String getResourceGetUrl() {
return this.resourceGetUrl;
}
public Long subresourceId() {
return this.subresourceId;
}
public boolean hasJson() {
return StringUtils.isNotBlank(this.commandAsJson);
}
public String json() {
return this.commandAsJson;
}
public String getActionName() {
return this.actionName;
}
public String getEntityName() {
return this.entityName;
}
public String getPermissionCode() {
return this.actionName + "_" + this.entityName;
}
public Long getResourceId() {
return this.resourceId;
}
public Long getSubresourceId() {
return this.subresourceId;
}
public void markAsAwaitingApproval() {
this.processingResult = CommandProcessingResultType.AWAITING_APPROVAL
.getValue();
}
public boolean isMarkedAsAwaitingApproval() {
if (this.processingResult
.equals(CommandProcessingResultType.AWAITING_APPROVAL
.getValue()))
return true;
return false;
}
public void updateForAudit(final Long officeId, final Long groupId,
final Long clientId, final Long loanId, final Long savingsId) {
this.officeId = officeId;
this.groupId = groupId;
this.clientId = clientId;
this.loanId = loanId;
this.savingsId = savingsId;
}
public String getResourceGetUrl() {
return this.resourceGetUrl;
}
}
@@ -0,0 +1,18 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.commands.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
/**
* A {@link RuntimeException} thrown when client resources are not found.
*/
public class CommandNotAwaitingApprovalException extends AbstractPlatformResourceNotFoundException {
public CommandNotAwaitingApprovalException(Long id) {
super("error.msg.command.id.not.awaiting.approval", "Audit with identifier " + id + " is Not Awaiting Approval", id);
}
}
@@ -13,6 +13,6 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN
public class CommandNotFoundException extends AbstractPlatformResourceNotFoundException {
public CommandNotFoundException(Long id) {
super("error.msg.command.id.invalid", "Command with identifier " + id + " does not exist", id);
super("error.msg.command.id.invalid", "Audit with identifier " + id + " does not exist", id);
}
}
@@ -8,12 +8,13 @@ package org.mifosplatform.commands.service;
import org.mifosplatform.commands.domain.CommandSource;
import org.mifosplatform.commands.domain.CommandSourceRepository;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.commands.exception.CommandNotAwaitingApprovalException;
import org.mifosplatform.commands.exception.CommandNotFoundException;
import org.mifosplatform.commands.exception.RollbackTransactionAsCommandIsNotApprovedByCheckerException;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.client.exception.ClientNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -21,87 +22,120 @@ import org.springframework.transaction.annotation.Transactional;
import com.google.gson.JsonElement;
@Service
public class PortfolioCommandSourceWritePlatformServiceImpl implements PortfolioCommandSourceWritePlatformService {
public class PortfolioCommandSourceWritePlatformServiceImpl implements
PortfolioCommandSourceWritePlatformService {
private final PlatformSecurityContext context;
private final CommandSourceRepository commandSourceRepository;
private final FromJsonHelper fromApiJsonHelper;
private final CommandProcessingService processAndLogCommandService;
private final PlatformSecurityContext context;
private final CommandSourceRepository commandSourceRepository;
private final FromJsonHelper fromApiJsonHelper;
private final CommandProcessingService processAndLogCommandService;
@Autowired
public PortfolioCommandSourceWritePlatformServiceImpl(final PlatformSecurityContext context,
final CommandSourceRepository commandSourceRepository, final FromJsonHelper fromApiJsonHelper,
final CommandProcessingService processAndLogCommandService) {
this.context = context;
this.commandSourceRepository = commandSourceRepository;
this.fromApiJsonHelper = fromApiJsonHelper;
this.processAndLogCommandService = processAndLogCommandService;
}
@Autowired
public PortfolioCommandSourceWritePlatformServiceImpl(
final PlatformSecurityContext context,
final CommandSourceRepository commandSourceRepository,
final FromJsonHelper fromApiJsonHelper,
final CommandProcessingService processAndLogCommandService) {
this.context = context;
this.commandSourceRepository = commandSourceRepository;
this.fromApiJsonHelper = fromApiJsonHelper;
this.processAndLogCommandService = processAndLogCommandService;
}
@Override
public CommandProcessingResult logCommandSource(final CommandWrapper wrapper) {
@Override
public CommandProcessingResult logCommandSource(final CommandWrapper wrapper) {
boolean isApprovedByChecker = false;
// check if is update of own account details
if (wrapper.isUpdateOfOwnUserDetails(context.authenticatedUser().getId())) {
// then allow this operation to proceed.
// maker checker doesnt mean anything here.
isApprovedByChecker = true; // set to true in case permissions have
// been maker-checker enabled by
// accident.
} else {
// if not user changing their own details - check user has
// permission to perform specific task.
context.authenticatedUser().validateHasPermissionTo(wrapper.getTaskPermissionName());
}
boolean isApprovedByChecker = false;
// check if is update of own account details
if (wrapper.isUpdateOfOwnUserDetails(context.authenticatedUser()
.getId())) {
// then allow this operation to proceed.
// maker checker doesnt mean anything here.
isApprovedByChecker = true; // set to true in case permissions have
// been maker-checker enabled by
// accident.
} else {
// if not user changing their own details - check user has
// permission to perform specific task.
context.authenticatedUser().validateHasPermissionTo(
wrapper.getTaskPermissionName());
}
final String json = wrapper.getJson();
CommandProcessingResult result = null;
try {
final JsonElement parsedCommand = this.fromApiJsonHelper.parse(json);
final JsonCommand command = JsonCommand.from(json, parsedCommand, this.fromApiJsonHelper, wrapper.getEntityName(),
wrapper.getEntityId(), wrapper.getSubentityId(), wrapper.getGroupId(), wrapper.getClientId(), wrapper.getLoanId(),
wrapper.getSavingsId(), wrapper.getCodeId(), wrapper.getSupportedEntityType(), wrapper.getSupportedEntityId(),
wrapper.getTransactionId());
final String json = wrapper.getJson();
CommandProcessingResult result = null;
try {
final JsonElement parsedCommand = this.fromApiJsonHelper
.parse(json);
final JsonCommand command = JsonCommand.from(json, parsedCommand,
this.fromApiJsonHelper, wrapper.getEntityName(),
wrapper.getEntityId(), wrapper.getSubentityId(),
wrapper.getGroupId(), wrapper.getClientId(),
wrapper.getLoanId(), wrapper.getSavingsId(),
wrapper.getCodeId(), wrapper.getSupportedEntityType(),
wrapper.getSupportedEntityId(), wrapper.getTransactionId());
result = this.processAndLogCommandService.processAndLogCommand(wrapper, command, isApprovedByChecker);
} catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) {
result = this.processAndLogCommandService.processAndLogCommand(
wrapper, command, isApprovedByChecker);
} catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) {
result = this.processAndLogCommandService.logCommand(e.getCommandSourceResult());
}
result = this.processAndLogCommandService.logCommand(e
.getCommandSourceResult());
}
return result;
}
return result;
}
@Override
public CommandProcessingResult approveEntry(final Long commandId) {
@Override
public CommandProcessingResult approveEntry(final Long makerCheckerId) {
final CommandSource commandSourceInput = this.commandSourceRepository.findOne(commandId);
if (commandSourceInput == null) {throw new ClientNotFoundException(commandId);}
context.authenticatedUser().validateHasCheckerPermissionTo(commandSourceInput.getPermissionCode());
CommandSource commandSourceInput = validateMakerCheckerTransaction(makerCheckerId);
final CommandWrapper wrapper = CommandWrapper.fromExistingCommand(commandId, commandSourceInput.getActionName(),
commandSourceInput.getEntityName(), commandSourceInput.resourceId(), commandSourceInput.subresourceId(),
commandSourceInput.getResourceGetUrl());
final CommandWrapper wrapper = CommandWrapper.fromExistingCommand(
makerCheckerId, commandSourceInput.getActionName(),
commandSourceInput.getEntityName(),
commandSourceInput.resourceId(),
commandSourceInput.subresourceId(),
commandSourceInput.getResourceGetUrl());
final JsonElement parsedCommand = this.fromApiJsonHelper.parse(commandSourceInput.json());
final JsonCommand command = JsonCommand.fromExistingCommand(commandId, commandSourceInput.json(), parsedCommand,
this.fromApiJsonHelper, commandSourceInput.getEntityName(), commandSourceInput.resourceId(),
commandSourceInput.subresourceId());
final JsonElement parsedCommand = this.fromApiJsonHelper
.parse(commandSourceInput.json());
final JsonCommand command = JsonCommand.fromExistingCommand(
makerCheckerId, commandSourceInput.json(), parsedCommand,
this.fromApiJsonHelper, commandSourceInput.getEntityName(),
commandSourceInput.resourceId(),
commandSourceInput.subresourceId());
final boolean makerCheckerApproval = true;
return this.processAndLogCommandService.processAndLogCommand(wrapper, command, makerCheckerApproval);
}
final boolean makerCheckerApproval = true;
return this.processAndLogCommandService.processAndLogCommand(wrapper,
command, makerCheckerApproval);
}
@Transactional
@Override
public Long deleteEntry(final Long makerCheckerId) {
@Transactional
@Override
public Long deleteEntry(final Long makerCheckerId) {
context.authenticatedUser();
validateMakerCheckerTransaction(makerCheckerId);
this.commandSourceRepository.delete(makerCheckerId);
this.commandSourceRepository.delete(makerCheckerId);
return makerCheckerId;
}
return makerCheckerId;
}
private CommandSource validateMakerCheckerTransaction(
final Long makerCheckerId) {
final CommandSource commandSourceInput = this.commandSourceRepository
.findOne(makerCheckerId);
if (commandSourceInput == null) {
throw new CommandNotFoundException(makerCheckerId);
}
if (!(commandSourceInput.isMarkedAsAwaitingApproval())) {
throw new CommandNotAwaitingApprovalException(makerCheckerId);
}
context.authenticatedUser().validateHasCheckerPermissionTo(
commandSourceInput.getPermissionCode());
return commandSourceInput;
}
}
@@ -441,7 +441,7 @@ public class AppUser extends AbstractPersistable<Long> implements PlatformUser {
public void validateHasCheckerPermissionTo(final String function) {
final String checkerPermissionName = function.toUpperCase() + "_CHECKER";
if (hasNotPermissionTo(checkerPermissionName)) {
if (hasNotPermissionTo("CHECKER_SUPER_USER") && hasNotPermissionTo(checkerPermissionName)) {
final String authorizationMessage = "User has no authority to be a checker for: " + function;
throw new NoAuthorizationException(authorizationMessage);
}