You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.0 KiB
94 lines
3.0 KiB
package org.ros.chatto.controller;
|
|
|
|
import java.security.Principal;
|
|
import java.sql.SQLException;
|
|
|
|
import org.ros.chatto.dto.ChatMessageDTO;
|
|
import org.ros.chatto.service.DBInitializerService;
|
|
import org.ros.chatto.service.UserService;
|
|
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.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;
|
|
}
|
|
}*/
|
|
|
|
@Controller
|
|
public class Home {
|
|
|
|
@Autowired
|
|
private UserService userService;
|
|
// @Autowired
|
|
// private DBInitializerService dbInitializerService;
|
|
|
|
// private boolean installationChecked = false;
|
|
|
|
@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";
|
|
}
|
|
|
|
@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);
|
|
// }
|
|
|
|
// boolean hasUserRole = authentication.getAuthorities().stream()
|
|
// .anyMatch(r -> r.getAuthority().equals("ROLE_USER"));
|
|
|
|
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("userNames", userService.findAllOtherUsers(principal.getName()));
|
|
return modelAndView;
|
|
}
|
|
// public String showHome(Model model)
|
|
// {
|
|
// model.addAttribute("message", "Welcome");
|
|
// return "home";
|
|
// }
|
|
}
|