Java tutorial
/* *Copyright 2016 Dominik Szalai (emptulik@gmail.com) * *Licensed under the Apache License, Version 2.0 (the "License"); *you may not use this file except in compliance with the License. *You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *Unless required by applicable law or agreed to in writing, software *distributed under the License is distributed on an "AS IS" BASIS, *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *See the License for the specific language governing permissions and *limitations under the License. */ /* * Copyright 2016 Dominik Szalai (emptulik@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cz.muni.fi.editor.webapp.controllers; import cz.muni.fi.editor.api.UserService; import cz.muni.fi.editor.api.dto.UserDTO; import cz.muni.fi.editor.api.exceptions.FieldException; import cz.muni.fi.editor.services.commons.EditorPasswordEncoder; import cz.muni.fi.editor.services.commons.Mapper; import cz.muni.fi.editor.webapp.additions.ControllerConstants; import cz.muni.fi.editor.webapp.forms.UserFormCreate; import cz.muni.fi.editor.webapp.forms.UserFormProfile; import cz.muni.fi.editor.webapp.forms.factories.FormFactory; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.validation.Valid; import java.util.List; import java.util.Objects; /** * @author Dominik Szalai - emptulik at gmail.com */ @Controller(value = "UC") @RequestMapping("/auth/user") @Log4j2 public class UserController { private static final String REDIRECT = "redirect:/auth/user/"; private static final String USERS_LIST = "users.list"; @Autowired private UserService userService; @Autowired private FormFactory formFactory; @Autowired private EditorPasswordEncoder passwordEncoder; @Autowired private Mapper mapper; @RequestMapping(value = "/", method = RequestMethod.GET) public String list() { return USERS_LIST; } @RequestMapping(value = "/create/", method = RequestMethod.POST) public String createSubmit(@Valid @ModelAttribute("userForm") UserFormCreate userForm, BindingResult result) { log.fatal(userForm); // check if passwords are the same if (!Objects.equals(userForm.getPassword(), userForm.getPasswordAgain())) { userForm.setPassword(""); userForm.setPasswordAgain(""); result.rejectValue("passwordAgain", "user.password.nomatch"); } if (result.hasErrors()) { return USERS_LIST; } else { UserDTO user = mapper.map(userForm, UserDTO.class); passwordEncoder.hashUserPassword(user); try { userService.create(user); } catch (FieldException ex) { log.warn(ex); result.rejectValue(ex.getField(), ex.getMessage()); userForm.setPassword(""); userForm.setPasswordAgain(""); return USERS_LIST; } return REDIRECT; } } @RequestMapping(value = "/{userID}/enable/") public String enableUser(@PathVariable Long userID, RedirectAttributes redirectAttributes) { UserDTO u = new UserDTO(); u.setId(userID); try { userService.enable(u); } catch (FieldException ex) { log.warn(ex); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, ex.getMessage()); } return REDIRECT; } @RequestMapping(value = "/{userID}/disable/") public String disableUser(@PathVariable Long userID, RedirectAttributes redirectAttributes) { UserDTO u = new UserDTO(); u.setId(userID); try { userService.disable(u); } catch (FieldException ex) { log.warn(ex); redirectAttributes.addAttribute(ControllerConstants.ALERT_POPUP, ex.getMessage()); } return REDIRECT; } @RequestMapping(value = "/all/") public String showAll(Model model) { model.addAttribute("userList", userService.getAll()); return USERS_LIST; } @RequestMapping(value = "/profile/", method = RequestMethod.GET) public String viewProfile(@AuthenticationPrincipal UserDTO user, Model model) { UserFormProfile userProfile = mapper.map(user, UserFormProfile.class); userProfile.setPassword(null); model.addAttribute("profileForm", userProfile); return "users.profile"; } @RequestMapping(value = "/profile/", method = RequestMethod.POST) public String submitProfileChange(@ModelAttribute("profileForm") @Valid UserFormProfile userFormProfile, BindingResult bindingResult) { if (bindingResult.hasErrors() || checkPasswords(userFormProfile)) { return "users.profile"; } else { return "redirect:/auth/user/profile/"; } } private boolean checkPasswords(UserFormProfile userFormProfile) { return !userFormProfile.getPassword().equals(userFormProfile.getPasswordAgain()); } @ModelAttribute("userForm") public UserFormCreate userForm() { return formFactory.initForm(UserFormCreate.class); } @ModelAttribute("profileForm") public UserFormProfile profileForm() { return formFactory.initForm(UserFormProfile.class); } @ModelAttribute("userList") public List<UserDTO> populateUsers() { return userService.getActiveUsers(); } }