Skip to content
Snippets Groups Projects
Commit dee62b1a authored by Joshua Grey's avatar Joshua Grey
Browse files

Merge branch 'search_authorization_quickFix' into 'dev'

Search authorization quick fix

See merge request c22001956/group-6-client-project-2023!37
parents 99ff6f8c 56adeb98
No related branches found
No related tags found
No related merge requests found
...@@ -139,7 +139,7 @@ public class ComplaintRepositoryImpl implements ComplaintRepository{ ...@@ -139,7 +139,7 @@ public class ComplaintRepositoryImpl implements ComplaintRepository{
public List<Complaint> getComplaint(Authentication userAuth, String policyNumber, String title) { public List<Complaint> getComplaint(Authentication userAuth, String policyNumber, String title) {
Collection<? extends GrantedAuthority> userRoles = userAuth.getAuthorities(); Collection<? extends GrantedAuthority> userRoles = userAuth.getAuthorities();
if (userRoles.contains(new SimpleGrantedAuthority("CUSTOMER")) || if (userRoles.contains(new SimpleGrantedAuthority("ADMIN")) ||
(userRoles.contains(new SimpleGrantedAuthority("STAFF")))) { (userRoles.contains(new SimpleGrantedAuthority("STAFF")))) {
String params = ""; String params = "";
......
...@@ -14,13 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -14,13 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
@Controller @Controller
@RequestMapping("/search") @RequestMapping(value = {"/staff/search","/admin/search"})
public class SearchController { public class SearchController {
ComplaintService complaintService; ComplaintService complaintService;
...@@ -30,11 +28,15 @@ public class SearchController { ...@@ -30,11 +28,15 @@ public class SearchController {
@GetMapping("/") @GetMapping("/")
public ModelAndView getSearchPage(Model model){ public ModelAndView getSearchPage(Model model, Authentication authentication){
List<Complaint> complaintList = new ArrayList<>(); List<Complaint> complaintList = new ArrayList<>();
Optional userAuth = authentication.getAuthorities().stream().findFirst();
ModelAndView modelAndView = new ModelAndView("search/searchPage", (Map<String, ?>) model); ModelAndView modelAndView = new ModelAndView("search/searchPage", (Map<String, ?>) model);
model.addAttribute("complaintList", complaintList); model.addAttribute("complaintList", complaintList);
model.addAttribute("userAuth", userAuth.get().toString().toLowerCase());
...@@ -43,8 +45,10 @@ public class SearchController { ...@@ -43,8 +45,10 @@ public class SearchController {
@PostMapping ("/processQuery") @PostMapping ("/processQuery")
public ModelAndView queryBuilder(@RequestParam("policyNumber") String policyNumber, @RequestParam("policyId") String policyId, @RequestParam("title") String title, Model model, Authentication authentication){ public ModelAndView queryBuilder(@RequestParam("policyNumber") String policyNumber, @RequestParam("policyId") String policyId, @RequestParam("title") String title, Model model, Authentication authentication){
System.out.println("test");
List<Complaint> complaintList = new ArrayList<>(); List<Complaint> complaintList = new ArrayList<>();
Optional userAuth = authentication.getAuthorities().stream().findFirst();
if(!policyId.trim().isEmpty()){ if(!policyId.trim().isEmpty()){
...@@ -57,6 +61,8 @@ public class SearchController { ...@@ -57,6 +61,8 @@ public class SearchController {
} }
model.addAttribute("complaintList", complaintList); model.addAttribute("complaintList", complaintList);
model.addAttribute("userAuth", userAuth.get().toString().toLowerCase());
return new ModelAndView("search/searchPage", (Map<String, ?>) model); return new ModelAndView("search/searchPage", (Map<String, ?>) model);
} }
...@@ -71,6 +77,7 @@ public class SearchController { ...@@ -71,6 +77,7 @@ public class SearchController {
} }
model.addAttribute("complaintList", complaintList); model.addAttribute("complaintList", complaintList);
model.addAttribute("userAuth", userAuth.get().toString().toLowerCase());
return new ModelAndView("search/searchPage", (Map<String, ?>) model); return new ModelAndView("search/searchPage", (Map<String, ?>) model);
......
...@@ -29,7 +29,7 @@ public class SecurityConfig { ...@@ -29,7 +29,7 @@ public class SecurityConfig {
public static final String[] ENDPOINTS_AUTHORISED = { public static final String[] ENDPOINTS_AUTHORISED = {
"/complaint/**", "/complaint/**",
"/search/**"
}; };
public static final String[] ENDPOINTS_CUSTOMER = { public static final String[] ENDPOINTS_CUSTOMER = {
......
...@@ -39,14 +39,14 @@ ...@@ -39,14 +39,14 @@
<a class="nav-link" th:href="@{/staff/home}">Home</a> <a class="nav-link" th:href="@{/staff/home}">Home</a>
<a class="nav-link" th:href="@{/staff/complaint}">All Complaints</a> <a class="nav-link" th:href="@{/staff/complaint}">All Complaints</a>
<a class="nav-link" th:href="@{/complaint/form}">New Complaint</a> <a class="nav-link" th:href="@{/complaint/form}">New Complaint</a>
<a class="nav-link" th:href="@{/search/}">Search</a> <a class="nav-link" th:href="@{/staff/search/}">Search</a>
</div> </div>
<div sec:authorize="hasAuthority('ADMIN')" class="navbar-nav"> <div sec:authorize="hasAuthority('ADMIN')" class="navbar-nav">
<a class="nav-link" th:href="@{/admin/home}">Home</a> <a class="nav-link" th:href="@{/admin/home}">Home</a>
<a class="nav-link" th:href="@{/admin/complaint}">All Complaints</a> <a class="nav-link" th:href="@{/admin/complaint}">All Complaints</a>
<a class="nav-link" th:href="@{/complaint/form}">New Complaint</a> <a class="nav-link" th:href="@{/complaint/form}">New Complaint</a>
<a class="nav-link" th:href="@{/search/}">Search</a> <a class="nav-link" th:href="@{/admin/search/}">Search</a>
</div> </div>
......
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>Title</title>
...@@ -10,23 +11,22 @@ ...@@ -10,23 +11,22 @@
<div th:replace="~{fragments/header :: header}"/> <div th:replace="~{fragments/header :: header}"/>
<br> <br>
<h1 class = "text-center fw-bold text-primary-emphasis">Search Page</h1> <h1 class = "text-center fw-bold text-primary-emphasis">Search Page</h1>
<div class="container d-flex justify-content-center">
<form th:action="@{/search/processQuery}" method="post" class = "form-row">
<input type="text" name="policyNumber" id="policyNumber" placeholder="policyNumber"> <div sec:authorize="hasAnyAuthority('STAFF', 'ADMIN')" class="container d-flex justify-content-center">
<input type="text" name="policyId" id="policyId" placeholder="policyId">
<input type="text" name="title" id="title" placeholder="title"> <form th:action="@{'/' + ${userAuth} + '/search/processQuery'}" method="post" class = "form-row">
<button class="btn btn-primary" type="submit">Submit</button>
<input type="text" name="policyNumber" id="policyNumber" placeholder="policyNumber">
<input type="text" name="policyId" id="policyId" placeholder="policyId">
</form> <input type="text" name="title" id="title" placeholder="title">
<button class="btn btn-primary" type="submit">Submit</button>
</div> </form>
</div>
<br>
<div th:if = "${complaintList != null}"> <div th:if = "${complaintList != null}">
...@@ -34,7 +34,8 @@ ...@@ -34,7 +34,8 @@
<div th:each="complaint : ${complaintList}" class="card my-1" style="width: 36rem;"> <div th:each="complaint : ${complaintList}" class="card my-1" style="width: 36rem;">
<a class="d-flex justify-content-around card-body align-items-center" id="complaintItem" <a class="d-flex justify-content-around card-body align-items-center" id="complaintItem"
th:href="@{/customer/complaint/details/{id} (id=${complaint.id})}"> th:href="@{'/' + ${userAuth} + '/complaint/details/' + ${complaint.id}}">
<span th:text="${complaint.title}"></span> <span th:text="${complaint.title}"></span>
<span th:text="${#dates.day(complaint.timeSubmitted)} + ' ' + ${#dates.monthNameShort(complaint.timeSubmitted)} <span th:text="${#dates.day(complaint.timeSubmitted)} + ' ' + ${#dates.monthNameShort(complaint.timeSubmitted)}
+ ' ' + ${#dates.year(complaint.timeSubmitted)}"></span> + ' ' + ${#dates.year(complaint.timeSubmitted)}"></span>
...@@ -43,13 +44,14 @@ ...@@ -43,13 +44,14 @@
class="badge rounded-pill align-middle"></span> class="badge rounded-pill align-middle"></span>
</a> </a>
</div> </div>
</section> </section>
</div> </div>
<div th:unless = "${complaintList != null}"> <!--<div th:unless = "${complaintList != null}">-->
<h2 class = "text-center fw-bold text-danger">No search results found please edit your query and try again.</h2> <!-- <h2 class = "text-center fw-bold text-danger">No search results found please edit your query and try again.</h2>-->
</div> <!--</div>-->
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -43,7 +43,7 @@ public class searchPageTests { ...@@ -43,7 +43,7 @@ public class searchPageTests {
@WithMockUser(username = "staff", authorities = {"STAFF"}) @WithMockUser(username = "staff", authorities = {"STAFF"})
public void shouldReturnSingleComplaintItem() throws Exception { public void shouldReturnSingleComplaintItem() throws Exception {
MvcResult result = mvc MvcResult result = mvc
.perform(post("/search/processQuery").with(csrf()).param("policyId", "2").param("policyNumber","").param("title","")) .perform(post("/staff/search/processQuery").with(csrf()).param("policyId", "2").param("policyNumber","").param("title",""))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
...@@ -70,7 +70,7 @@ public class searchPageTests { ...@@ -70,7 +70,7 @@ public class searchPageTests {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
MvcResult result = mvc MvcResult result = mvc
.perform(post("/search/processQuery").with(csrf()).param("policyId", "").param("policyNumber","user").param("title","")) .perform(post("/staff/search/processQuery").with(csrf()).param("policyId", "").param("policyNumber","user").param("title",""))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment