Skip to content

Commit dd90ca4

Browse files
author
Sakai Developer
committed
coderabbit suggestions
1 parent 36bd764 commit dd90ca4

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

polls/api/src/java/org/sakaiproject/poll/logic/PollListManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public interface PollListManager extends EntityProducer {
100100
*/
101101
public Option getOptionById(Long optionId);
102102

103+
/**
104+
* Populate and persist a UUID for the supplied option when one is missing.
105+
*
106+
* @param option the option to update; may be {@code null}
107+
* @return the same option instance after ensuring a UUID
108+
*/
109+
public Option ensureOptionUuidAndSave(Option option);
110+
103111
/**
104112
* Get all options for a specific poll
105113
* @param pollId the id for a poll

polls/api/src/java/org/sakaiproject/poll/model/Vote.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ public Vote() {
9696
}
9797

9898
public Vote(Poll poll, Option option, String subId, Instant voteDate, String userId, String ip) {
99+
if (poll == null) {
100+
throw new IllegalArgumentException("Poll must not be null when creating a vote");
101+
}
102+
if (option == null) {
103+
throw new IllegalArgumentException("Option must not be null when creating a vote");
104+
}
105+
Long pollIdValue = poll.getPollId();
106+
Long optionPollId = option.getPollId();
107+
if (optionPollId == null && option.getPoll() != null) {
108+
optionPollId = option.getPoll().getPollId();
109+
}
110+
if (pollIdValue != null && optionPollId != null && !pollIdValue.equals(optionPollId)) {
111+
throw new IllegalArgumentException(String.format("Option %s does not belong to poll %s", String.valueOf(option.getOptionId()), String.valueOf(pollIdValue)));
112+
}
99113
this.pollId = poll.getPollId();
100114
this.pollOption = option.getOptionId();
101115
this.poll = poll;

polls/impl/src/java/org/sakaiproject/poll/service/impl/PollListManagerImpl.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,8 @@ public boolean savePoll(Poll t) throws SecurityException, IllegalArgumentExcepti
141141

142142
if (t.getPollId() == null) {
143143
newPoll = true;
144-
String generatedUuid = null;
145-
if (idManager != null) {
146-
generatedUuid = idManager.createUuid();
147-
}
148-
if (generatedUuid == null || generatedUuid.trim().isEmpty()) {
144+
String generatedUuid = idManager.createUuid();
145+
if (StringUtils.isBlank(generatedUuid)) {
149146
generatedUuid = UUID.randomUUID().toString();
150147
}
151148
t.setUuid(generatedUuid);
@@ -290,10 +287,17 @@ public Poll getPollWithVotes(Long pollId) {
290287
}
291288

292289
public Option getOptionById(Long optionId) {
293-
Option option = optionRepository.findById(optionId).orElse(null);
294-
if (option != null && option.getUuid() == null) {
290+
return optionRepository.findById(optionId).orElse(null);
291+
}
292+
293+
public Option ensureOptionUuidAndSave(Option option) {
294+
if (option == null) {
295+
return null;
296+
}
297+
if (StringUtils.isBlank(option.getUuid())) {
295298
option.setUuid(UUID.randomUUID().toString());
296-
saveOption(option);
299+
option = optionRepository.save(option);
300+
log.debug("Option id {} assigned UUID {}", option.getOptionId(), option.getUuid());
297301
}
298302
return option;
299303
}
@@ -314,12 +318,17 @@ public void deleteOption(Option option, boolean soft) {
314318
}
315319

316320
public boolean saveOption(Option t) {
317-
if (t.getUuid() == null || t.getUuid().trim().isEmpty()) {
318-
t.setUuid( UUID.randomUUID().toString() );
321+
if (t == null) {
322+
throw new IllegalArgumentException("Option cannot be null when saving");
319323
}
320324

321-
optionRepository.save(t);
322-
log.debug("Option {} successfully saved", t.toString());
325+
Option persisted;
326+
if (StringUtils.isBlank(t.getUuid())) {
327+
persisted = ensureOptionUuidAndSave(t);
328+
} else {
329+
persisted = optionRepository.save(t);
330+
}
331+
log.debug("Option {} successfully saved", persisted);
323332
return true;
324333
}
325334

@@ -383,6 +392,7 @@ public String archive(String siteId, Document doc, Stack<Element> stack, String
383392
List<Option> options = getVisibleOptionsForPoll(poll.getPollId());
384393

385394
for (Option option : options) {
395+
option = ensureOptionUuidAndSave(option);
386396
Element el2 = PollUtil.optionToXml(option, doc, stack);
387397
el.appendChild(el2);
388398
}

polls/tool/src/java/org/sakaiproject/poll/tool/mvc/VoteController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ public String showVote(@RequestParam("pollId") Long pollId,
5858
Model model,
5959
Locale locale,
6060
RedirectAttributes redirectAttributes) {
61-
Poll poll = pollListManager.getPollById(pollId);
61+
Poll poll;
62+
try {
63+
poll = pollListManager.getPollById(pollId);
64+
} catch (SecurityException e) {
65+
redirectAttributes.addFlashAttribute("alert", messageSource.getMessage("vote_noperm.voteCollection", null, locale));
66+
return "redirect:/faces/votePolls";
67+
}
6268
if (poll == null) {
6369
redirectAttributes.addFlashAttribute("alert", messageSource.getMessage("vote_noperm.voteCollection", null, locale));
6470
return "redirect:/faces/votePolls";

polls/tool/src/webapp/WEB-INF/templates/polls/edit.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<h1 class="h3 mb-3" th:text="${isNew} ? #{new_poll_title} : #{new_poll_title_edit}">Poll</h1>
2525

2626
<form th:action="@{/faces/voteAdd}" th:object="${pollForm}" method="post" class="needs-validation" novalidate>
27+
<input type="hidden" th:if="${_csrf != null}" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
2728
<input type="hidden" th:field="*{pollId}" />
2829

2930
<div class="mb-3">

polls/tool/src/webapp/WEB-INF/templates/polls/option-delete.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ <h1 class="h4 mb-3" th:text="#{delete_confirm}">Confirm delete</h1>
2525
</div>
2626

2727
<form th:action="@{/faces/pollOptionDelete}" method="post">
28+
<input type="hidden" th:if="${_csrf != null}" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
2829
<input type="hidden" name="optionId" th:value="${option.optionId}" />
2930

3031
<div class="mb-3" th:if="${hasVotes}">

0 commit comments

Comments
 (0)