ref #9877, ref #9498 LoginDialog Tab for registration request ready and account regis...
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Mon, 22 Nov 2021 16:56:26 +0000 (17:56 +0100)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Mon, 22 Nov 2021 16:56:26 +0000 (17:56 +0100)
src/main/java/eu/etaxonomy/cdm/vaadin/component/LoginDialog.java
src/main/java/eu/etaxonomy/cdm/vaadin/design/LoginDialogDesign.java
src/main/java/eu/etaxonomy/cdm/vaadin/event/RegisterNewUserEvent.java [deleted file]
src/main/java/eu/etaxonomy/cdm/vaadin/view/LoginPresenter.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/LoginViewBean.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/PasswordResetPresenter.java
src/main/resources/eu/etaxonomy/cdm/vaadin/design/LoginDialogDesign.html

index 3d0708ecffae5fa845f91ad5eda5b42fd6489d62..27df90658e2102d2d10bec1b75b6bfacf80114dd 100644 (file)
@@ -28,8 +28,6 @@ public class LoginDialog extends LoginDialogDesign {
     public LoginDialog() {
         addSelectedTabChangeListener(e -> focusFirstElement(e.getTabSheet()));
         getLoginMessageLabel().setVisible(false);
-//        getTab(1).setEnabled(false);
-//        getTab(2).setEnabled(false);
         focusFirstElement(this);
     }
 
@@ -43,7 +41,7 @@ public class LoginDialog extends LoginDialogDesign {
             getUserName().focus();
             break;
         case 1: // Register
-            getUserNameSuggestion().focus();
+            getEmail().focus();
             break;
         case 2: // Password Recovery
             getUserNameOrEmail().focus();
@@ -83,13 +81,6 @@ public class LoginDialog extends LoginDialogDesign {
         return cancelLoginButton;
     }
 
-    /**
-     * @return the userNameSuggestion
-     */
-    public TextField getUserNameSuggestion() {
-        return userNameSuggestion;
-    }
-
     /**
      * @return the email
      */
@@ -129,6 +120,10 @@ public class LoginDialog extends LoginDialogDesign {
         return messageLogin;
     }
 
+    public Label getRegisterMessageLabel() {
+        return registerMessage;
+    }
+
     public Label getMessageSendRecoveryEmailLabel() {
         return messageRecoveryEmail;
     }
index 4d7efc21f0d002e0a06e94e8a4a4d6a02d080fe5..c56b82b87a1a2755e4ebfc2b6f945057c5d06553 100644 (file)
@@ -26,9 +26,9 @@ public class LoginDialogDesign extends TabSheet {
     protected Label messageLogin;
     protected Button loginButton;
     protected Button cancelLoginButton;
-    protected TextField userNameSuggestion;
     protected TextField email;
     protected Button registerButton;
+    protected Label registerMessage;
     protected Button cancelRegistrationButton;
     protected TextField userNameOrEmail;
     protected Button sendRecoveryEmail;
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/event/RegisterNewUserEvent.java b/src/main/java/eu/etaxonomy/cdm/vaadin/event/RegisterNewUserEvent.java
deleted file mode 100644 (file)
index 552d23c..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
-* Copyright (C) 2017 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
-package eu.etaxonomy.cdm.vaadin.event;
-
-import com.vaadin.ui.Button.ClickEvent;
-
-/**
- * @author a.kohlbecker
- * @since Apr 25, 2017
- *
- */
-public class RegisterNewUserEvent {
-
-    ClickEvent e;
-
-    /**
-     * @param e
-     */
-    public RegisterNewUserEvent(ClickEvent e) {
-        this.e = e;
-    }
-
-}
index 191d241dd790f567eb93dace6695262697b3250e..79a3c236772bf886ac85608334c16ff5a75b52e5 100644 (file)
@@ -16,10 +16,13 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 
+import javax.mail.internet.AddressException;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.mail.MailException;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
@@ -30,11 +33,13 @@ import org.vaadin.spring.events.EventBus;
 import org.vaadin.spring.events.EventBusListener;
 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
 
+import com.vaadin.data.validator.AbstractStringValidator;
 import com.vaadin.spring.annotation.SpringComponent;
 import com.vaadin.spring.annotation.ViewScope;
 import com.vaadin.ui.themes.ValoTheme;
 
 import eu.etaxonomy.cdm.api.application.ICdmRepository;
+import eu.etaxonomy.cdm.api.service.security.AccountSelfManagementException;
 import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
 import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
 import eu.etaxonomy.cdm.vaadin.event.UserAccountEvent;
@@ -135,6 +140,14 @@ public class LoginPresenter extends AbstractPresenter<LoginView> implements Even
             log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
             authenticate(System.getProperty(PROPNAME_USER), System.getProperty(PROPNAME_PASSWORD));
         }
+
+        getView().getLoginDialog().getEmail().addValidator(new AbstractStringValidator("An account for this email address already exits. You may want to use the \"Password Revovery\" tab intsead?") {
+            private static final long serialVersionUID = 1L;
+            @Override
+            protected boolean isValidValue(String value) {
+                return !repo.getAccountRegistrationService().emailAddressExists(value);
+            }
+        });
     }
 
     @Override
@@ -147,17 +160,19 @@ public class LoginPresenter extends AbstractPresenter<LoginView> implements Even
     }
 
     @EventBusListenerMethod
