2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.view
;
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
;
18 import javax
.mail
.internet
.AddressException
;
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
;
27 import com
.vaadin
.spring
.annotation
.SpringComponent
;
28 import com
.vaadin
.spring
.annotation
.ViewScope
;
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
;
38 * @author a.kohlbecker
43 public class AccountRegistrationPresenter
extends AbstractPresenter
<AccountRegistrationView
> {
45 private static final long serialVersionUID
= 2656148780493202130L;
48 @Qualifier("cdmRepository")
49 private ICdmRepository repo
;
52 @Qualifier("accountCreationRequestTokenStore")
53 private IAbstractRequestTokenStore
<AccountCreationRequest
, Object
> tokenStore
;
55 protected EventBus
.UIEventBus uiEventBus
;
57 AccountCreationRequest accountCreationRequest
= null;
60 protected void setUIEventBus(EventBus
.UIEventBus uiEventBus
){
61 this.uiEventBus
= uiEventBus
;
62 uiEventBus
.subscribe(this);
66 public void handleViewEntered() {
68 boolean debug
= false;
70 getView().getEmailAddress().setValue("debug-user@edit.test");
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");
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);
87 @EventBusListenerMethod
88 public void onRegisterAccountEvent(UserAccountEvent event
) throws AccountSelfManagementException
, ExecutionException
, MailException
, AddressException
{
90 if(event
.getAction().equals(UserAccountEvent
.UserAccountAction
.REGISTER_ACCOUNT
)) {
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();
103 // -- wait for passwordResetService.resetPassword to complete
104 boolean asyncTimeout
= false;
105 Boolean result
= false;
107 passwordChangedSignal
.await(2, TimeUnit
.SECONDS
);
108 result
= resetPasswordFuture
.get();
109 } catch (InterruptedException e
) {
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.");
119 if(!asyncTimeout
&& result
) {
120 getView().showSuccessMessage("Your password has been changed and a confirmation email has been sent to you.");
122 getView().showErrorMessage("A timeout has occured, please try again.");