Skip to content

Commit 9f33d38

Browse files
author
Christian Wansart
committed
switch user id to uuid cookie, fix user id check
1 parent 97f704c commit 9f33d38

File tree

4 files changed

+59
-47
lines changed

4 files changed

+59
-47
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'de.cwansart'
8-
version = '1.0.0'
8+
version = '1.0.2'
99

1010
java {
1111
toolchain {

src/main/java/de/cwansart/unipoll/VoteController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package de.cwansart.unipoll;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56
import java.util.Optional;
7+
import java.util.UUID;
68

79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.http.HttpStatusCode;
@@ -14,7 +16,9 @@
1416
import org.springframework.web.bind.annotation.RequestParam;
1517
import org.springframework.web.server.ResponseStatusException;
1618

19+
import jakarta.servlet.http.Cookie;
1720
import jakarta.servlet.http.HttpServletRequest;
21+
import jakarta.servlet.http.HttpServletResponse;
1822

1923
class ChoiceElement {
2024
private int id;
@@ -67,10 +71,9 @@ public String show(
6771
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll does not exist");
6872
}
6973

70-
String userId = request.getRemoteAddr();
71-
74+
Optional<Cookie> userIdCookie = Arrays.asList(request.getCookies()).stream().filter(c -> c.getName().equals("unipoll-user-id")).findFirst();
7275
// check if user already voted
73-
if (voteRepo.findByIdAndUserId(id, userId).isPresent()) {
76+
if (userIdCookie.isPresent() && voteRepo.findByPollIdAndUserId(id, userIdCookie.get().getValue()).isPresent()) {
7477
return "redirect:/results?id=" + id + "&v=1";
7578
}
7679
model.addAttribute("choices", poll.get().getChoices());
@@ -84,17 +87,17 @@ public String save(
8487
@RequestParam(name = "id", required = true) long id,
8588
@ModelAttribute("voteForm") VoteForm voteForm,
8689
Model model,
87-
HttpServletRequest request
90+
HttpServletResponse response
8891
) {
8992
Optional<Poll> poll = pollRepo.findById(id);
9093
if (poll.isEmpty()) {
9194
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll does not exist");
9295
}
9396

94-
String userId = request.getRemoteAddr();
97+
String userId = UUID.randomUUID().toString();
9598

9699
// check if user has already voted
97-
if (voteRepo.findByIdAndUserId(id, userId).isPresent()) {
100+
if (voteRepo.findByPollIdAndUserId(id, userId).isPresent()) {
98101
return "redirect:/results?id=" + id + "&v=1";
99102
}
100103

@@ -115,6 +118,9 @@ public String save(
115118
vote.setChoices(choices);
116119
vote.setUserId(userId);
117120
voteRepo.save(vote);
121+
122+
response.addCookie(new Cookie("unipoll-user-id", userId));
123+
118124
return "redirect:/vote?id=" + id;
119125
}
120126
}

src/main/java/de/cwansart/unipoll/VoteRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public interface VoteRepository extends Repository<Vote, Long> {
99
Vote save(Vote vote);
1010
Optional<Vote> findById(Long id);
11-
Optional<Vote> findByIdAndUserId(Long id, String userId);
11+
Optional<Vote> findByPollIdAndUserId(Long pollId, String userId);
1212
List<Vote> findByPollId(Long pollId);
1313
long countById(Long id);
1414
}
Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
11
spring.application.name=unipoll
22

33

4-
5-
6-
7-
8-
9-
10-
11-
12-
4+
########################################################################
5+
# FOR LOCAL DEVELOPMENT
6+
########################################################################
7+
## default connection pool
8+
spring.datasource.hikari.connectionTimeout=20000
9+
spring.datasource.hikari.maximumPoolSize=5
10+
11+
## PostgreSQL
12+
spring.datasource.url=jdbc:postgresql://localhost:5432/unipoll
13+
spring.datasource.username=unipoll
14+
spring.datasource.password=unipoll
15+
# create and drop table, good for testing, production set to none or comment it
16+
spring.jpa.hibernate.ddl-auto=create-drop
17+
18+
#debug=true
1319

1420
#spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL;
1521
#spring.datasource.driverClassName=org.h2.Driver
1622
#spring.datasource.username=sa
1723
#spring.datasource.password=password
1824
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
19-
#
20-
#spring.jpa.show-sql=true
21-
#spring.jpa.properties.hibernate.format_sql=true
22-
#logging.level.org.hibernate.SQL=DEBUG
23-
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
24-
#logging.level.org.hibernate.orm.jdbc.bind=TRACE
25-
#logging.level.org.hibernate.type=TRACE
26-
#
27-
#logging.level.org.hibernate=info
28-
## Statistics and slow queries
29-
#logging.level.org.hibernate.stat=debug
30-
#logging.level.org.hibernate.SQL_SLOW=info
31-
## 2nd Level Cache
32-
#logging.level.org.hibernate.cache=debug
33-
## Direct log messages to stdout
34-
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
35-
#log4j.appender.stdout.Target=System.out
36-
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
37-
#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
38-
## Root logger option
39-
#log4j.rootLogger=INFO, stdout
40-
## Hibernate logging options (INFO only shows startup messages)
41-
#log4j.logger.org.hibernate=INFO
42-
## Log JDBC bind parameter runtime arguments
43-
#log4j.logger.org.hibernate.type=trace
44-
#
45-
#spring.h2.console.enabled=true
46-
#spring.h2.console.path=/h2-console
47-
#spring.h2.console.settings.trace=false
48-
#spring.h2.console.settings.web-allow-others=false
25+
26+
spring.jpa.show-sql=true
27+
spring.jpa.properties.hibernate.format_sql=true
28+
logging.level.org.hibernate.SQL=DEBUG
29+
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
30+
logging.level.org.hibernate.orm.jdbc.bind=TRACE
31+
logging.level.org.hibernate.type=TRACE
32+
33+
logging.level.org.hibernate=info
34+
# Statistics and slow queries
35+
logging.level.org.hibernate.stat=debug
36+
logging.level.org.hibernate.SQL_SLOW=info
37+
# 2nd Level Cache
38+
logging.level.org.hibernate.cache=debug
39+
# Direct log messages to stdout
40+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
41+
log4j.appender.stdout.Target=System.out
42+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
43+
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
44+
# Root logger option
45+
log4j.rootLogger=INFO, stdout
46+
# Hibernate logging options (INFO only shows startup messages)
47+
log4j.logger.org.hibernate=INFO
48+
# Log JDBC bind parameter runtime arguments
49+
log4j.logger.org.hibernate.type=trace
50+
51+
spring.h2.console.enabled=true
52+
spring.h2.console.path=/h2-console
53+
spring.h2.console.settings.trace=false
54+
spring.h2.console.settings.web-allow-others=false

0 commit comments

Comments
 (0)