-    public void onPasswordRevoveryEvent(UserAccountEvent event) throws MalformedURLException, ExecutionException {
+    public void onPasswordRevoveryEvent(UserAccountEvent event) throws MalformedURLException, ExecutionException, MailException, AddressException, AccountSelfManagementException {
 
         if(event.getAction().equals(UserAccountEvent.UserAccountAction.REQUEST_PASSWORD_RESET)) {
             requestPasswordReset();
+        } else if(event.getAction().equals(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT)) {
+            requestAccountCreation();
         }
     }
 
     private void requestPasswordReset() throws MalformedURLException, ExecutionException {
         String userNameOrEmail = getView().getLoginDialog().getUserNameOrEmail().getValue();
         URL servletBaseUrl = VaadinServletUtilities.getServletBaseUrl();
-        logger.debug("PasswordRevoveryEvent for " + servletBaseUrl + ", userNameOrEmail:" + userNameOrEmail);
+        logger.debug("UserAccountAction.REQUEST_PASSWORD_RESET for " + servletBaseUrl + ", userNameOrEmail:" + userNameOrEmail);
         // Implementation note: UI modifications allied in the below callback methods will not affect the UI
         // immediately, therefore we use a CountDownLatch
         CountDownLatch finshedSignal = new CountDownLatch(1);
@@ -185,7 +200,7 @@ public class LoginPresenter extends AbstractPresenter<LoginView> implements Even
         }
         if(!asyncException.isEmpty()) {
             getView().getLoginDialog().getMessageSendRecoveryEmailLabel()
-            .setValue("Sending an password reset email to you has failed. Please try again later or contect the support in case this error persists.");
+            .setValue("Sending the password reset email to you has failed. Please try again later or contect the support in case this error persists.");
             getView().getLoginDialog().getMessageSendRecoveryEmailLabel().setStyleName(ValoTheme.LABEL_FAILURE);
         } else {
             if(!asyncTimeout && result) {
@@ -202,4 +217,50 @@ public class LoginPresenter extends AbstractPresenter<LoginView> implements Even
         }
     }
 
+    private void requestAccountCreation() throws MalformedURLException, MailException, AddressException, AccountSelfManagementException, ExecutionException {
+        String emailAddress = getView().getLoginDialog().getEmail().getValue();
+        URL servletBaseUrl = VaadinServletUtilities.getServletBaseUrl();
+
+        logger.debug("UserAccountAction.REGISTER_ACCOUNT for " + servletBaseUrl + ", emailAddress:" + emailAddress);
+
+        CountDownLatch finshedSignal = new CountDownLatch(1);
+        List<Throwable> asyncException = new ArrayList<>(1);
+        ListenableFuture<Boolean> futureResult = repo.getAccountRegistrationService().emailAccountRegistrationRequest(emailAddress,
+                servletBaseUrl.toString() + "/app/" + UserAccountSelfManagementUI.PATH + "#!" + AccountRegistrationViewBean.NAME + "/%s");
+        futureResult.addCallback(
+                    successFuture -> {
+                        finshedSignal.countDown();
+                    },
+                    exception -> {
+                        // possible MailException
+                        asyncException.add(exception);
+                        finshedSignal.countDown();
+                    }
+                );
+        boolean asyncTimeout = false;
+        Boolean result = false;
+        try {
+            finshedSignal.await(2, TimeUnit.SECONDS);
+            result = futureResult.get();
+        } catch (InterruptedException e) {
+            asyncTimeout = true;
+        }
+        if(!asyncException.isEmpty()) {
+            getView().getLoginDialog().getRegisterMessageLabel()
+            .setValue("Sending the account resitration email to you has failed. Please try again later or contect the support in case this error persists.");
+            getView().getLoginDialog().getRegisterMessageLabel().setStyleName(ValoTheme.LABEL_FAILURE);
+        } else {
+            if(!asyncTimeout && result) {
+                getView().getLoginDialog().getRegisterMessageLabel().setValue("An email with with further instructions has been sent to you.");
+                getView().getLoginDialog().getRegisterMessageLabel().setStyleName(ValoTheme.LABEL_SUCCESS);
+                getView().getLoginDialog().getEmail().setEnabled(false);
+                getView().getLoginDialog().getRegisterButton().setEnabled(false);
+
+            } else {
+                getView().getLoginDialog().getRegisterMessageLabel().setValue("A timeout has occured, please try again.");
+                getView().getLoginDialog().getRegisterMessageLabel().setStyleName(ValoTheme.LABEL_FAILURE);
+            }
+        }
+    }
+
 }
