/** * Is the current user logged? */ default boolean isLogged() { return getCurrentAccount() != null; }
/** * Returns the current logged account as an option */ default Optional<Account> getAccount() { return Optional.ofNullable(getCurrentAccount()); }
@Override public <T> T load(PreferencesType<T> preferencesType, T defaultValue) { // Gets the current account Account account = securityService.getCurrentAccount(); if (account != null) { return repository.find(account.id(), preferencesType.getClass().getName()) .map(preferencesType::fromStorage) .orElse(defaultValue); } else { // Not logged return defaultValue; } }
@Override public <T> void store(PreferencesType<T> preferencesType, T value) { // Gets the current account Account account = securityService.getCurrentAccount(); if (account != null) { repository.store( account.id(), preferencesType.getClass().getName(), preferencesType.forStorage(value) ); } } }
@Override public Ack changePassword(PasswordChange input) { // Checks the account Account account = securityService.getCurrentAccount(); if (account == null) { throw new AccessDeniedException("Must be logged to change password."); } else if (!account.getAuthenticationSource().isAllowingPasswordChange()) { throw new AccessDeniedException("Password change is not allowed from ontrack."); } else if (!accountRepository.checkPassword( account.id(), encodedPassword -> passwordEncoder.matches(input.getOldPassword(), encodedPassword) )) { throw new UserOldPasswordException(); } else { accountRepository.setPassword( account.id(), passwordEncoder.encode(input.getNewPassword()) ); return Ack.OK; } } }