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