Мне нужен пользователь, чтобы иметь возможность выставлять оценки только за рецензию, если пользователь авторизован И ИД пользователя не совпадает с ИД автора рецензии. Как добавить два определения безопасности, в которых другое сравнивает идентификатор пользователя?
Вот мой html:
<td class="rating-tab">
<span th:text="${ratingToUse}"></span>
<form sec:authorize="isAuthenticated()"
action="#"
th:action="@{/addrating}"
method="post" th:object="${newRating}" class="mt-5">
<input value="0" type="hidden" class="rating" data-glyphicon="0"
th:field="*{stars}">
<input type="hidden" id="reviewID" name="reviewID" th:value="${oneReview.reviewId}">
<button class="btn btn-info" type="submit" style="margin-top: 10px;">Submit my rating</button>
</form>
</td>
, а вот мой контроллер для просмотра и оценки:
@GetMapping(value = "/review/add/{id}")
public ModelAndView getReviewView(@PathVariable String id, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
ImdbMovieData thisMovie = imdbAPIService.getOneMovieOnly(id);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User customUser = (User)authentication.getPrincipal();
int userId = customUser.getId();
String userName = customUser.getUserName();
modelAndView.addObject("review", new Review(userName, userId));
modelAndView.addObject("thisMovie", thisMovie);
modelAndView.setViewName("add-review");
return modelAndView;
}
и
@PostMapping(value = "/addrating")
public ModelAndView addRating(@Valid Rating ratingToAdd, BindingResult bindingResult, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
Review thisReview = reviewService.findByReviewID(ratingToAdd.getReviewID());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User customUser = (User)authentication.getPrincipal();
int userId = customUser.getId();
if(userId == thisReview.getUserId()){
modelAndView.setViewName("redirect:read-review/" + ratingToAdd.getReviewID());
return modelAndView;
}
if (!bindingResult.hasErrors()) {
ratingService.saveRating(ratingToAdd);
}
Review reviewToUpdate = reviewService.findByReviewID(ratingToAdd.getReviewID());
if (reviewToUpdate.totalRatingCount == null && reviewToUpdate.totalRatingSum == null) {
reviewToUpdate.totalRatingCount = 1;
reviewToUpdate.totalRatingSum = ratingToAdd.getStars();
}
else {
reviewToUpdate.totalRatingCount++;
reviewToUpdate.totalRatingSum += ratingToAdd.getStars();
}
reviewService.saveReview(reviewToUpdate);
modelAndView.setViewName("redirect:read-review/" + ratingToAdd.getReviewID());
return modelAndView;
}