Browse Source

Cleaned home controller

master
Rohan Sircar 4 years ago
parent
commit
33bd91b5b7
  1. 88
      chatto/src/main/java/org/ros/chatto/controller/Home.java

88
chatto/src/main/java/org/ros/chatto/controller/Home.java

@ -1,7 +1,7 @@
package org.ros.chatto.controller;
import java.security.Principal;
import java.sql.SQLException;
import java.util.Optional;
import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.service.UserService;
@ -9,85 +9,43 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*
@Controller
@RequestMapping(value = "/test")
public class TestController {
@GetMapping
public ModelAndView getTestData() {
ModelAndView mv = new ModelAndView();
mv.setViewName("welcome");
mv.getModel().put("data", "Welcome home man");
return mv;
}
}*/
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class Home {
@Autowired
private UserService userService;
// @Autowired
// private DBInitializerService dbInitializerService;
// private boolean installationChecked = false;
private final UserService userService;
@RequestMapping("/")
public ModelAndView showPage(Principal principal) throws SQLException {
ModelAndView mv = new ModelAndView("home");
String welcomeMesage = String.format("Welcome to chatto");
if (principal != null) {
welcomeMesage = String.format("Welcome back %s!", principal.getName());
}
mv.addObject("message", welcomeMesage);
// mv.addObject("userNames", userService.findAllOtherUsers(principal.getName()));
// if (!installationChecked) {
// dbInitializerService.connectDB();
// if(dbInitializerService.getNumTables() == 0)
// dbInitializerService.populateDB();
// dbInitializerService.closeConnection();
// installationChecked = true;
// }
return mv;
}
@GetMapping("/crypt")
public String showCrypt() {
return "crypto";
public String showPage(Model model, Principal principal) {
final String welcomeMessage = Optional.of(principal).map(p -> {
return String.format("Welcome back %s!", p.getName());
}).orElse("Welcome to chatto");
model.addAttribute("message", welcomeMessage);
return "home";
}
@GetMapping("/chat")
public ModelAndView showChat(Principal principal) {
ModelAndView modelAndView = new ModelAndView("chat");
modelAndView.addObject(new ChatMessageDTO());
// modelAndView.addObject("userNames", userRepositoryCustom.getAllUserNames("hmm"));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Set<String> roles = authentication.getAuthorities().stream().map(r -> r.getAuthority())
// .collect(Collectors.toSet());
// for (String r : roles) {
// System.out.println(r);
// }
public String showChat(Model model, Principal principal) {
model.addAttribute(new ChatMessageDTO());
// boolean hasUserRole = authentication.getAuthorities().stream()
// .anyMatch(r -> r.getAuthority().equals("ROLE_USER"));
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
boolean isAdmin = authentication.getAuthorities().stream()
.anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN") || r.getAuthority().equals("ROLE_SUPER_USER"));
System.out.println("Is admin? " + isAdmin);
modelAndView.addObject("activeUsers", userService.getOtherActiveUsers(principal.getName()));
return modelAndView;
.anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN")
|| r.getAuthority().equals("ROLE_SUPER_USER"));
log.trace("Is admin? " + isAdmin);
model.addAttribute("activeUsers",
userService.getOtherActiveUsers(principal.getName()));
return "chat";
}
// public String showHome(Model model)
// {
// model.addAttribute("message", "Welcome");
// return "home";
// }
}
Loading…
Cancel
Save