cleanup
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / AccountRegistrationPresenter.java
1 /**
2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.view;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17
18 import javax.mail.internet.AddressException;
19
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.beans.factory.annotation.Qualifier;
22 import org.springframework.mail.MailException;
23 import org.springframework.util.concurrent.ListenableFuture;
24 import org.vaadin.spring.events.EventBus;
25 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
26
27 import com.vaadin.spring.annotation.SpringComponent;
28 import com.vaadin.spring.annotation.ViewScope;
29
30 import eu.etaxonomy.cdm.api.application.ICdmRepository;
31 import eu.etaxonomy.cdm.api.security.AccountCreationRequest;
32 import eu.etaxonomy.cdm.api.security.IAbstractRequestTokenStore;
33 import eu.etaxonomy.cdm.api.service.security.AccountSelfManagementException;
34 import eu.etaxonomy.cdm.vaadin.event.UserAccountEvent;
35 import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
36
37 /**
38 * @author a.kohlbecker
39 * @since Nov 11, 2021
40 */
41 @SpringComponent
42 @ViewScope
43 public class AccountRegistrationPresenter extends AbstractPresenter<AccountRegistrationView> {
44
45 private static final long serialVersionUID = 2656148780493202130L;
46
47 @Autowired
48 @Qualifier("cdmRepository")
49 private ICdmRepository repo;
50
51 @Autowired
52 @Qualifier("accountCreationRequestTokenStore")
53 private IAbstractRequestTokenStore<AccountCreationRequest, Object> tokenStore;
54
55 protected EventBus.UIEventBus uiEventBus;
56
57 private AccountCreationRequest accountCreationRequest = null;
58
59 @Autowired
60 protected void setUIEventBus(EventBus.UIEventBus uiEventBus){
61 this.uiEventBus = uiEventBus;
62 uiEventBus.subscribe(this);
63 }
64
65 @Override
66 public void handleViewEntered() {
67
68 boolean debug = false;
69 if(debug) {
70 getView().getEmailAddress().setValue("debug-user@edit.test");
71 } else {
72 List<String> viewParameters = getNavigationManager().getCurrentViewParameters();
73 if(viewParameters.size() != 1 || !tokenStore.isEligibleToken(viewParameters.get(0))) {
74 // invalid token show error
75 getView().showErrorMessage("Invalid token");
76 }
77 Optional<AccountCreationRequest> resetRequestOpt = tokenStore.findRequest(viewParameters.get(0));
78 if(resetRequestOpt.isPresent()) {
79 accountCreationRequest = resetRequestOpt.get();
80 getView().getEmailAddress().setReadOnly(false);
81 getView().getEmailAddress().setValue(accountCreationRequest.getUserEmail());
82 getView().getEmailAddress().setReadOnly(true);
83 }
84 }
85 }
86
87 @EventBusListenerMethod
88 public void onRegisterAccountEvent(UserAccountEvent event) throws AccountSelfManagementException, ExecutionException, MailException, AddressException {
89
90 if(event.getAction().equals(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT)) {
91
92 CountDownLatch passwordChangedSignal = new CountDownLatch(1);
93 List<Throwable> asyncException = new ArrayList<>(1);
94 ListenableFuture<Boolean> resetPasswordFuture = repo.getAccountRegistrationService().createUserAccount(accountCreationRequest.getToken(),
95 getView().getUserName().getValue(), getView().getPassword1Field().getValue(),
96 getView().getGivenName().getValue(), getView().getFamilyName().getValue(), getView().getPrefix().getValue());
97 resetPasswordFuture.addCallback(requestSuccessVal -> {
98 passwordChangedSignal.countDown();
99 }, futureException -> {
100 asyncException.add(futureException);
101 passwordChangedSignal.countDown();
102 });
103 // -- wait for passwordResetService.resetPassword to complete
104 boolean asyncTimeout = false;
105 Boolean result = false;
106 try {
107 passwordChangedSignal.await(2, TimeUnit.SECONDS);
108 result = resetPasswordFuture.get();
109 } catch (InterruptedException e) {
110 asyncTimeout = true;
111 }
112 if(!asyncException.isEmpty()) {
113 if(asyncException.get(0) instanceof MailException) {
114 getView().showSuccessMessage("Your password has been changed but sending the confirmation email has failed.");
115 } else if(asyncException.get(0) instanceof AccountSelfManagementException) {
116 getView().showErrorMessage("The password reset token has beceome invalid. Please request gain for a password reset.");
117 }
118 } else {
119 if(!asyncTimeout && result) {
120 getView().showSuccessMessage("Your password has been changed and a confirmation email has been sent to you.");
121 } else {
122 getView().showErrorMessage("A timeout has occured, please try again.");
123 }
124 }
125
126 }
127 }
128 }