public boolean changePassword(int id, AccountPasswordViewModel pswdViewModel) { boolean result = false; try { Accounts account = super.find(id); if (account.getPassword().equals(pswdViewModel.getOldPassword()) && pswdViewModel.getNewPassword() != null && pswdViewModel.getNewPassword().length() > 0) { account.setPassword(pswdViewModel.getNewPassword()); super.edit(account); result = true; } } catch (Exception e) { logger.warn(e.getMessage()); } return result; }
public Optional<AuthenticationResponseViewModel> authenticate( AccountAuthenticateViewModel authViewModel) { Optional<AuthenticationResponseViewModel> result = Optional.empty(); if (authViewModel.credentialsAreValid()) { Accounts account = null; try { if (authViewModel.userNameIsSet()) { account = em.createNamedQuery("Accounts.findByUserName", Accounts.class) .setParameter("userName", authViewModel.getUserName()) .getSingleResult(); } else if (authViewModel.emailIsSet()) { account = em.createNamedQuery("Accounts.findByEmail", Accounts.class) .setParameter("email", authViewModel.getEmail()) .getSingleResult(); } if (account.getPassword().equals(authViewModel.getPassword())) { logger.info( "authentication for user '" + account.getUserName() + "' (" + account.getId() + ") succeeded"); AuthenticationResponseViewModel authenticationResponseViewModel = new AuthenticationResponseViewModel(); authenticationResponseViewModel.setId(account.getId()); authenticationResponseViewModel.setRole(account.getRole()); result = Optional.of(authenticationResponseViewModel); } } catch (Exception e) { logger.warn(e.getMessage()); } } return result; }