index c87a633760877dae361f16457f4ecb64473d9ba6..248785af54b28a73b002fddcf7f9885f9d3d4770 100644 (file)
@@ -8,8 +8,10 @@
 */
 package eu.etaxonomy.cdm.vaadin.view;
 
+import org.apache.commons.lang3.StringUtils;
 import org.vaadin.spring.events.EventScope;
 
+import com.vaadin.data.validator.EmailValidator;
 import com.vaadin.data.validator.StringLengthValidator;
 import com.vaadin.event.ShortcutAction.KeyCode;
 import com.vaadin.navigator.View;
@@ -22,7 +24,6 @@ import com.vaadin.ui.themes.ValoTheme;
 
 import eu.etaxonomy.cdm.vaadin.component.LoginDialog;
 import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
-import eu.etaxonomy.cdm.vaadin.event.RegisterNewUserEvent;
 import eu.etaxonomy.cdm.vaadin.event.UserAccountEvent;
 import eu.etaxonomy.vaadin.mvp.AbstractView;
 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
@@ -54,24 +55,36 @@ public class LoginViewBean  extends AbstractView<LoginPresenter> implements Logi
         root.setComponentAlignment(loginDialog, Alignment.MIDDLE_CENTER);
         setCompositionRoot(root);
 
+        // --- login tab
         loginDialog.getLoginButton().addClickListener(e -> handleLoginClick(e));
         loginDialog.getLoginButton().setClickShortcut(KeyCode.ENTER);
-        loginDialog.getRegisterButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new RegisterNewUserEvent(e)));
-        loginDialog.getSendOnetimeLogin().addClickListener(e -> {
-            getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REQUEST_PASSWORD_RESET,e));
-        });
         // NOTE: null viewName will be replaced by the default view name in NavigationManagerBean
         loginDialog.getCancelLoginButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
 
+        // --- Register tab
+        loginDialog.getRegisterButton().addClickListener(e -> {
+                getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT, e));
+            }
+        );
         loginDialog.getCancelRegistrationButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
+        loginDialog.getEmail().addValidator(new EmailValidator("The enterd E-mail address is not valid."));
+        loginDialog.getEmail().addValueChangeListener(e -> updateRegisterButtonState());
+        // further validators added in the presenter
 
+        // --- Password reset tab
         StringLengthValidator nameOrEmailValidator = new StringLengthValidator("Please enter your username or email address.");
         loginDialog.getUserNameOrEmail().addValidator(nameOrEmailValidator);
         loginDialog.getUserNameOrEmail().addTextChangeListener(e -> {
             String text = e.getText();
-            logger.debug("text: " + text);
             loginDialog.getSendOnetimeLogin().setEnabled(text != null && text.length() > 1);
         });
+        loginDialog.getSendOnetimeLogin().addClickListener(e -> {
+            getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REQUEST_PASSWORD_RESET,e));
+        });
+    }
+
+    private void updateRegisterButtonState() {
+        loginDialog.getRegisterButton().setEnabled(StringUtils.isNoneBlank(loginDialog.getEmail().getValue()) && loginDialog.getEmail().getErrorMessage() == null);
     }
 
     private void handleLoginClick(ClickEvent e) {
index 79c00db31533699533c1775afb3f77d5d7d9a1b3..4baf5c3fde028b615efce9addae6305dc07f277e 100644 (file)
@@ -71,7 +71,7 @@ public class PasswordResetPresenter extends AbstractPresenter<PasswordResetView>
                 // invalid token show error
                 getView().showErrorMessage("Invalid token");
             }
-            Optional<PasswordResetRequest> resetRequestOpt = tokenStore.findResetRequest(viewParameters.get(0));
+            Optional<PasswordResetRequest> resetRequestOpt = tokenStore.findRequest(viewParameters.get(0));
             if(resetRequestOpt.isPresent()) {
                 resetRequest = resetRequestOpt.get();
                 getView().setUserName(resetRequest.getUserName());
index 9885879454cd469ec562a863566fe3c715e64f5d..640410e485320d7dc1144408530b6eb1d199048a 100644 (file)
@@ -35,7 +35,6 @@
    </tab>
    <tab caption="Register">
     <vaadin-form-layout size-auto margin>
-     <vaadin-text-field caption="Username" _id="userNameSuggestion"></vaadin-text-field>
      <vaadin-text-field caption="Email" _id="email"></vaadin-text-field>
      <vaadin-horizontal-layout spacing width-full>
       <vaadin-button enabled="false" style-name="primary" plain-text _id="registerButton">
@@ -45,7 +44,7 @@
         Cancel 
       </vaadin-button>
      </vaadin-horizontal-layout>
-     <vaadin-label style-name="tiny" width="190px">
+     <vaadin-label style-name="tiny" width="190px" _id="registerMessage">
        After clicking 
       <b class="primary">Register</b> you will receive an E-mail with further instructions. 
      </vaadin-label>