You know the drill

This commit is contained in:
2020-12-06 01:19:39 +01:00
parent 9531dadd69
commit 7e39d2dab9
9 changed files with 144 additions and 79 deletions
@@ -26,7 +26,7 @@ import java.util.*;
@Controller @Controller
public class VotingController { public class VotingController {
private Boolean candidatesAdded = true; private Boolean candidatesAdded = false;
private static final Logger LOGGER = LogManager.getLogger(VotingController.class); private static final Logger LOGGER = LogManager.getLogger(VotingController.class);
private TableAction tableAction = new TableAction(); private TableAction tableAction = new TableAction();
@@ -51,7 +51,6 @@ public class VotingController {
@PostConstruct @PostConstruct
public void init() { public void init() {
LOGGER.info("setups");
if (voterRepository.findAll().size() == 0) { if (voterRepository.findAll().size() == 0) {
tableAction.setUpVoters(voterRepository); tableAction.setUpVoters(voterRepository);
LOGGER.info("Voters successfully set up"); LOGGER.info("Voters successfully set up");
@@ -86,6 +85,7 @@ public class VotingController {
public String VerifyName(@RequestParam String name, Model model) { public String VerifyName(@RequestParam String name, Model model) {
if (name.strip().toLowerCase().matches("[a-z]+\\.[a-z]+@adolfinum+\\.de$")) { if (name.strip().toLowerCase().matches("[a-z]+\\.[a-z]+@adolfinum+\\.de$")) {
try { try {
LOGGER.warn(name);
Voter voter = voterRepository.findByEmail(name.toLowerCase().strip()); Voter voter = voterRepository.findByEmail(name.toLowerCase().strip());
if (voter.getVote_status()) { if (voter.getVote_status()) {
LOGGER.warn(name + " has already voted"); LOGGER.warn(name + " has already voted");
@@ -97,9 +97,12 @@ public class VotingController {
AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository);
sendSimpleMessage(name,"Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode()); sendSimpleMessage(name,"Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode());
model.addAttribute("name", name); model.addAttribute("name", name);
model.addAttribute("codeExpired", false);
model.addAttribute("codeFalse", false);
return "authenticate.html"; return "authenticate.html";
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
LOGGER.error(name + " is not allowed to vote"); LOGGER.error(name + " is not allowed to vote");
return "errors/notRegistered.html"; return "errors/notRegistered.html";
} }
@@ -109,7 +112,10 @@ public class VotingController {
} }
@RequestMapping("/vote") @RequestMapping("/vote")
public String voting_adding(@RequestParam String name, Model model){ public String voting_adding(@RequestParam String code,@RequestParam String name, Model model){
switch (tableAction.checkToken(name, code, authCodesRepository)){
case "matched":
LOGGER.warn("matched");
if(candidatesAdded) { if(candidatesAdded) {
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
@@ -126,6 +132,20 @@ public class VotingController {
model.addAttribute("name", name); model.addAttribute("name", name);
return "addingCandidates.html"; return "addingCandidates.html";
} }
case "expired":
model.addAttribute("name", name);
model.addAttribute("codeExpired", true);
model.addAttribute("codeFalse", false);
return "authenticate.html";
case "wrong":
model.addAttribute("name", name);
model.addAttribute("codeExpired", false);
model.addAttribute("codeFalse", true);
return "authenticate.html";
}
return "fatalError";
} }
@RequestMapping("/saveCandidates") @RequestMapping("/saveCandidates")
@@ -148,7 +168,7 @@ public class VotingController {
} }
index++; index++;
} }
//tableAction.updateCandidatesubmit_status(voterEmail, voterRepository); tableAction.updateCandidatesubmit_status(name, voterRepository);
return "candidateAddingSuccessful.html"; return "candidateAddingSuccessful.html";
} }
} }
@@ -162,7 +182,7 @@ public class VotingController {
for (String s : partVoteValues) { for (String s : partVoteValues) {
tableAction.voteFor(s, candidateRepository); tableAction.voteFor(s, candidateRepository);
} }
//tableAction.updateVotingStatus(voterEmail, voterRepository); tableAction.updateVotingStatus(name, voterRepository);
LOGGER.info(name + " has voted!"); LOGGER.info(name + " has voted!");
return "voteSuccessful.html"; return "voteSuccessful.html";
} }
@@ -36,4 +36,12 @@ public class AuthCode {
public long getTime() { public long getTime() {
return time; return time;
} }
public void setTime(long time) {
this.time = time;
}
public Long getId() {
return id;
}
} }
@@ -2,6 +2,8 @@ package com.github.cato447.AbizeitungVotingSystem.repositories;
import com.github.cato447.AbizeitungVotingSystem.entities.AuthCode; import com.github.cato447.AbizeitungVotingSystem.entities.AuthCode;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AuthCodesRepository extends JpaRepository<AuthCode, Integer> { public interface AuthCodesRepository extends JpaRepository<AuthCode, Integer> {
@@ -7,6 +7,10 @@ import org.aspectj.weaver.loadtime.definition.LightXMLParser;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*; import java.util.*;
public class TableAction { public class TableAction {
@@ -39,10 +43,13 @@ public class TableAction {
public AuthCode generateToken(String name, String code, AuthCodesRepository authCodesRepository) { public AuthCode generateToken(String name, String code, AuthCodesRepository authCodesRepository) {
AuthCode authCode = new AuthCode(name, code); AuthCode authCode = new AuthCode(name, code);
if (authCodesRepository.findByName(authCode.getName()) != null) { try{
authCodesRepository.findByName(authCode.getName()).setCode(authCode.getCode()); AuthCode existingCode = authCodesRepository.findByName(authCode.getName());
return authCode; existingCode.setCode(code);
} else { existingCode.setTime(System.currentTimeMillis());
authCodesRepository.save(existingCode);
return existingCode;
} catch (Exception e){
authCodesRepository.save(authCode); authCodesRepository.save(authCode);
return authCode; return authCode;
} }
@@ -74,7 +81,8 @@ public class TableAction {
public void setUpVoters(VoterRepository voterRepository){ public void setUpVoters(VoterRepository voterRepository){
try { try {
File emailFile = new File("src/main/resources/Q2_emails.txt"); String path = "src/main/resources/Q2_emails.txt";
File emailFile = new File(path);
Scanner myReader = new Scanner(emailFile); Scanner myReader = new Scanner(emailFile);
ArrayList<Voter> voters = new ArrayList<Voter>(); ArrayList<Voter> voters = new ArrayList<Voter>();
while (myReader.hasNextLine()) { while (myReader.hasNextLine()) {
@@ -104,9 +112,9 @@ public class TableAction {
} }
public void setUpCategories(CategoryRepository categoryRepository){ public void setUpCategories(CategoryRepository categoryRepository){
ArrayList<String> names = new ArrayList<>();
try { try {
File categoryFile = new File("src/main/resources/Categories.txt"); String path = "src/main/resources/Categories.txt";
File categoryFile = new File(path);
Scanner myReader = new Scanner(categoryFile); Scanner myReader = new Scanner(categoryFile);
ArrayList<Category> categories = new ArrayList<Category>(); ArrayList<Category> categories = new ArrayList<Category>();
while (myReader.hasNextLine()) { while (myReader.hasNextLine()) {
+36 -36
View File
@@ -1,36 +1,36 @@
1. Wer wird nie von zuhause ausziehen? (Schüler) Wer wird nie von zuhause ausziehen?
1. Wer ist der/die kreativste Zuspätkommer/in (Schüler) Wer ist der/die kreativste Zuspätkommer/in?
1. Mit wem wird man am ehesten in der Zukunft angeben zur Schule gegangen zu sein? (Schüler) Mit wem wird man am ehesten in der Zukunft angeben zur Schule gegangen zu sein?
1. Wer wohnt im Fittnessstudio? (Schüler) Wer wohnt im Fittnessstudio? (Schüler)
1. Wer ist der coolste Leherer? Wer ist der coolste Leherer?
1. Wer ist die coolste Lehrerin? Wer ist die coolste Lehrerin?
1. Wer macht den besten Matheunterricht? Wer macht den besten Matheunterricht?
1. Welcher Lehrer hat den Beruf verfehlt? Welcher Lehrer hat den Beruf verfehlt?
1. Welcher Lehrer ist nie da? Welcher Lehrer ist nie da?
1. Wer hat das Internet erfunden? Wer hat das Internet erfunden?
1. Wer sponsort Schollin? (Schüler) Wer sponsort Schollin? (Schüler)
1. Wer ist am engagiertesten? (Schüler) Wer ist am engagiertesten? (Schüler)
1. Wer wird Bundeskanzler? (Schüler) Wer wird Bundeskanzler? (Schüler)
1. Wer hat das Kimpeltum gegründet? (Schüler) Wer hat das Kimpeltum gegründet? (Schüler)
1. Wer saß am längsten im Klassenschrank? (Schüler) Wer saß am längsten im Klassenschrank? (Schüler)
1. Wer hat die gößte Sauklaue? (Leherer) Wer hat die gößte Sauklaue? (Leherer)
1. Wer gründet das nächste Google? (Schüler) Wer gründet das nächste Google? (Schüler)
1. Wer kommt am schnellsten in den Knast? (Schüler) Wer kommt am schnellsten in den Knast? (Schüler)
1. Top 3-Pärchen? (Schüler) Top 3-Pärchen? (Schüler)
1. Top 3 Lehrergespanne? (Schüler) Top 3 Lehrergespanne? (Schüler)
1. Wer ist der motiviertester Lehrer? Wer ist der motiviertester Lehrer?
1. Wer ist Google auf zwei Beinen(Schüler)? Wer ist Google auf zwei Beinen(Schüler)?
1. Wer hat den besten Style (Schüler)? Wer hat den besten Style (Schüler)?
1. Wer hat den besten Style (Lehrer)? Wer hat den besten Style (Lehrer)?
1. Wer währe ein gutes Pärchen (Lehrer)? Wer währe ein gutes Pärchen (Lehrer)?
1. Wer trinkt am meisten Kaffe (Schüler)? Wer trinkt am meisten Kaffe (Schüler)?
1. Wer trinkt am meisten Kaffe (Lehrer)? Wer trinkt am meisten Kaffe (Lehrer)?
1. Tischflip? Tischflip?
1. Wer wird Harzer? Wer wird Harzer?
1. Wer hat Wiedererkennungswert (Schüler)? Wer hat Wiedererkennungswert (Schüler)?
1. Wen erkennt man an der Lache (Schüler)? Wen erkennt man an der Lache (Schüler)?
1. Wer bringt die dümmsten Witze? (Lehrer) Wer bringt die dümmsten Witze? (Lehrer)
1. Wer hat den meisten Schwachsinn erzählt? (Lehrer) Wer hat den meisten Schwachsinn erzählt? (Lehrer)
1. Welche Lehrer machen nie das, was die Schüler wollten? (Lehrer) Welche Lehrer machen nie das, was die Schüler wollten? (Lehrer)
1. Wer isst immer? (Lehrer) Wer isst immer? (Lehrer)
1. Wer hat die meisten peinlichen Momente gebracht? Wer hat die meisten peinlichen Momente gebracht?
@@ -25,7 +25,7 @@ h1 {
margin-bottom: 5%; margin-bottom: 5%;
} }
h2.categoryHeader { h2 {
color: #FFF; color: #FFF;
} }
@@ -53,6 +53,11 @@ input {
text-shadow: 0 -2px 0 rgba(0, 0, 0, 0.25), 0 1px 0 rgba(255, 255, 255, 0.2); text-shadow: 0 -2px 0 rgba(0, 0, 0, 0.25), 0 1px 0 rgba(255, 255, 255, 0.2);
} }
.errorCode {
margin-top: 10%;
color: #bb1515;
}
html, html,
body { body {
width: 100%; width: 100%;
@@ -2,7 +2,9 @@ td.voted {
background-color: #f05048; background-color: #f05048;
} }
td.submitted {} td.submitted {
background-color: #dbc95e;
}
td.notVoted { td.notVoted {
background-color: #76ed6b; background-color: #76ed6b;
+24 -3
View File
@@ -35,12 +35,26 @@
if (!this.value && ev.key === "Backspace" && i) $inp.eq(i - 1).focus(); if (!this.value && ev.key === "Backspace" && i) $inp.eq(i - 1).focus();
} }
}); });
function getCode() {
groupInputs = document.querySelectorAll(".passInput");
var code = "";
groupInputs.forEach(input => {
console.log("Input: " + input.value);
code += input.value;
});
console.log("Code: " + code);
output = document.getElementById("authCode");
output.value = code;
document.getElementById("passForm").submit();
}
</script> </script>
<h1 th:text="|Dir wurde eine Email an ${name} gesendet!|"></h1> <h1 th:text="|Dir wurde ein Code an ${name} gesendet!|"></h1>
<h2>Gebe den enthaltenen Authentifizierungscode ein</h2> <h2>Bitte gebe den Authentifizierungscode ein</h2>
<form action="#" id="passForm" th:action="@{/vote}" method="post"> <form action="#" id="passForm" th:action="@{/vote}" method="post">
<input id="voterName" type="hidden" name="name" th:value="${name}" /> <input id="voterName" type="hidden" name="name" th:value="${name}" />
<input id="authCode" type="hidden" name="code" value="" />
<input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}" autofocus> <input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}" autofocus>
<input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}"> <input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}">
<input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}"> <input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}">
@@ -48,8 +62,15 @@
<input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}"> <input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}">
<input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}"> <input type="text" placeholder="•" class="passInput" name="pass[]" maxlength="1" autocomplete="off" required pattern="\d{1}">
<button type="submit" id="signup_button">Abstimmen</button>
</form> </form>
<button class="submitButton" id="signup_button" onclick="getCode()">Abstimmen</button>
<div th:if="${codeExpired}">
<h2 class="errorCode">Der Code ist nicht mehr gültig!<br>Fordere einen neuen an</h2>
</div>
<div th:if="${codeFalse}">
<h2 class="errorCode">Der eingegebene Code ist falsch</h2>
</div>
</body> </body>
+7 -8
View File
@@ -18,28 +18,27 @@
<tr> <tr>
<th>Id</th> <th>Id</th>
<th>E-Mail</th> <th>E-Mail</th>
<th>Status</th>
</tr> </tr>
<tr th:each="voter : ${voters}"> <tr th:each="voter : ${voters}">
<div th:if="${voter.candidatesubmit_status}">
<td class="submitted" th:text="${voter.id}"></td>
<td class="submitted" th:text="${voter.email}"></td>
<td class="submitted" th:text="Candidates submitted"></td>
</div>
<!-- If voter has voted --> <!-- If voter has voted -->
<div th:if="${voter.vote_status}"> <div th:if="${voter.vote_status}">
<td class="voted" th:text="${voter.id}"></td> <td class="voted" th:text="${voter.id}"></td>
<td class="voted" th:text="${voter.email}"></td> <td class="voted" th:text="${voter.email}"></td>
<td class="voted" th:text="Voted"></td>
</div> </div>
<!-- ELSE --> <!-- ELSE -->
<div th:unless="${voter.vote_status}"> <div th:unless="${voter.vote_status}">
<div th:if="${voter.candidatesubmit_status}">
<td class="submitted" th:text="${voter.id}"></td>
<td class="submitted" th:text="${voter.email}"></td>
</div>
<div th:unless="${voter.candidatesubmit_status}">
<td class="notVoted" th:text="${voter.id}"></td> <td class="notVoted" th:text="${voter.id}"></td>
<td class="notVoted" th:text="${voter.email}"></td> <td class="notVoted" th:text="${voter.email}"></td>
<td class="notVoted" th:text="$Not voted"></td> </div>
</div>
</tr> </tr>
</table> </table>
</div> </div>