Skip to content
Snippets Groups Projects
Commit 5a356c2f authored by Robert Goldmann's avatar Robert Goldmann
Browse files

at least one account must be set to visibility full access (otherwise no default account exists)

parent 10cfbe50
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -119,7 +120,7 @@ public class AccountController extends BaseController ...@@ -119,7 +120,7 @@ public class AccountController extends BaseController
} }
@PostMapping(value = "/newAccount") @PostMapping(value = "/newAccount")
public String post(HttpServletRequest request, Model model, public String post(HttpServletRequest request, WebRequest webRequest, Model model,
@ModelAttribute("NewAccount") Account account, @ModelAttribute("NewAccount") Account account,
BindingResult bindingResult) BindingResult bindingResult)
{ {
...@@ -133,6 +134,15 @@ public class AccountController extends BaseController ...@@ -133,6 +134,15 @@ public class AccountController extends BaseController
bindingResult.addError(new FieldError("NewAccount", "name", "", false, new String[]{"warning.duplicate.account.name"}, null, null)); bindingResult.addError(new FieldError("NewAccount", "name", "", false, new String[]{"warning.duplicate.account.name"}, null, null));
} }
List<Account> activatedAccounts = accountService.getRepository().findAllByTypeAndAccountStateOrderByNameAsc(AccountType.CUSTOM, AccountState.FULL_ACCESS);
boolean newAccountStateWouldLeaveNoFullAccessAccounts = activatedAccounts.size() == 1 && account.getAccountState() != AccountState.FULL_ACCESS;
if(!isNewAccount && newAccountStateWouldLeaveNoFullAccessAccounts)
{
final String warningMessage = Localization.getString("warning.account.edit.state", Localization.getString(AccountState.FULL_ACCESS.getLocalizationKey()));
WebRequestUtils.putNotification(webRequest, new Notification(warningMessage, NotificationType.WARNING));
bindingResult.addError(new FieldError("NewAccount", "state", account.getAccountState(), false, new String[]{"warning.account.edit.state"}, null, null));
}
if(bindingResult.hasErrors()) if(bindingResult.hasErrors())
{ {
model.addAttribute("error", bindingResult); model.addAttribute("error", bindingResult);
...@@ -171,6 +181,15 @@ public class AccountController extends BaseController ...@@ -171,6 +181,15 @@ public class AccountController extends BaseController
existingAccount.setType(AccountType.CUSTOM); existingAccount.setType(AccountType.CUSTOM);
existingAccount.setAccountState(newAccount.getAccountState()); existingAccount.setAccountState(newAccount.getAccountState());
accountService.getRepository().save(existingAccount); accountService.getRepository().save(existingAccount);
if(existingAccount.isDefault() && existingAccount.getAccountState() != AccountState.FULL_ACCESS)
{
// set any activated account as new default account
accountService.unsetDefaultForAllAccounts();
List<Account> activatedAccounts = accountService.getRepository().findAllByTypeAndAccountStateOrderByNameAsc(AccountType.CUSTOM, AccountState.FULL_ACCESS);
Account newDefaultAccount = activatedAccounts.get(0);
accountService.setAsDefaultAccount(newDefaultAccount.getID());
}
} }
} }
} }
\ No newline at end of file
...@@ -151,6 +151,7 @@ delete.image.error.not.existing=Fehler: Bild mit ID {0} existiert nicht ...@@ -151,6 +151,7 @@ delete.image.error.not.existing=Fehler: Bild mit ID {0} existiert nicht
# WARNING # WARNING
warning.text.account.delete=Das Konto "{0}" kann nicht gelöscht werden, da mindestens ein Konto existieren muss. Um dieses Konto zu löschen musst du zuerst ein neues anlegen. warning.text.account.delete=Das Konto "{0}" kann nicht gelöscht werden, da mindestens ein Konto existieren muss. Um dieses Konto zu löschen musst du zuerst ein neues anlegen.
warning.account.edit.state=Für mindestens ein Konto muss die Sichtbarkeit auf "{0}" gesetzt sein!s
warning.empty.category.name=Bitte gib einen Namen ein. warning.empty.category.name=Bitte gib einen Namen ein.
warning.empty.account.name=Bitte gib einen Namen ein. warning.empty.account.name=Bitte gib einen Namen ein.
warning.duplicate.account.name=Es existiert bereits ein Konto mit diesem Namen. warning.duplicate.account.name=Es existiert bereits ein Konto mit diesem Namen.
......
...@@ -151,6 +151,7 @@ delete.image.error.not.existing=Error: Image with ID {0} does not exist ...@@ -151,6 +151,7 @@ delete.image.error.not.existing=Error: Image with ID {0} does not exist
# WARNING # WARNING
warning.text.account.delete=The account "{0}" could not be deleted, because at least one account must exist at all time. You have to create a new account in order to delete this one. warning.text.account.delete=The account "{0}" could not be deleted, because at least one account must exist at all time. You have to create a new account in order to delete this one.
warning.account.edit.state=At least one account must have set it's visibility to "{0}"!
warning.empty.category.name=Please insert a name. warning.empty.category.name=Please insert a name.
warning.empty.account.name=Please insert a name. warning.empty.account.name=Please insert a name.
warning.duplicate.account.name=An account with this name is already existing. warning.duplicate.account.name=An account with this name is already existing.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment