Java tutorial
/* * The MIT License * * Copyright 2015 Tijme Gommers <t.gommers@jetcat.nl>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.klm.workshop.controller.host.manage; import com.klm.workshop.dao.UserDAO; import com.klm.workshop.helper.PasswordHasher; import com.klm.workshop.model.User; import com.klm.workshop.security.CustomUserDetails; import com.klm.workshop.validator.MyAccountValidator; import java.util.Locale; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; /** * Host manage account controller * * @author Tijme Gommers <t.gommers@jetcat.nl> */ @Controller("hostManageAccountController") @RequestMapping(value = "/host/manage") public class AccountController { /** * User data access object */ @Autowired private UserDAO userDAO; /** * Translations */ @Autowired private MessageSource messageSource; /** * Retrieve the currently logged in user * * @return Current user */ private User getCurrentUser() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); CustomUserDetails details = (CustomUserDetails) auth.getPrincipal(); return details.getUser(); } /** * Show update account form * * @param model Objects and view * @return Form to update my account */ @RequestMapping(value = "/account/my-account", method = RequestMethod.GET) public ModelAndView getUpdate(ModelAndView model) { model.addObject("user", new MyAccountValidator((User) userDAO.findById(getCurrentUser().getId()))); model.setViewName("host/manage/account/my_account"); return model; } /** * Update my account, and show update account form. On error, show errors. On * success, show success message. * * @param model Objects and view * @param user The posted user * @param result Binded validation * @param redirect The redirect attributes * @param locale The current client locale * @return Form to update a user, or a redirect (if user was updated successfully) */ @RequestMapping(value = "/account/my-account", method = RequestMethod.POST) public ModelAndView postUpdate(ModelAndView model, @ModelAttribute("user") @Valid MyAccountValidator user, BindingResult result, RedirectAttributes redirect, Locale locale) { if (result.hasErrors()) { model.addObject("roles", User.Role.values()); model.setViewName("host/manage/account/my_account"); } else { User oldUser = (User) userDAO.findById(getCurrentUser().getId()); oldUser.setFirstName(user.getFirstName()); oldUser.setLastName(user.getLastName()); oldUser.setEmail(user.getEmail()); if (user.hasNewPassword()) { String hash = PasswordHasher.hash(user.getPassword()); oldUser.setPassword(hash); } userDAO.update(oldUser); String success = messageSource.getMessage("general.update_success", new String[] { user.getFullName() }, locale); redirect.addFlashAttribute("alertSuccess", success); model.setViewName("redirect:/host/manage/account/my-account"); } return model; } }