Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package joseph.hotel.controllers;
  2.  
  3. import joseph.hotel.entities.users.models.UsersEntity;
  4. import joseph.hotel.entities.users.models.UsersProfileEntity;
  5. import joseph.hotel.entities.users.service.UserProfileService;
  6. import joseph.hotel.entities.users.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.core.context.SecurityContextHolder;
  10. import org.springframework.security.core.userdetails.UserDetails;
  11. import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.ModelMap;
  14. import org.springframework.validation.BindingResult;
  15. import org.springframework.web.bind.annotation.ModelAttribute;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18.  
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.validation.Valid;
  22. import java.util.List;
  23.  
  24. @Controller
  25. public class PetHotelController {
  26.  
  27.     @Autowired
  28.     private
  29.     UserProfileService userProfileService;
  30.  
  31.     @Autowired
  32.     private
  33.     UserService userService;
  34.  
  35.     @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
  36.     public String homePage(ModelMap model) {
  37.         model.addAttribute("greeting", "Welcome to PetHotel");
  38.         return "welcome";
  39.     }
  40.  
  41.     @RequestMapping(value = "/admin", method = RequestMethod.GET)
  42.     public String adminPage(ModelMap model) {
  43.         model.addAttribute("user", getPrincipal());
  44.         return "admin";
  45.     }
  46.  
  47.     @RequestMapping(value = "/db", method = RequestMethod.GET)
  48.     public String dbaPage(ModelMap model) {
  49.         model.addAttribute("user", getPrincipal());
  50.         return "dba";
  51.     }
  52.  
  53.     @RequestMapping(value = "/Access_Denied", method = RequestMethod.GET)
  54.     public String accessDeniedPage(ModelMap model) {
  55.         model.addAttribute("user", getPrincipal());
  56.         return "accessDenied";
  57.     }
  58.  
  59.     @RequestMapping(value = "/login", method = RequestMethod.GET)
  60.     public String loginPage() {
  61.         return "login";
  62.     }
  63.  
  64.     @RequestMapping(value="/logout", method = RequestMethod.GET)
  65.     public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
  66.         Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  67.         if (auth != null){
  68.             new SecurityContextLogoutHandler().logout(request, response, auth);
  69.         }
  70.         return "redirect:/login?logout";
  71.     }
  72.  
  73.  
  74.     @RequestMapping(value = "/newUser", method = RequestMethod.GET)
  75.     public String newRegistration(ModelMap model) {
  76.         UsersEntity user = new UsersEntity();
  77.         model.addAttribute("user", user);
  78.         return "newuser";
  79.     }
  80.  
  81.     /*
  82.      * This method will be called on form submission, handling POST request It
  83.      * also validates the user input
  84.      */
  85.     @RequestMapping(value = "/newUser", method = RequestMethod.POST)
  86.     public String saveRegistration(@Valid UsersEntity user,
  87.                                    BindingResult result, ModelMap model) {
  88.  
  89.         if (result.hasErrors()) {
  90.             System.out.println("There are errors");
  91.             return "newuser";
  92.         }
  93.         userService.save(user);
  94.         System.out.println("Username : "+user.getUsername());
  95.         System.out.println("Password : "+user.getPassword());
  96.         System.out.println("Email : "+user.getEmail());
  97.         System.out.println("Status : " + user.getState());
  98.         System.out.println("Checking UsrProfiles....");
  99.         if(user.getUserProfiles()!=null){
  100.             for(UsersProfileEntity profile : user.getUserProfiles()){
  101.                 System.out.println("Profile : "+ profile.getType());
  102.             }
  103.         }
  104.  
  105.         model.addAttribute("success", "User " + user.getUsername() + " has been registered successfully");
  106.         return "registrationsuccess";
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.     private String getPrincipal(){
  113.         String userName = null;
  114.         Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  115.  
  116.         if (principal instanceof UserDetails) {
  117.             userName = ((UserDetails)principal).getUsername();
  118.         } else {
  119.             userName = principal.toString();
  120.         }
  121.         return userName;
  122.     }
  123.  
  124.  
  125.  
  126.     @ModelAttribute("roles")
  127.     public List<UsersProfileEntity> initializeProfiles() {
  128.         return userProfileService.findAll();
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement