MIFOSX-929 - added check for multiple permission for client

This commit is contained in:
Pramod Nuthakki
2014-02-26 20:10:35 +05:30
parent fd32d997e7
commit 85297aaa65
7 changed files with 73 additions and 20 deletions
@@ -9,11 +9,14 @@ import org.mifosplatform.commands.domain.CommandSource;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.useradministration.domain.AppUser;
public interface CommandProcessingService {
CommandProcessingResult processAndLogCommand(CommandWrapper wrapper, JsonCommand command, boolean isApprovedByChecker);
CommandProcessingResult logCommand(CommandSource commandSourceResult);
boolean validateCommand(final CommandWrapper commandWrapper,final AppUser user);
}
@@ -53,8 +53,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
public CommandProcessingResult processAndLogCommand(final CommandWrapper wrapper, final JsonCommand command,
final boolean isApprovedByChecker) {
final boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName())
&& !isApprovedByChecker;
final boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName());
final NewCommandSourceHandler handler = findCommandHandler(wrapper);
final CommandProcessingResult result = handler.processCommand(command);
@@ -86,7 +85,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
this.commandSourceRepository.save(commandSourceResult);
}
if (rollbackTransaction) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); }
if ((rollbackTransaction || result.isRollbackTransaction()) && !isApprovedByChecker) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); }
return result;
}
@@ -627,4 +626,11 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
return handler;
}
@Override
public boolean validateCommand(final CommandWrapper commandWrapper, final AppUser user) {
boolean rollbackTransaction = this.configurationDomainService.isMakerCheckerEnabledForTask(commandWrapper.taskPermissionName());
user.validateHasPermissionTo(commandWrapper.getTaskPermissionName());
return rollbackTransaction;
}
}
@@ -26,12 +26,13 @@ public class CommandProcessingResult {
@SuppressWarnings("unused")
private final String resourceIdentifier;
private final Long productId;
private Boolean rollbackTransaction;
public static CommandProcessingResult fromDetails(final Long commandId, final Long officeId, final Long groupId, final Long clientId,
final Long loanId, final Long savingsId, final String resourceIdentifier, final Long entityId, final String transactionId,
final Map<String, Object> changes, final Long productId) {
final Map<String, Object> changes, final Long productId,final Boolean rollbackTransaction) {
return new CommandProcessingResult(commandId, officeId, groupId, clientId, loanId, savingsId, resourceIdentifier, entityId,
transactionId, changes, productId);
transactionId, changes, productId,rollbackTransaction);
}
public static CommandProcessingResult commandOnlyResult(final Long commandId) {
@@ -85,7 +86,7 @@ public class CommandProcessingResult {
private CommandProcessingResult(final Long commandId, final Long officeId, final Long groupId, final Long clientId, final Long loanId,
final Long savingsId, final String resourceIdentifier, final Long resourceId, final String transactionId,
final Map<String, Object> changesOnly, final Long productId) {
final Map<String, Object> changesOnly, final Long productId, Boolean rollbackTransaction) {
this.commandId = commandId;
this.officeId = officeId;
this.groupId = groupId;
@@ -97,6 +98,7 @@ public class CommandProcessingResult {
this.changes = changesOnly;
this.transactionId = transactionId;
this.productId = productId;
this.rollbackTransaction = rollbackTransaction;
}
private CommandProcessingResult(final Long resourceId, final Long officeId, final Long commandId, final Map<String, Object> changesOnly) {
@@ -169,4 +171,14 @@ public class CommandProcessingResult {
public Long getProductId() {
return this.productId;
}
public boolean isRollbackTransaction() {
return this.rollbackTransaction!=null && this.rollbackTransaction;
}
public void setRollbackTransaction(boolean rollbackTransaction) {
this.rollbackTransaction = rollbackTransaction;
}
}
@@ -24,10 +24,11 @@ public class CommandProcessingResultBuilder {
private String transactionId;
private Map<String, Object> changes;
private Long productId;
private boolean rollbackTransaction = false;
public CommandProcessingResult build() {
return CommandProcessingResult.fromDetails(this.commandId, this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId,
this.resourceIdentifier, this.entityId, this.transactionId, this.changes, this.productId);
this.resourceIdentifier, this.entityId, this.transactionId, this.changes, this.productId,this.rollbackTransaction);
}
public CommandProcessingResultBuilder withCommandId(final Long withCommandId) {
@@ -84,5 +85,10 @@ public class CommandProcessingResultBuilder {
this.productId = productId;
return this;
}
public CommandProcessingResultBuilder setRollbackTransaction(final boolean rollbackTransaction) {
this.rollbackTransaction = this.rollbackTransaction || rollbackTransaction;
return this;
}
}
@@ -13,6 +13,9 @@ import java.util.Map;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.commands.service.CommandProcessingService;
import org.mifosplatform.commands.service.CommandWrapperBuilder;
import org.mifosplatform.infrastructure.codes.domain.CodeValue;
import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
@@ -77,6 +80,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
private final SavingsAccountRepository savingsRepository;
private final SavingsProductRepository savingsProductRepository;
private final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService;
private final CommandProcessingService commandProcessingService;
@Autowired
public ClientWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
@@ -85,7 +89,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final GroupRepository groupRepository, final StaffRepositoryWrapper staffRepository,
final CodeValueRepositoryWrapper codeValueRepository, final LoanRepository loanRepository,
final SavingsAccountRepository savingsRepository,final SavingsProductRepository savingsProductRepository,
final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService) {
final SavingsApplicationProcessWritePlatformService savingsApplicationProcessWritePlatformService,
final CommandProcessingService commandProcessingService) {
this.context = context;
this.clientRepository = clientRepository;
this.officeRepository = officeRepository;
@@ -99,6 +104,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
this.savingsRepository = savingsRepository;
this.savingsProductRepository = savingsProductRepository;
this.savingsApplicationProcessWritePlatformService = savingsApplicationProcessWritePlatformService;
this.commandProcessingService = commandProcessingService;
}
@Transactional
@@ -185,6 +191,11 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
}
final Client newClient = Client.createNew(currentUser,clientOffice, clientParentGroup, staff, savingsProduct, command);
boolean rollbackTransaction = false;
if(newClient.isActive()){
final CommandWrapper commandWrapper = new CommandWrapperBuilder().activateClient(null).build();
rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, currentUser);
}
this.clientRepository.save(newClient);
@@ -197,7 +208,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final Locale locale = command.extractLocale();
final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
Long savingsId = openOverdraftSavingsAccount(newClient, fmt);
CommandProcessingResult result = openOverdraftSavingsAccount(newClient, fmt);
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
@@ -205,7 +216,9 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
.withClientId(newClient.getId()) //
.withGroupId(groupId) //
.withEntityId(newClient.getId()) //
.withSavingsId(savingsId)//
.withSavingsId(result.getSavingsId())//
.setRollbackTransaction(rollbackTransaction)//
.setRollbackTransaction(result.isRollbackTransaction())//
.build();
} catch (final DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve);
@@ -284,7 +297,7 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final AppUser currentUser = this.context.authenticatedUser();
client.activate(currentUser, fmt, activationDate);
Long savingsId = openOverdraftSavingsAccount(client, fmt);
CommandProcessingResult result = openOverdraftSavingsAccount(client, fmt);
this.clientRepository.saveAndFlush(client);
return new CommandProcessingResultBuilder() //
@@ -292,7 +305,8 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
.withOfficeId(client.officeId()) //
.withClientId(clientId) //
.withEntityId(clientId) //
.withSavingsId(savingsId)//
.withSavingsId(result.getSavingsId())//
.setRollbackTransaction(result.isRollbackTransaction())//
.build();
} catch (final DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve);
@@ -300,17 +314,17 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
}
}
private Long openOverdraftSavingsAccount(final Client client, final DateTimeFormatter fmt) {
Long accountId = null;
private CommandProcessingResult openOverdraftSavingsAccount(final Client client, final DateTimeFormatter fmt) {
CommandProcessingResult commandProcessingResult = CommandProcessingResult.empty();
if(client.isActive() && client.SavingsProduct() != null){
if(!client.SavingsProduct().isAllowOverdraft()){
String defaultUserMessage = "Client's saving account must be a overdraft account";
throw new InvalidClientSavingProductException("saving.product", "must.be.ovrdraft.account", defaultUserMessage, client.SavingsProduct().getId(),client.getId());
}
SavingsAccountDataDTO savingsAccountDataDTO = new SavingsAccountDataDTO(client, null, client.SavingsProduct(), client.getActivationLocalDate(), client.activatedBy(), fmt);
accountId = this.savingsApplicationProcessWritePlatformService.createActiveApplication(savingsAccountDataDTO);
commandProcessingResult = this.savingsApplicationProcessWritePlatformService.createActiveApplication(savingsAccountDataDTO);
}
return accountId;
return commandProcessingResult;
}
private void logAsErrorUnexpectedDataIntegrityException(final DataIntegrityViolationException dve) {
@@ -25,5 +25,5 @@ public interface SavingsApplicationProcessWritePlatformService {
CommandProcessingResult applicantWithdrawsFromApplication(Long savingsId, JsonCommand command);
Long createActiveApplication(SavingsAccountDataDTO savingsAccountDataDTO);
CommandProcessingResult createActiveApplication(SavingsAccountDataDTO savingsAccountDataDTO);
}
@@ -16,6 +16,9 @@ import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.joda.time.format.DateTimeFormatter;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.commands.service.CommandProcessingService;
import org.mifosplatform.commands.service.CommandWrapperBuilder;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
@@ -75,6 +78,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
private final StaffRepositoryWrapper staffRepository;
private final SavingsAccountApplicationTransitionApiJsonValidator savingsAccountApplicationTransitionApiJsonValidator;
private final SavingsAccountChargeAssembler savingsAccountChargeAssembler;
private final CommandProcessingService commandProcessingService;
@Autowired
public SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
@@ -84,7 +88,8 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
final GroupRepository groupRepository, final SavingsProductRepository savingsProductRepository,
final NoteRepository noteRepository, final StaffRepositoryWrapper staffRepository,
final SavingsAccountApplicationTransitionApiJsonValidator savingsAccountApplicationTransitionApiJsonValidator,
final SavingsAccountChargeAssembler savingsAccountChargeAssembler) {
final SavingsAccountChargeAssembler savingsAccountChargeAssembler,
final CommandProcessingService commandProcessingService) {
this.context = context;
this.savingAccountRepository = savingAccountRepository;
this.savingAccountAssembler = savingAccountAssembler;
@@ -97,6 +102,7 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
this.staffRepository = staffRepository;
this.savingsAccountApplicationTransitionApiJsonValidator = savingsAccountApplicationTransitionApiJsonValidator;
this.savingsAccountChargeAssembler = savingsAccountChargeAssembler;
this.commandProcessingService = commandProcessingService;
}
/*
@@ -433,7 +439,10 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
}
@Override
public Long createActiveApplication(final SavingsAccountDataDTO savingsAccountDataDTO) {
public CommandProcessingResult createActiveApplication(final SavingsAccountDataDTO savingsAccountDataDTO) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().savingsAccountActivation(null).build();
boolean rollbackTransaction = this.commandProcessingService.validateCommand(commandWrapper, savingsAccountDataDTO.getAppliedBy());
final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsAccountDataDTO.getClient(),savingsAccountDataDTO.getGroup(),
savingsAccountDataDTO.getSavingsProduct(),savingsAccountDataDTO.getApplicationDate(),savingsAccountDataDTO.getAppliedBy());
@@ -446,6 +455,9 @@ public class SavingsApplicationProcessWritePlatformServiceJpaRepositoryImpl impl
account.updateAccountNo(accountNoGenerator.generate());
this.savingAccountRepository.save(account);
}
return account.getId();
return new CommandProcessingResultBuilder() //
.withSavingsId(account.getId()) //
.setRollbackTransaction(rollbackTransaction)//
.build();
}
}