View Javadoc
1   /*
2    * Copyright 2012-2017 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.codelibs.fess.app.service.UserService;
25  import org.codelibs.fess.app.web.base.FessSearchAction;
26  import org.codelibs.fess.app.web.login.LoginAction;
27  import org.lastaflute.web.Execute;
28  import org.lastaflute.web.login.credential.UserPasswordCredential;
29  import org.lastaflute.web.response.HtmlResponse;
30  import org.lastaflute.web.validation.VaErrorHook;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class ProfileAction extends FessSearchAction {
35  
36      private static final Logger logger = LoggerFactory.getLogger(ProfileAction.class);
37  
38      // ===================================================================================
39      // Constant
40      //
41  
42      // ===================================================================================
43      // Attribute
44      //
45      @Resource
46      private UserService userService;
47  
48      // ===================================================================================
49      // Hook
50      // ======
51  
52      // ===================================================================================
53      // Search Execute
54      // ==============
55  
56      @Execute
57      public HtmlResponse index() {
58          return asIndexHtml();
59      }
60  
61      @Execute
62      public HtmlResponse changePassword(final ProfileForm form) {
63          final VaErrorHook toIndexPage = () -> {
64              form.clearSecurityInfo();
65              return asIndexHtml();
66          };
67          validatePasswordForm(form, toIndexPage);
68          final String username = getUserBean().map(u -> u.getUserId()).get();
69          try {
70              userService.changePassword(username, form.newPassword);
71              saveInfo(messages -> messages.addSuccessChangedPassword(GLOBAL));
72          } catch (final Exception e) {
73              logger.error("Failed to change password for " + username, e);
74              throwValidationError(messages -> messages.addErrorsFailedToChangePassword(GLOBAL), toIndexPage);
75          }
76          return redirect(getClass());
77      }
78  
79      private void validatePasswordForm(final ProfileForm form, final VaErrorHook validationErrorLambda) {
80          validate(form, messages -> {}, validationErrorLambda);
81  
82          if (!form.newPassword.equals(form.confirmNewPassword)) {
83              form.newPassword = null;
84              form.confirmNewPassword = null;
85              throwValidationError(messages -> {
86                  messages.addErrorsInvalidConfirmPassword(GLOBAL);
87              }, validationErrorLambda);
88          }
89  
90          fessLoginAssist.findLoginUser(new UserPasswordCredential(getUserBean().get().getUserId(), form.oldPassword)).orElseGet(() -> {
91              throwValidationError(messages -> {
92                  messages.addErrorsNoUserForChangingPassword(GLOBAL);
93              }, validationErrorLambda);
94              return null;
95          });
96      }
97  
98      protected HtmlResponse asIndexHtml() {
99          return getUserBean().map(u -> asHtml(virtualHost(path_Profile_IndexJsp)).useForm(ProfileForm.class)).orElseGet(
100                 () -> redirect(LoginAction.class));
101     }
102 }