From 1a32dd1944cd6de5b9d76116b406dd9b127e1e85 Mon Sep 17 00:00:00 2001 From: narwhalsilent <110954060+narwhalsilent@users.noreply.github.com> Date: Thu, 4 Apr 2024 16:27:40 +0800 Subject: [PATCH] Implement defensive command access - On person tab: No access to loan commands based on loan index. - On view all loans tab: No access to person commands based on person index. - On view loan of a person tab: Can only modify the person whose loan is being shown - On analytics tab: No access to loan and person commands based on index. --- src/main/java/seedu/address/MainApp.java | 1 + .../java/seedu/address/logic/commands/DeleteCommand.java | 1 + .../seedu/address/logic/commands/ViewLoansCommand.java | 4 ++-- src/main/java/seedu/address/model/Model.java | 7 ++++++- src/main/java/seedu/address/model/ModelManager.java | 3 +++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 3d6bd06d5af..19e1dfa0bcd 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -61,6 +61,7 @@ public void init() throws Exception { storage = new StorageManager(addressBookStorage, userPrefsStorage); model = initModelManager(storage, userPrefs); + model.updateFilteredLoanList(Model.PREDICATE_SHOW_NO_LOANS); logic = new LogicManager(model, storage); diff --git a/src/main/java/seedu/address/logic/commands/DeleteCommand.java b/src/main/java/seedu/address/logic/commands/DeleteCommand.java index 1135ac19b74..bf87cfa8672 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteCommand.java @@ -42,6 +42,7 @@ public CommandResult execute(Model model) throws CommandException { Person personToDelete = lastShownList.get(targetIndex.getZeroBased()); model.deletePerson(personToDelete); + model.setToPersonTab(); return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete))); } diff --git a/src/main/java/seedu/address/logic/commands/ViewLoansCommand.java b/src/main/java/seedu/address/logic/commands/ViewLoansCommand.java index 77c7ab5440e..eedc05b349c 100644 --- a/src/main/java/seedu/address/logic/commands/ViewLoansCommand.java +++ b/src/main/java/seedu/address/logic/commands/ViewLoansCommand.java @@ -3,7 +3,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ACTIVE_LOANS; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_LOANS; -import static seedu.address.model.Model.PREDICATE_SHOW_NO_PERSON; +import static seedu.address.model.Model.PREDICATE_SHOW_NO_PERSONS; import seedu.address.model.Model; @@ -22,7 +22,7 @@ public ViewLoansCommand(boolean isShowAllLoans) { @Override public CommandResult execute(Model model) { requireNonNull(model); - model.updateFilteredPersonList(PREDICATE_SHOW_NO_PERSON); + model.updateFilteredPersonList(PREDICATE_SHOW_NO_PERSONS); if (isShowAllLoans) { model.updateFilteredLoanList(PREDICATE_SHOW_ALL_LOANS); } else { diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index c0145a431a1..b979c778387 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -26,7 +26,7 @@ public interface Model { /** * {@code Predicate} that always evaluates to false */ - Predicate PREDICATE_SHOW_NO_PERSON = unused -> false; + Predicate PREDICATE_SHOW_NO_PERSONS = unused -> false; /** * {@code Predicate} that always evaluate to true @@ -38,6 +38,11 @@ public interface Model { */ Predicate PREDICATE_SHOW_ALL_ACTIVE_LOANS = loan -> loan.isActive(); + /** + * {@code Predicate} that always evaluates to false + */ + Predicate PREDICATE_SHOW_NO_LOANS = unused -> false; + /** * Replaces user prefs data with the data in {@code userPrefs}. */ diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index dc223243cb7..8882b430ee7 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -239,6 +239,7 @@ public void setToPersonTab() { System.out.println("Method setToPersonTab called inside ModelManager"); this.isLoansTab.setValue(false); this.isAnalyticsTab.setValue(false); + this.updateFilteredLoanList(PREDICATE_SHOW_NO_LOANS); this.setIsPersonTab(true); } @@ -247,6 +248,8 @@ public void setIsAnalyticsTab(Boolean isAnalyticsTab) { if (isAnalyticsTab) { this.isLoansTab.setValue(false); this.isPersonTab.setValue(false); + this.updateFilteredPersonList(PREDICATE_SHOW_NO_PERSONS); + this.updateFilteredLoanList(PREDICATE_SHOW_NO_LOANS); } this.isAnalyticsTab.setValue(isAnalyticsTab); }