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 org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
20 import org
.springframework
.mail
.MailException
;
21 import org
.springframework
.util
.concurrent
.ListenableFuture
;
22 import org
.vaadin
.spring
.events
.EventBus
;
23 import org
.vaadin
.spring
.events
.annotation
.EventBusListenerMethod
;
25 import com
.vaadin
.spring
.annotation
.SpringComponent
;
26 import com
.vaadin
.spring
.annotation
.ViewScope
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
29 import eu
.etaxonomy
.cdm
.api
.security
.IAbstractRequestTokenStore
;
30 import eu
.etaxonomy
.cdm
.api
.security
.PasswordResetRequest
;
31 import eu
.etaxonomy
.cdm
.api
.service
.security
.AccountSelfManagementException
;
32 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
33 import eu
.etaxonomy
.cdm
.vaadin
.event
.UserAccountEvent
;
34 import eu
.etaxonomy
.vaadin
.mvp
.AbstractPresenter
;
37 * @author a.kohlbecker
42 public class PasswordResetPresenter
extends AbstractPresenter
<PasswordResetView
> {
44 private static final long serialVersionUID
= 2656148780493202130L;
47 @Qualifier("cdmRepository")
48 private ICdmRepository repo
;
51 @Qualifier("passwordResetTokenStore")
52 private IAbstractRequestTokenStore
<PasswordResetRequest
, User
> tokenStore
;
54 protected EventBus
.UIEventBus uiEventBus
;
56 PasswordResetRequest resetRequest
= null;
59 protected void setUIEventBus(EventBus
.UIEventBus uiEventBus
){
60 this.uiEventBus
= uiEventBus
;
61 uiEventBus
.subscribe(this);
65 public void handleViewEntered() {
67 boolean debug
= false;
69 getView().setUserName("debug-user");
71 List
<String
> viewParameters
= getNavigationManager().getCurrentViewParameters();
72 if(viewParameters
.size() != 1 || !tokenStore
.isEligibleToken(viewParameters
.get(0))) {
73 // invalid token show error
74 getView().showErrorMessage("Invalid token");
76 Optional
<PasswordResetRequest
> resetRequestOpt
= tokenStore
.findRequest(viewParameters
.get(0));
77 if(resetRequestOpt
.isPresent()) {
78 resetRequest
= resetRequestOpt
.get();
79 getView().setUserName(resetRequest
.getUserName());
84 @EventBusListenerMethod
85 public void onPasswordRevoveryEvent(UserAccountEvent event
) throws AccountSelfManagementException
, ExecutionException
{
87 if(event
.getAction().equals(UserAccountEvent
.UserAccountAction
.RESET_PASSWORD
)) {
88 String newPassword
= getView().getPassword1Field().getValue();
90 CountDownLatch passwordChangedSignal
= new CountDownLatch(1);
91 List
<Throwable
> asyncException
= new ArrayList
<>(1);
92 ListenableFuture
<Boolean
> resetPasswordFuture
= repo
.getPasswordResetService().resetPassword(resetRequest
.getToken(), newPassword
);
93 resetPasswordFuture
.addCallback(requestSuccessVal
-> {
94 passwordChangedSignal
.countDown();
95 }, futureException
-> {
96 asyncException
.add(futureException
);
97 passwordChangedSignal
.countDown();
99 // -- wait for passwordResetService.resetPassword to complete
100 boolean asyncTimeout
= false;
101 Boolean result
= false;
103 passwordChangedSignal
.await(2, TimeUnit
.SECONDS
);
104 result
= resetPasswordFuture
.get();
105 } catch (InterruptedException e
) {
108 if(!asyncException
.isEmpty()) {
109 if(asyncException
.get(0) instanceof MailException
) {
110 getView().showSuccessMessage("Your password has been changed but sending the confirmation email has failed.");
111 } else if(asyncException
.get(0) instanceof AccountSelfManagementException
) {
112 getView().showErrorMessage("The password reset token has beceome invalid. Please request gain for a password reset.");
115 if(!asyncTimeout
&& result
) {
116 getView().showSuccessMessage("Your password has been changed and a confirmation email has been sent to you.");
118 getView().showErrorMessage("A timeout has occured, please try again.");