Loading src/main/java/com/example/demo/CustomLoginSuccessHandler.java +3 −5 Original line number Diff line number Diff line Loading @@ -39,12 +39,10 @@ public class CustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHan } // check user role and decide the redirect URL if (roles.contains("ADMIN")) { url = "/admin"; } else if (roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/"; if (roles.contains("ADMIN") || roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/connected"; } return url; } } src/main/java/com/example/demo/controller/UserController.java +17 −14 Original line number Diff line number Diff line package com.example.demo.controller; import java.security.Principal; import java.util.List; import javax.servlet.http.HttpSession; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; Loading @@ -16,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.example.demo.HelpPropositionRepository; import com.example.demo.HelpRequest; import com.example.demo.HelpRequestRepository; import com.example.demo.model.User; import com.example.demo.repository.UserRepository; Loading Loading @@ -69,26 +65,33 @@ public class UserController { } @RequestMapping(value = { "/login" }, method = RequestMethod.GET) public ModelAndView login(HttpSession session, Principal principal) { public ModelAndView login() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("login"); // resources/template/login.html return modelAndView; } @RequestMapping(value = { "connected" }, method = RequestMethod.GET) public ModelAndView connected(HttpSession session, Principal principal) { if(principal != null){ User user = userService.findByEmail(principal.getName()); session.setAttribute("username", user.getUsername()); session.setAttribute("id", user.getId()); session.setAttribute("id", String.valueOf(user.getId())); session.setAttribute("isPoulain", String.valueOf(user.isPoulain())); session.setAttribute("isMentor", String.valueOf(user.isMentor())); session.setAttribute("isAdmin", String.valueOf(user.isAdmin())); } ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("login"); // resources/template/login.html return modelAndView; return new ModelAndView("redirect:/"); } @RequestMapping(value = {"/index", "", "/"}, method = RequestMethod.GET) public ModelAndView index(HttpSession session, Principal principal) { public ModelAndView index() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); // resources/template/index.html String email = SecurityContextHolder.getContext().getAuthentication().getName(); User user = userService.findByEmail(email); if (user != null) session.setAttribute("username", user.getUsername()); return modelAndView; } Loading src/main/java/com/example/demo/model/Role.java +8 −0 Original line number Diff line number Diff line Loading @@ -37,5 +37,13 @@ public class Role { this.name = name; } public String getDesc(){ return desc; } public void setDesc(String desc){ this.desc = desc; } } No newline at end of file src/main/java/com/example/demo/model/User.java +21 −3 Original line number Diff line number Diff line Loading @@ -92,15 +92,33 @@ public class User { return roles; } public String getRole(){ ArrayList<String> rolesarray = new ArrayList<String>(); public ArrayList<String> getRolesArray(){ ArrayList<String> returnArray = new ArrayList<String>(); for (Role r : roles){ rolesarray.add(r.getName()); returnArray.add(r.getName()); } return returnArray; } public String getRole(){ ArrayList<String> rolesarray = getRolesArray(); return rolesarray.get(0); } public boolean isPoulain(){ return getRolesArray().contains("POULAIN"); } public boolean isMentor(){ return getRolesArray().contains("MENTOR"); } public boolean isAdmin(){ return getRolesArray().contains("ADMIN"); } public void setRoles(Set<Role> roles) { this.roles = roles; } Loading src/main/resources/templates/allHelpRequest.html +2 −2 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ <body> <header th:insert="nav.html :: header"> </header> <h1>All Help Request</h1> <div th:each="help: ${helpPropositions}"> <div th:each="help: ${helpRequests}"> <div class="row"> <div class="col s12 m6"> <div class="card-panel teal lighten-1"> Loading @@ -22,7 +22,7 @@ <p th:text="${help.poulain.id}"></p> <p th:if="${help.mentor != null}" th:text=" ${help.mentor.id}"></p> <form th:unless="${help.mentor != null}" action="#" th:action="@{/editProposition/{id}(id=${help.id})}"> <form th:unless="${help.mentor != null}" action="#" th:action="@{/editRequest/{id}(id=${help.id})}"> <input class="waves-effect waves-light btn right teal darken-2" type="submit" value="Se proposer"/> </form> Loading Loading
src/main/java/com/example/demo/CustomLoginSuccessHandler.java +3 −5 Original line number Diff line number Diff line Loading @@ -39,12 +39,10 @@ public class CustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHan } // check user role and decide the redirect URL if (roles.contains("ADMIN")) { url = "/admin"; } else if (roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/"; if (roles.contains("ADMIN") || roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/connected"; } return url; } }
src/main/java/com/example/demo/controller/UserController.java +17 −14 Original line number Diff line number Diff line package com.example.demo.controller; import java.security.Principal; import java.util.List; import javax.servlet.http.HttpSession; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; Loading @@ -16,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.example.demo.HelpPropositionRepository; import com.example.demo.HelpRequest; import com.example.demo.HelpRequestRepository; import com.example.demo.model.User; import com.example.demo.repository.UserRepository; Loading Loading @@ -69,26 +65,33 @@ public class UserController { } @RequestMapping(value = { "/login" }, method = RequestMethod.GET) public ModelAndView login(HttpSession session, Principal principal) { public ModelAndView login() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("login"); // resources/template/login.html return modelAndView; } @RequestMapping(value = { "connected" }, method = RequestMethod.GET) public ModelAndView connected(HttpSession session, Principal principal) { if(principal != null){ User user = userService.findByEmail(principal.getName()); session.setAttribute("username", user.getUsername()); session.setAttribute("id", user.getId()); session.setAttribute("id", String.valueOf(user.getId())); session.setAttribute("isPoulain", String.valueOf(user.isPoulain())); session.setAttribute("isMentor", String.valueOf(user.isMentor())); session.setAttribute("isAdmin", String.valueOf(user.isAdmin())); } ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("login"); // resources/template/login.html return modelAndView; return new ModelAndView("redirect:/"); } @RequestMapping(value = {"/index", "", "/"}, method = RequestMethod.GET) public ModelAndView index(HttpSession session, Principal principal) { public ModelAndView index() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); // resources/template/index.html String email = SecurityContextHolder.getContext().getAuthentication().getName(); User user = userService.findByEmail(email); if (user != null) session.setAttribute("username", user.getUsername()); return modelAndView; } Loading
src/main/java/com/example/demo/model/Role.java +8 −0 Original line number Diff line number Diff line Loading @@ -37,5 +37,13 @@ public class Role { this.name = name; } public String getDesc(){ return desc; } public void setDesc(String desc){ this.desc = desc; } } No newline at end of file
src/main/java/com/example/demo/model/User.java +21 −3 Original line number Diff line number Diff line Loading @@ -92,15 +92,33 @@ public class User { return roles; } public String getRole(){ ArrayList<String> rolesarray = new ArrayList<String>(); public ArrayList<String> getRolesArray(){ ArrayList<String> returnArray = new ArrayList<String>(); for (Role r : roles){ rolesarray.add(r.getName()); returnArray.add(r.getName()); } return returnArray; } public String getRole(){ ArrayList<String> rolesarray = getRolesArray(); return rolesarray.get(0); } public boolean isPoulain(){ return getRolesArray().contains("POULAIN"); } public boolean isMentor(){ return getRolesArray().contains("MENTOR"); } public boolean isAdmin(){ return getRolesArray().contains("ADMIN"); } public void setRoles(Set<Role> roles) { this.roles = roles; } Loading
src/main/resources/templates/allHelpRequest.html +2 −2 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ <body> <header th:insert="nav.html :: header"> </header> <h1>All Help Request</h1> <div th:each="help: ${helpPropositions}"> <div th:each="help: ${helpRequests}"> <div class="row"> <div class="col s12 m6"> <div class="card-panel teal lighten-1"> Loading @@ -22,7 +22,7 @@ <p th:text="${help.poulain.id}"></p> <p th:if="${help.mentor != null}" th:text=" ${help.mentor.id}"></p> <form th:unless="${help.mentor != null}" action="#" th:action="@{/editProposition/{id}(id=${help.id})}"> <form th:unless="${help.mentor != null}" action="#" th:action="@{/editRequest/{id}(id=${help.id})}"> <input class="waves-effect waves-light btn right teal darken-2" type="submit" value="Se proposer"/> </form> Loading