From 6c73ed51318ac069cd9f616645820cef978dce98 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Thu, 30 Jan 2020 11:34:59 +0200 Subject: [PATCH] Fixed issue when trying to retrieve non-existing user during e-mail confirmation and when the e-mail is confirmed it will show proper message on the frontend (ref #233) --- .../java/eu/eudat/controllers/EmailConfirmation.java | 9 ++++++++- .../logic/managers/EmailConfirmationManager.java | 11 ++++++----- .../email-confirmation.component.ts | 5 +++++ dmp-frontend/src/assets/i18n/en.json | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/EmailConfirmation.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/EmailConfirmation.java index 7a6f11416..d1c9582aa 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/EmailConfirmation.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/EmailConfirmation.java @@ -36,7 +36,11 @@ public class EmailConfirmation { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (HasConfirmedEmailException | TokenExpiredException ex) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); + if (ex instanceof TokenExpiredException) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); + } else { + return ResponseEntity.status(HttpStatus.FOUND).body(new ResponseItem().status(ApiMessageCode.WARN_MESSAGE)); + } } } @@ -48,6 +52,9 @@ public class EmailConfirmation { this.emailConfirmationManager.sendConfirmationEmail(email, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (Exception ex) { + if (ex instanceof HasConfirmedEmailException) { + return ResponseEntity.status(HttpStatus.FOUND).body(new ResponseItem().status(ApiMessageCode.WARN_MESSAGE)); + } return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/EmailConfirmationManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/EmailConfirmationManager.java index 907ceac18..bf26fa3e4 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/EmailConfirmationManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/EmailConfirmationManager.java @@ -34,20 +34,21 @@ public class EmailConfirmationManager { .getDatabaseRepository().getLoginConfirmationEmailDao().asQueryable() .where((builder, root) -> builder.equal(root.get("token"), UUID.fromString(token))).getSingle(); - if (loginConfirmationEmail.getExpiresAt().compareTo(new Date()) < 0) - throw new TokenExpiredException("Token has expired."); - UserInfo user = databaseRepository.getUserInfoDao().asQueryable() .where((builder, root) -> builder.equal(root.get("id"), loginConfirmationEmail.getUserId())).getSingle(); if (user.getEmail() != null) throw new HasConfirmedEmailException("User already has confirmed his Email."); + if (loginConfirmationEmail.getExpiresAt().compareTo(new Date()) < 0) + throw new TokenExpiredException("Token has expired."); + loginConfirmationEmail.setIsConfirmed(true); // Checks if mail is used by another user. If it is, merges the new the old. - UserInfo oldUser = databaseRepository.getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), loginConfirmationEmail.getEmail())).getSingle(); - if (oldUser != null) { + Long existingUsers = databaseRepository.getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), loginConfirmationEmail.getEmail())).count(); + if (existingUsers > 0) { + UserInfo oldUser = databaseRepository.getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), loginConfirmationEmail.getEmail())).getSingle(); mergeNewUserToOld(user, oldUser); expireUserToken(user); databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail); diff --git a/dmp-frontend/src/app/ui/auth/login/email-confirmation/email-confirmation.component.ts b/dmp-frontend/src/app/ui/auth/login/email-confirmation/email-confirmation.component.ts index fb12bf926..317f563fb 100644 --- a/dmp-frontend/src/app/ui/auth/login/email-confirmation/email-confirmation.component.ts +++ b/dmp-frontend/src/app/ui/auth/login/email-confirmation/email-confirmation.component.ts @@ -62,7 +62,12 @@ export class EmailConfirmation extends BaseComponent implements OnInit { } onCallbackError(error: any) { + if (error.status === 302) { + this.uiNotificationService.snackBarNotification(this.language.instant('EMAIL-CONFIRMATION.EMAIL-FOUND'), SnackBarNotificationLevel.Warning); + this.router.navigate(['home']); + } else { this.uiNotificationService.snackBarNotification(this.language.instant('EMAIL-CONFIRMATION.EXPIRED-EMAIL'), SnackBarNotificationLevel.Error); this.router.navigate(['login']); + } } } diff --git a/dmp-frontend/src/assets/i18n/en.json b/dmp-frontend/src/assets/i18n/en.json index a9c709c60..cdad8fbd4 100644 --- a/dmp-frontend/src/assets/i18n/en.json +++ b/dmp-frontend/src/assets/i18n/en.json @@ -146,7 +146,8 @@ "REQUEST-EMAIL-HEADER": "We are almost done! Please fill your e-mail.", "REQUEST-EMAIL-TEXT": "You will need to confirm it to use the application.", "SUBMIT": "Submit", - "SENT-EMAIL-HEADER": "Email was send!" + "SENT-EMAIL-HEADER": "Email was send!", + "EMAIL-FOUND": "Email is already confirmed" }, "HOME": { "DMPS": "DMPs",