use of com.dappermoose.stsimplefinance.data.LoginUser in project spring-thymeleaf-simplefinance by heitkergm.
the class RegisterAction method processRegisterAction.
/**
* Process register action.
*
* @param register the register
* @param res the res
* @param model the model
* @param request the request
* @return the string
*/
@Transactional
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegisterAction(@Valid @ModelAttribute("register") final RegisterUser register, final BindingResult res, final Model model, final HttpServletRequest request) {
if (res.hasErrors()) {
model.addAttribute("register", register);
model.addAttribute("tzones", context.getBean("tzones"));
return "register";
}
// check if the user already exists
final List<LoginUser> users = userRepository.findByUserName(register.getUserName());
if (users.size() > 0) {
final ObjectError objErr = new ObjectError("RegisterUser", messageSource.getMessage("register.duplicateUser", null, request.getLocale()));
res.addError(objErr);
model.addAttribute("register", register);
model.addAttribute("tzones", context.getBean("tzones"));
return "register";
}
// create user bean
final LoginUser user = new LoginUser();
user.setUserName(register.getUserName());
user.setPassword(register.getPassword());
user.setTzone(register.getTzone());
userRepository.save(user);
return "redirect:/main";
}
use of com.dappermoose.stsimplefinance.data.LoginUser in project spring-thymeleaf-simplefinance by heitkergm.
the class UserDetailsDaoImpl method loadUserByUsername.
@Override
public UserDetails loadUserByUsername(final String userName) throws UsernameNotFoundException {
List<LoginUser> users = userRepository.findByUserName(userName);
if (users.size() <= 0) {
throw new UsernameNotFoundException("User Name " + userName + " not found.");
}
LoginUser luser = users.get(0);
UserDetails user = new User(luser.getUserName(), luser.getPassword(), YesNoEnum.toBoolean(luser.getEnabled()), true, true, true, AuthorityUtils.NO_AUTHORITIES);
return user;
}
use of com.dappermoose.stsimplefinance.data.LoginUser in project spring-thymeleaf-simplefinance by heitkergm.
the class ChangePasswordAction method mainAction.
/**
* Main action. Display the form.
*
* @param model the model
* @return the string
*/
@RequestMapping(value = "/changepwd", method = RequestMethod.GET)
public String mainAction(final Model model) {
final ChangePwd changepwd = new ChangePwd();
String userName = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
List<LoginUser> users = userRepository.findByUserName(userName);
LoginUser user = users.get(0);
changepwd.setUserName(user.getUserName());
changepwd.setTzone(user.getTzone());
model.addAttribute("changepwd", changepwd);
model.addAttribute("tzones", context.getBean("tzones"));
return "changepwd";
}
use of com.dappermoose.stsimplefinance.data.LoginUser in project spring-thymeleaf-simplefinance by heitkergm.
the class ChangePasswordAction method processRegisterAction.
/**
* Process change password action.
*
* @param changepwd the change password form data
* @param res the res
* @param model the model
* @param request the request
* @return the string
*/
@Transactional
@RequestMapping(value = "/changepwd", method = RequestMethod.POST)
public String processRegisterAction(@Valid @ModelAttribute("changepwd") final ChangePwd changepwd, final BindingResult res, final Model model, final HttpServletRequest request) {
if (res.hasErrors()) {
model.addAttribute("changepwd", changepwd);
model.addAttribute("tzones", context.getBean("tzones"));
return "changepwd";
}
// check the current password, etc.
String userName = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
List<LoginUser> users = userRepository.findByUserName(userName);
LoginUser user = users.get(0);
if (!user.checkpw(changepwd.getCurrentPassword())) {
final ObjectError objErr = new ObjectError("User", messageSource.getMessage("changepwd.currpwd.bad", null, request.getLocale()));
res.addError(objErr);
model.addAttribute("changepwd", changepwd);
model.addAttribute("tzones", context.getBean("tzones"));
return "changepwd";
}
// modify user bean
user.setPassword(changepwd.getPassword());
user.setTzone(changepwd.getTzone());
userRepository.save(user);
// logout
try {
request.logout();
} catch (ServletException se) {
LOG.info("Servlet exception upon logout " + se.getMessage());
}
return "redirect:/main";
}
use of com.dappermoose.stsimplefinance.data.LoginUser in project spring-thymeleaf-simplefinance by heitkergm.
the class MainAction method mainAction.
/**
* Main action.
*
* @param model
* - the model to display
* @param principal
* - the user principal from spring security
*
* @return the string
*/
@RequestMapping("/main")
public String mainAction(final Model model, final Principal principal) {
final List<LoginUser> logins = loginRepository.findByUserName(principal.getName());
final List<Account> accounts = accountRepository.findByUser(logins.get(0));
model.addAttribute("accounts", accounts);
// get the overall balance.
BigDecimal balance = BigDecimal.ZERO.setScale(2);
for (final Account acct : accounts) {
balance = balance.add(acct.getStartingBalance().setScale(2));
}
model.addAttribute("balance", balance);
return "main";
}
Aggregations