com.github.iintelligas.controller.UserController Maven / Gradle / Ivy
The newest version!
package com.github.iintelligas.controller;
import com.github.iintelligas.exception.DuplicateDataException;
import com.github.iintelligas.exception.NoDataFoundException;
import com.github.iintelligas.persist.dto.Role;
import com.github.iintelligas.persist.dto.User;
import com.github.iintelligas.persist.entity.Enabled;
import com.github.iintelligas.service.RoleService;
import com.github.iintelligas.service.UserService;
import com.github.iintelligas.util.UserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
@Controller
public class UserController
{
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private final UserService userService;
private final RoleService roleService;
private final PasswordEncoder passwordEncoder;
@Autowired
public UserController(UserService userService, PasswordEncoder passwordEncoder, RoleService roleService) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
this.roleService = roleService;
}
@ModelAttribute("allEnabled")
public List populateEnabled() {
return Arrays.asList(Enabled.values());
}
@GetMapping("/profile-user-list")
public String landing(Model model)
{
populateUsers(model);
return "profile/profile-user-list";
}
private void populateUsers(Model model) {
List users = userService.getUsers();
model.addAttribute("users", users);
}
@GetMapping("/profile-user-add")
public String userAdd(Model model)
{
User user = new User();
user.setRoles(roleService.getRoles());
model.addAttribute("user", user);
return "profile/profile-user-add";
}
@PostMapping(value = "/profile-user-add-save")
public ModelAndView userAddSave(@ModelAttribute User user, @RequestParam String action)
{
if(logger.isDebugEnabled())
{
logger.debug("START - profile-user-add-save");
}
ModelAndView modelAndView ;
if("cancel".equals(action))
{
modelAndView = getModelAndViewForCancelFlow();
return modelAndView;
}
try
{
if(StringUtils.isBlank(user.getPassword()))
{
modelAndView = new ModelAndView("profile/profile-user-add");
modelAndView.addObject("errorMessage", "Password is required.");
modelAndView.addObject("user", user);
return modelAndView;
}
if(!user.getPassword().equals(user.getConfirm()))
{
modelAndView = new ModelAndView("profile/profile-user-add");
modelAndView.addObject("errorMessage", "Confirm does not patch with Password.");
modelAndView.addObject("user", user);
return modelAndView;
}
List selectedRole = new ArrayList<>();
for(Role role : user.getRoles())
{
if (role.isSelected())
{
selectedRole.add(role);
}
}
user.getRoles().clear();
if(CollectionUtils.isNotEmpty(selectedRole)) {
user.setRoles(selectedRole);
}
String planPassword = user.getPassword();
user.setPassword(passwordEncoder.encode(planPassword));
user = userService.addUser(user);
String message = "User '" + user.getUsername() + "' successfully added";
modelAndView = getModelAndViewForCancelFlow();
modelAndView.addObject("successMessage", message);
}
catch (Exception e)
{
modelAndView = new ModelAndView("profile/profile-user-add");
modelAndView.addObject("user", user);
if (e instanceof DuplicateDataException)
{
modelAndView.addObject("errorMessage", e.getMessage());
}
logger.error(e.getMessage(), e);
}
if(logger.isDebugEnabled())
{
logger.debug("END - profile-user-add-save");
}
return modelAndView;
}
@GetMapping("/profile-user-update")
public String userEdit(Model model, @RequestParam(value = "id") String id)
{
if(StringUtils.isBlank(id))
{
model.addAttribute("errorMessage", "Invalid User Id. Please provide proper User Id to Edit.");
return "profile/profile-user-update";
}
Long idLong;
try
{
idLong = Long.valueOf(id);
}
catch (NumberFormatException e)
{
model.addAttribute("errorMessage", "Invalid User Id. Please provide proper User Id to Edit.");
return "profile/profile-user-update";
}
User user = userService.getUser(idLong);
if(user == null)
{
model.addAttribute("errorMessage", "Could not found User with Id : "+id);
return "profile/profile-user-update";
}
Map stringRoleMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(user.getRoles()))
{
for (Role selectedRole : user.getRoles())
{
stringRoleMap.put(selectedRole.getId(), selectedRole);
}
}
List storedRoles = roleService.getRoles();
List allRoles = new ArrayList<>(storedRoles.size());
for(Role role : storedRoles)
{
if (stringRoleMap.get(role.getId()) == null)
{
role.setSelected(false);
allRoles.add(role);
}
else
{
role.setSelected(true);
allRoles.add(role);
}
}
if(CollectionUtils.isNotEmpty(user.getRoles()))
{
user.getRoles().clear();
}
user.setTempPassword(user.getPassword());
user.setRoles(allRoles);
model.addAttribute("user", user);
return "profile/profile-user-update";
}
@PostMapping(value = "/profile-user-update-save")
public ModelAndView userUpdateSave(@ModelAttribute User user, @RequestParam String action)
{
if(logger.isDebugEnabled())
{
logger.debug("START - profile-user-update-save");
}
ModelAndView modelAndView ;
if("cancel".equals(action))
{
modelAndView = getModelAndViewForCancelFlow();
return modelAndView;
}
try
{
String planPassword = user.getPassword();
if(StringUtils.isNotBlank(planPassword))
{
if(!planPassword.equals(user.getConfirm())) {
modelAndView = new ModelAndView("profile/profile-user-update");
modelAndView.addObject("errorMessage", "Confirm does not patch with Password.");
modelAndView.addObject("user", user);
return modelAndView;
}
user.setPassword(passwordEncoder.encode(planPassword));
}
else
{
user.setPassword(user.getTempPassword());
}
List selectedRole = new ArrayList<>();
for(Role role : user.getRoles())
{
if (role.isSelected())
{
selectedRole.add(role);
}
}
user.getRoles().clear();
if(CollectionUtils.isNotEmpty(selectedRole)) {
user.setRoles(selectedRole);
}
userService.updateUser(user);
String message = "User '" + user.getUsername() + "' successfully updated";
modelAndView = getModelAndViewForCancelFlow();
modelAndView.addObject("successMessage", message);
}
catch (Exception e)
{
Map params=new HashMap<>();
params.put("id", String.valueOf(user.getId()));
modelAndView = new ModelAndView("profile/profile-user-update", params);
modelAndView.addObject("user", user);
if (e instanceof DuplicateDataException)
{
modelAndView.addObject("errorMessage", e.getMessage());
}
logger.error(e.getMessage(), e);
}
if(logger.isDebugEnabled())
{
logger.debug("END - profile-user-update-save");
}
return modelAndView;
}
@GetMapping("/profile-user-delete")
public String userDelete(Model model, @RequestParam(value = "id") String id)
{
if(StringUtils.isBlank(id))
{
model.addAttribute("errorMessage", "Invalid User Id. Please provide proper User Id to delete.");
populateUsers(model);
return "profile/profile-user-list";
}
Long idLong;
try
{
idLong = Long.valueOf(id);
}
catch (NumberFormatException e)
{
model.addAttribute("errorMessage", "Invalid User Id. Please provide proper User Id to delete.");
populateUsers(model);
return "profile/profile-user-list";
}
try {
userService.deleteUser(idLong);
populateUsers(model);
model.addAttribute("successMessage", "Successfully deleted User.");
return "profile/profile-user-list";
}
catch (NoDataFoundException e)
{
populateUsers(model);
model.addAttribute("errorMessage", e.getMessage());
return "profile/profile-user-list";
}
}
private ModelAndView getModelAndViewForCancelFlow() {
ModelAndView modelAndView = new ModelAndView("profile/profile-user-list");
List users = userService.getUsers();
modelAndView.addObject("users", users);
return modelAndView;
}
@GetMapping("/profile-update.html")
public String roleAdd(Model model)
{
User user = UserUtil.getLoggedinUser();
user.setTempPassword(user.getPassword());
model.addAttribute("user", user);
return "profile/profile-update";
}
@PostMapping(value = "/profile-update-save")
public ModelAndView profileUpdateSave(@ModelAttribute User user, @RequestParam String action)
{
if(logger.isDebugEnabled())
{
logger.debug("START - profile-update-save");
}
ModelAndView modelAndView = new ModelAndView("profile/profile-update");
String planPassword = user.getPassword();
if(StringUtils.isNotBlank(planPassword))
{
if(!planPassword.equals(user.getConfirm())) {
modelAndView.addObject("errorMessage", "Confirm does not patch with Password.");
modelAndView.addObject("user", user);
return modelAndView;
}
user.setPassword(passwordEncoder.encode(planPassword));
}
else
{
user.setPassword(user.getTempPassword());
}
if(StringUtils.isBlank(user.getFirstName()))
{
modelAndView.addObject("errorMessage", "First Name is required.");
modelAndView.addObject("user", user);
return modelAndView;
}
if(StringUtils.isBlank(user.getLastName()))
{
modelAndView.addObject("errorMessage", "Last Name is required.");
modelAndView.addObject("user", user);
return modelAndView;
}
userService.updateProfile(user);
String message = "User '" + user.getUsername() + "' successfully updated";
modelAndView.addObject("successMessage", message);
if(logger.isDebugEnabled())
{
logger.debug("END - profile-update-save");
}
return modelAndView;
}
}