From ea83da04005b918b763bfab53133b734dd1719a5 Mon Sep 17 00:00:00 2001 From: Kristijan Mitrovic Date: Wed, 15 Apr 2020 11:19:33 +0200 Subject: [PATCH] Remove automatic policy creation for deleted accounts --- .../bubble/dao/account/AccountPolicyDAO.java | 19 ++++++++++++++----- .../block_delete_account.json | 5 +++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/bubble-server/src/main/java/bubble/dao/account/AccountPolicyDAO.java b/bubble-server/src/main/java/bubble/dao/account/AccountPolicyDAO.java index f77eba69..47d0ffa1 100644 --- a/bubble-server/src/main/java/bubble/dao/account/AccountPolicyDAO.java +++ b/bubble-server/src/main/java/bubble/dao/account/AccountPolicyDAO.java @@ -8,10 +8,9 @@ import bubble.model.account.AccountPolicy; import org.cobbzilla.wizard.validation.ValidationResult; import org.springframework.stereotype.Repository; -import java.util.List; - import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.wizard.resources.ResourceUtil.invalidEx; +import static org.cobbzilla.wizard.resources.ResourceUtil.notFoundEx; @Repository public class AccountPolicyDAO extends AccountOwnedEntityDAO { @@ -32,9 +31,19 @@ public class AccountPolicyDAO extends AccountOwnedEntityDAO { } public AccountPolicy findSingleByAccount(String accountUuid) { - final List found = findByAccount(accountUuid); - return found.isEmpty() ? create(new AccountPolicy().setAccount(accountUuid)) - : found.size() > 1 ? die("findSingleByAccount: "+found.size()+" found!") : found.get(0); + final var found = findByAccount(accountUuid); + if (found.size() == 1) return found.get(0); + + if (found.size() > 1) { + die("findSingleByAccount: More than 1 policy found for account " + accountUuid + " - " + found.size()); + } + + // If there's no policy, and the account is marked as deleted - just return not found: + final var account = getConfiguration().getBean(AccountDAO.class).findById(accountUuid); + if (account.deleted()) throw notFoundEx(); + + // Else, create a policy for existing account: + return create(new AccountPolicy().setAccount(accountUuid)); } } diff --git a/bubble-server/src/test/resources/models/tests/account_deletion/block_delete_account.json b/bubble-server/src/test/resources/models/tests/account_deletion/block_delete_account.json index e261c638..304317e5 100644 --- a/bubble-server/src/test/resources/models/tests/account_deletion/block_delete_account.json +++ b/bubble-server/src/test/resources/models/tests/account_deletion/block_delete_account.json @@ -74,8 +74,9 @@ }, { - "comment": "as root, look up account policy again - it should recreated for this object with full deletion policy", + "comment": "as root, look up account policy again - it should not be found now", "request": { "session": "rootSession", "uri": "users/{{user.uuid}}/policy" }, - "response": { "check": [{ "condition": "json.getDeletionPolicy().name() == 'block_delete'" }] } + "response": { "status": 404 } } + ]