Skip to content

Commit

Permalink
Refactor ConfirmEmailService to offer verification checking and start…
Browse files Browse the repository at this point in the history
… to use in some places. IQSS#6936
  • Loading branch information
poikilotherm committed Jun 10, 2020
1 parent 75742b9 commit 9673e82
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public void deleteAuthenticatedUser(Object pk) {
if (apiToken != null) {
em.remove(apiToken);
}
// @todo: this should be handed down to the service instead of doing it here.
ConfirmEmailData confirmEmailData = confirmEmailService.findSingleConfirmEmailDataByUser(user);
if (confirmEmailData != null) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public void sendConfirmEmail() {

/**
* Determines whether the button to send a verification email appears on user page
* TODO: cant this be refactored to use confirmEmailService.hasVerifiedEmail(currentUser) ?
* @return
*/
public boolean showVerifyEmailButton() {
Expand All @@ -557,12 +558,11 @@ public boolean showVerifyEmailButton() {
}

public boolean isEmailIsVerified() {

return currentUser.getEmailConfirmed() != null && confirmEmailService.findSingleConfirmEmailDataByUser(currentUser) == null;
return confirmEmailService.hasVerifiedEmail(currentUser);
}

public boolean isEmailNotVerified() {
return currentUser.getEmailConfirmed() == null || confirmEmailService.findSingleConfirmEmailDataByUser(currentUser) != null;
return !confirmEmailService.hasVerifiedEmail(currentUser);
}

public boolean isEmailGrandfathered() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ConfirmEmailServiceBean {
private static final Logger logger = Logger.getLogger(ConfirmEmailServiceBean.class.getCanonicalName());

@EJB
AuthenticationServiceBean dataverseUserService;
AuthenticationServiceBean authenticationService;

@EJB
MailServiceBean mailService;
Expand All @@ -46,6 +46,19 @@ public class ConfirmEmailServiceBean {

@PersistenceContext(unitName = "VDCNet-ejbPU")
private EntityManager em;

/**
* A simple interface to check if a user email has been verified or not.
* @param user
* @return true if verified, false otherwise
*/
public boolean hasVerifiedEmail(AuthenticatedUser user) {
boolean hasTimestamp = user.getEmailConfirmed() != null;
boolean hasNoStaleVerificationTokens = this.findSingleConfirmEmailDataByUser(user) == null;
boolean isVerifiedByAuthProvider = authenticationService.lookupProvider(user).isEmailVerified();

return (hasTimestamp && hasNoStaleVerificationTokens) || isVerifiedByAuthProvider;
}

/**
* Initiate the email confirmation process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {

//ConfirmEmailData

// todo: the deletion should be handed down to the service!
ConfirmEmailData confirmEmailData = ctxt.confirmEmail().findSingleConfirmEmailDataByUser(consumedAU);
if (confirmEmailData != null){
ctxt.em().remove(confirmEmailData);
Expand Down

0 comments on commit 9673e82

Please sign in to comment.