View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /**
17   * @author Keiichi Watanabe
18   * @author shinsuke
19   */
20  package org.codelibs.fess.app.web.profile;
21  
22  import javax.annotation.Resource;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.codelibs.fess.app.service.UserService;
27  import org.codelibs.fess.app.web.base.FessSearchAction;
28  import org.codelibs.fess.app.web.base.login.LocalUserCredential;
29  import org.codelibs.fess.app.web.login.LoginAction;
30  import org.codelibs.fess.mylasta.action.FessUserBean;
31  import org.lastaflute.web.Execute;
32  import org.lastaflute.web.response.HtmlResponse;
33  import org.lastaflute.web.validation.VaErrorHook;
34  
35  public class ProfileAction extends FessSearchAction {
36  
37      private static final Logger logger = LogManager.getLogger(ProfileAction.class);
38  
39      // ===================================================================================
40      // Constant
41      //
42  
43      // ===================================================================================
44      // Attribute
45      //
46      @Resource
47      private UserService userService;
48  
49      // ===================================================================================
50      // Hook
51      // ======
52  
53      // ===================================================================================
54      // Search Execute
55      // ==============
56  
57      @Execute
58      public HtmlResponse index() {
59          return asIndexHtml();
60      }
61  
62      @Execute
63      public HtmlResponse changePassword(final ProfileForm form) {
64          final VaErrorHook toIndexPage = () -> {
65              form.clearSecurityInfo();
66              return asIndexHtml();
67          };
68          validatePasswordForm(form, toIndexPage);
69          final String username = getUserBean().map(FessUserBean::getUserId).get();
70          try {
71              userService.changePassword(username, form.newPassword);
72              saveInfo(messages -> messages.addSuccessChangedPassword(GLOBAL));
73          } catch (final Exception e) {
74              logger.error("Failed to change password for {}", username, e);
75              throwValidationError(messages -> messages.addErrorsFailedToChangePassword(GLOBAL), toIndexPage);
76          }
77          return redirect(getClass());
78      }
79  
80      private void validatePasswordForm(final ProfileForm form, final VaErrorHook validationErrorLambda) {
81          validate(form, messages -> {}, validationErrorLambda);
82  
83          if (!form.newPassword.equals(form.confirmNewPassword)) {
84              form.newPassword = null;
85              form.confirmNewPassword = null;
86              throwValidationError(messages -> {
87                  messages.addErrorsInvalidConfirmPassword(GLOBAL);
88              }, validationErrorLambda);
89          }
90  
91          fessLoginAssist.findLoginUser(new LocalUserCredential(getUserBean().get().getUserId(), form.oldPassword)).orElseGet(() -> {
92              throwValidationError(messages -> {
93                  messages.addErrorsNoUserForChangingPassword(GLOBAL);
94              }, validationErrorLambda);
95              return null;
96          });
97      }
98  
99      protected HtmlResponse asIndexHtml() {
100         return getUserBean().map(u -> asHtml(virtualHost(path_Profile_IndexJsp)).useForm(ProfileForm.class))
101                 .orElseGet(() -> redirect(LoginAction.class));
102     }
103 }