From cf138cd8cff90b7848da059cdc70892744881987 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 17 Dec 2020 03:11:28 +0100 Subject: [PATCH] Must function now Email is down --- .../controller/VotingController.java | 41 ++++++++++++---- .../entities/AuthCode.java | 4 ++ .../table/TableAction.java | 5 +- ...mittedcandidates.css => allreadyVoted.css} | 4 ++ .../resources/static/styles/authenticate.css | 22 +++++++-- .../resources/templates/authenticate.html | 47 +++++++++++++++---- src/main/resources/templates/error.html | 24 ++++++++-- .../templates/errors/alreadyVoted.html | 40 ++++++++++++++-- .../errors/alreadyVotedForMotto.html | 14 ------ .../errors/alreadysubmittedcandidates.html | 44 ----------------- .../templates/errors/notRegistered.html | 20 ++++++-- 11 files changed, 172 insertions(+), 93 deletions(-) rename src/main/resources/static/styles/{alreadysubmittedcandidates.css => allreadyVoted.css} (97%) delete mode 100644 src/main/resources/templates/errors/alreadyVotedForMotto.html delete mode 100644 src/main/resources/templates/errors/alreadysubmittedcandidates.html diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java index dad5b36..69f9923 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/controller/VotingController.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.Logger; import javax.annotation.PostConstruct; import java.util.*; +import java.util.concurrent.ExecutionException; @Controller public class VotingController { @@ -110,16 +111,22 @@ public class VotingController { if (name.strip().toLowerCase().matches("[a-z]+\\.[a-z]+@adolfinum+\\.de$")) { try { LOGGER.warn(name); + try { + Voter voter = voterRepository.findByEmail(name.toLowerCase().strip()); + } catch (Exception e){ + LOGGER.error(name + " is not allowed to vote"); + return "errors/notRegistered.html"; + } Voter voter = voterRepository.findByEmail(name.toLowerCase().strip()); if (voter.getVote_status() && votingPhase) { LOGGER.warn(name + " has already voted"); return "errors/alreadyVoted.html"; } else if (voter.getCandidatesubmit_status() && addingPhase) { LOGGER.warn(name + " has already submitted its candidates"); - return "errors/alreadysubmittedcandidates.html"; + return "errors/alreadyVoted.html"; } else if (voter.getMotto_status() && mottoPhase) { LOGGER.warn(name + " has already chose their motto"); - return "errors/alreadyVotedForMotto.html"; + return "errors/alreadyVoted.html"; } else { if (authCodesRepository.findByName(name) == null) { LOGGER.warn("no code"); @@ -130,30 +137,43 @@ public class VotingController { sendSimpleMessage(name, "Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode()); } model.addAttribute("name", name); + model.addAttribute("codeTime", authCodesRepository.findByName(name).getExpirationTime()); model.addAttribute("codeExpired", false); model.addAttribute("codeFalse", false); return "authenticate.html"; } } catch (Exception e) { e.printStackTrace(); - LOGGER.error(name + " is not allowed to vote"); - return "errors/notRegistered.html"; + tableAction.deleteToken(name, authCodesRepository); + return "error.html"; } } else { return "errors/falseInput.html"; } } + @RequestMapping("/newCode") + public String provideNewCode(@RequestParam String name, Model model){ + AuthCode authCode = tableAction.generateToken(name, RandomNumber.getRandomNumberString(), authCodesRepository); + sendSimpleMessage(name, "Code zur Authentifizierung", "Dein Code lautet: " + authCode.getCode()); + model.addAttribute("name", name); + model.addAttribute("codeTime", authCodesRepository.findByName(name).getExpirationTime()); + model.addAttribute("codeExpired", false); + model.addAttribute("codeFalse", false); + return "authenticate.html"; + } + @RequestMapping("/vote") public String voting_adding(@RequestParam String code, @RequestParam String name, Model model) { String tokenStatus = tableAction.checkToken(name, code, authCodesRepository); - + LOGGER.warn(code); if (tokenStatus.equals("matched")) { LOGGER.warn("matched"); if (mottoPhase) { List mottos = mottoRepository.findAll(); model.addAttribute("mottos", mottos); model.addAttribute("name", name); + model.addAttribute("code", code); return "mottoVoting.html"; } else if (addingPhase) { PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper(); @@ -164,11 +184,13 @@ public class VotingController { model.addAttribute("categories", categories); model.addAttribute("form", possibleCandidates); model.addAttribute("name", name); + model.addAttribute("code", code); return "addingCandidates.html"; } else if (votingPhase) { List categories = categoryRepository.findAll(); model.addAttribute("categories", categories); model.addAttribute("name", name); + model.addAttribute("code", code); return "voting.html"; } } else if (tokenStatus.equals("expired")) { @@ -188,10 +210,11 @@ public class VotingController { } @RequestMapping("/saveCandidates") - public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates, @RequestParam String name) { + public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates, @RequestParam String name, @RequestParam String code) { if (voterRepository.findByEmail(name).getVote_status()) { return "errors/alreadyVoted.html"; } else { + authCodesRepository.delete(authCodesRepository.findByName(code)); LinkedList posCandidates = possibleCandidates.getPossibleCandidates(); long index = 1; for (PossibleCandidate posCandidate : posCandidates) { @@ -213,11 +236,12 @@ public class VotingController { } @RequestMapping("/saveMotto") - public String mottoSaving(@RequestParam String name, @RequestParam String voteValue) { + public String mottoSaving(@RequestParam String name, @RequestParam String voteValue, @RequestParam String code) { LOGGER.info(name); if (voterRepository.findByEmail(name).getMotto_status()) { return "errors/alreadySubmitted.html"; } else { + authCodesRepository.delete(authCodesRepository.findByName(code)); tableAction.voteForMotto(voteValue, mottoRepository); tableAction.updateMottoStatus(name, voterRepository); LOGGER.info(name + " has choose his motto"); @@ -226,10 +250,11 @@ public class VotingController { } @RequestMapping("/processVote") - public String ProcessVote(@RequestParam String name, @RequestParam String voteValues) { + public String ProcessVote(@RequestParam String name, @RequestParam String voteValues, @RequestParam String code) { if (voterRepository.findByEmail(name).getCandidatesubmit_status()) { return "errors/alreadySubmitted.html"; } else { + authCodesRepository.delete(authCodesRepository.findByName(code)); String[] partVoteValues = voteValues.split(","); for (String s : partVoteValues) { tableAction.voteForCandidate(s, candidateRepository); diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/AuthCode.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/AuthCode.java index 1e1db47..846d666 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/AuthCode.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/entities/AuthCode.java @@ -37,6 +37,10 @@ public class AuthCode { return time; } + public long getExpirationTime(){ + return time + 1800*1000; + } + public boolean isExpired(){ return System.currentTimeMillis() >= (time + 1800*1000); } diff --git a/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java b/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java index c2ce93a..c12a984 100644 --- a/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java +++ b/src/main/java/com/github/cato447/AbizeitungVotingSystem/table/TableAction.java @@ -55,7 +55,6 @@ public class TableAction { try { AuthCode authCode = authCodesRepository.findByName(name); if (authCode.getCode().equals(code) && !authCode.isExpired()) { - authCodesRepository.delete(authCode); return "matched"; } else if (authCode.isExpired()) { authCodesRepository.delete(authCode); @@ -67,6 +66,10 @@ public class TableAction { return "wrong"; } + public void deleteToken(String name, AuthCodesRepository authCodesRepository){ + authCodesRepository.delete(authCodesRepository.findByName(name)); + } + private boolean fiveMinutesPassed(Long time){ return System.currentTimeMillis() >= (time + 300*1000); } diff --git a/src/main/resources/static/styles/alreadysubmittedcandidates.css b/src/main/resources/static/styles/allreadyVoted.css similarity index 97% rename from src/main/resources/static/styles/alreadysubmittedcandidates.css rename to src/main/resources/static/styles/allreadyVoted.css index 9e57561..4d4f997 100644 --- a/src/main/resources/static/styles/alreadysubmittedcandidates.css +++ b/src/main/resources/static/styles/allreadyVoted.css @@ -24,6 +24,10 @@ h1 { font-size: 75px; } +h2 { + color: #6d6d78; +} + .submitButton { padding: .25em 0; border: 0; diff --git a/src/main/resources/static/styles/authenticate.css b/src/main/resources/static/styles/authenticate.css index d2178b4..16f0640 100644 --- a/src/main/resources/static/styles/authenticate.css +++ b/src/main/resources/static/styles/authenticate.css @@ -21,9 +21,12 @@ body { } h1 { - margin-top: 5%; color: #FFF; - margin-bottom: 5%; +} + +h1.end { + color: #6d6d78; + margin-bottom: 2%; } h2 { @@ -41,7 +44,7 @@ input { .submitButton { margin-top: 5%; - margin-bottom: 5%; + margin-bottom: 1%; border: 0; outline: 0; background: #bb1515; @@ -54,6 +57,19 @@ input { text-shadow: 0 -2px 0 rgba(0, 0, 0, 0.25), 0 1px 0 rgba(255, 255, 255, 0.2); } +.newCode { + border: 0; + outline: 0; + background: #bb1515; + color: rgba(255, 255, 255, 0.85); + font-size: 1rem; + width: 250px; + letter-spacing: .0625rem; + border-radius: 12px; + box-shadow: 0 3px 5px 1px rgba(0, 0, 0, 0.25); + 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; diff --git a/src/main/resources/templates/authenticate.html b/src/main/resources/templates/authenticate.html index d20b486..f57a703 100644 --- a/src/main/resources/templates/authenticate.html +++ b/src/main/resources/templates/authenticate.html @@ -10,14 +10,9 @@ - -

+

Nicht wundern das kann ein wenig dauern!

+

Bitte gebe den Authentifizierungscode ein

@@ -28,11 +23,18 @@ -
+ + +
+ + +
+ +
-

Der Code ist nicht mehr gültig!
Fordere einen neuen an

+

Der Code ist nicht mehr gültig!

@@ -76,6 +78,33 @@ output.value = code; document.getElementById("passForm").submit(); } + + function updateCounter(time) { + const zeroPad = (num, places) => String(num).padStart(places, '0') + + // Show time remaining now. + showTimeRemaining(); + + var dateFuture = time; + console.log(dateFuture); + + // Set a timer to update the displayed clock every 1000 milliseconds. + var updateTimeLoop = setInterval(showTimeRemaining, 1000); + + function showTimeRemaining() { + var dateNow = Date.now(); + var days = zeroPad(Math.floor((dateFuture - dateNow) / (1000 * 60 * 60 * 24)), 2); + var hours = zeroPad(Math.floor(((dateFuture - dateNow) - days * 1000 * 60 * 60 * 24) / (1000 * 60 * 60)), 2); + var mins = zeroPad(Math.floor(((dateFuture - dateNow) - days * 1000 * 60 * 60 * 24 - hours * 1000 * 60 * 60) / (1000 * 60)), 2); + var secs = zeroPad(Math.floor(((dateFuture - dateNow) - days * 1000 * 60 * 60 * 24 - hours * 1000 * 60 * 60 - mins * 1000 * 60) / 1000), 2); + document.getElementById("time_remain").innerHTML = "Der Code gilt noch für " + mins + ":" + secs; + if (hours + mins + secs == 0) { + document.getElementById("time_remain").innerHTML = "Der Code ist abgelaufen"; + } + } + } + + updateCounter(document.getElementById("time_remain").innerHTML); diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 4e05b4b..dcf2207 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -1,10 +1,24 @@ - + - -

Something went wrong!

-

Our Engineers are on it

- Go Home + + + Title + + + + +
+

Oops irgendwas ist kaputt gegangen

+

Keine Sorge!
Probiers nochmal :D

+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/errors/alreadyVoted.html b/src/main/resources/templates/errors/alreadyVoted.html index e6cb039..ad4dc2b 100644 --- a/src/main/resources/templates/errors/alreadyVoted.html +++ b/src/main/resources/templates/errors/alreadyVoted.html @@ -3,12 +3,44 @@ - Abizeitung 2020/2021 Voting + Title + - -

Jeder darf nur einmal abstimmen!

- Return to Login-Site + +
+

Du hast schon abgestimmt!

+

Nächste Abstimmung in:

+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/errors/alreadyVotedForMotto.html b/src/main/resources/templates/errors/alreadyVotedForMotto.html deleted file mode 100644 index 572e139..0000000 --- a/src/main/resources/templates/errors/alreadyVotedForMotto.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - Title - - - -

Du hast deine Motto schon gewählt!

- Return to Login-Site - - - \ No newline at end of file diff --git a/src/main/resources/templates/errors/alreadysubmittedcandidates.html b/src/main/resources/templates/errors/alreadysubmittedcandidates.html deleted file mode 100644 index 99868c8..0000000 --- a/src/main/resources/templates/errors/alreadysubmittedcandidates.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - Title - - - - -
-

Du hast schon abgestimmt!

-

Nächste Abstimmung in:

- -
- - - - - \ No newline at end of file diff --git a/src/main/resources/templates/errors/notRegistered.html b/src/main/resources/templates/errors/notRegistered.html index 6ef3123..b8ce0b4 100644 --- a/src/main/resources/templates/errors/notRegistered.html +++ b/src/main/resources/templates/errors/notRegistered.html @@ -1,14 +1,24 @@ - + - Abizeitung 2020/2021 Voting + Title + - -

Überprüfe noch einmal die eingegebene E-Mail Adresse Sollte der Fall eintreten, dass du deine E-Mail Adresse richtig eingegeben hast und du Schüler der Q2 bist, schreibe eine Mail an simon.bussmann@adolfinum.de mit dem Betreff LoginFehler

- Return to Login-Site + +
+

Du bist nicht zur Wahl zugelassen!

+

Falls du abstimmen können solltest
schreibe eine Mail an ***REMOVED***

+ +
+ + \ No newline at end of file