View Javadoc
1   /*
2    * Copyright 2012-2019 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  package org.codelibs.fess.app.web.login;
17  
18  import org.codelibs.fess.app.web.base.FessLoginAction;
19  import org.codelibs.fess.app.web.base.login.LocalUserCredential;
20  import org.codelibs.fess.util.RenderDataUtil;
21  import org.dbflute.optional.OptionalThing;
22  import org.lastaflute.web.Execute;
23  import org.lastaflute.web.login.exception.LoginFailureException;
24  import org.lastaflute.web.response.HtmlResponse;
25  
26  public class LoginAction extends FessLoginAction {
27  
28      // ===================================================================================
29      //                                                                       Login Execute
30      //                                                                      ==============
31  
32      @Execute
33      public HtmlResponse index() {
34          return asIndexPage(null).useForm(LoginForm.class);
35      }
36  
37      private HtmlResponse asIndexPage(final LoginForm form) {
38          if (form != null) {
39              form.clearSecurityInfo();
40          }
41          return asHtml(virtualHost(path_Login_IndexJsp)).renderWith(data -> {
42              RenderDataUtil.register(data, "notification", fessConfig.getNotificationLogin());
43              saveToken();
44          });
45      }
46  
47      @Execute
48      public HtmlResponse login(final LoginForm form) {
49          validate(form, messages -> {}, () -> asIndexPage(form));
50          verifyToken(() -> asIndexPage(form));
51          final String username = form.username;
52          final String password = form.password;
53          form.clearSecurityInfo();
54          try {
55              return fessLoginAssist.loginRedirect(new LocalUserCredential(username, password), op -> {}, () -> {
56                  activityHelper.login(getUserBean());
57                  userInfoHelper.deleteUserCodeFromCookie(request);
58                  return getHtmlResponse();
59              });
60          } catch (final LoginFailureException lfe) {
61              activityHelper.loginFailure(OptionalThing.of(new LocalUserCredential(username, password)));
62              throwValidationError(messages -> messages.addErrorsLoginError(GLOBAL), () -> asIndexPage(form));
63          }
64          return redirect(getClass());
65      }
66  
67  }