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.base.login;
17  
18  import java.util.function.Function;
19  
20  import javax.annotation.Resource;
21  
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.app.web.RootAction;
24  import org.codelibs.fess.app.web.login.LoginAction;
25  import org.codelibs.fess.entity.FessUser;
26  import org.codelibs.fess.es.user.exbhv.UserBhv;
27  import org.codelibs.fess.exception.UserRoleLoginException;
28  import org.codelibs.fess.mylasta.action.FessUserBean;
29  import org.codelibs.fess.mylasta.direction.FessConfig;
30  import org.codelibs.fess.sso.SsoAuthenticator;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.dbflute.optional.OptionalEntity;
33  import org.dbflute.optional.OptionalThing;
34  import org.lastaflute.core.magic.async.AsyncManager;
35  import org.lastaflute.core.time.TimeManager;
36  import org.lastaflute.web.login.LoginHandlingResource;
37  import org.lastaflute.web.login.PrimaryLoginManager;
38  import org.lastaflute.web.login.TypicalLoginAssist;
39  import org.lastaflute.web.login.credential.LoginCredential;
40  import org.lastaflute.web.login.exception.LoginRequiredException;
41  import org.lastaflute.web.login.option.LoginSpecifiedOption;
42  import org.lastaflute.web.servlet.session.SessionManager;
43  
44  /**
45   * @author jflute
46   * @author shinsuke
47   */
48  public class FessLoginAssist extends TypicalLoginAssist<String, FessUserBean, FessUser> // #change_it also UserBean
49          implements PrimaryLoginManager {
50  
51      // ===================================================================================
52      //                                                                           Attribute
53      //                                                                           =========
54      @Resource
55      private TimeManager timeManager;
56      @Resource
57      private AsyncManager asyncManager;
58      @Resource
59      private SessionManager sessionManager;
60      @Resource
61      private FessConfig fessConfig;
62      @Resource
63      private UserBhv userBhv;
64  
65      // ===================================================================================
66      //                                                                           Find User
67      //                                                                           =========
68      @Override
69      public boolean checkUserLoginable(final LoginCredential credential) {
70          throw new UnsupportedOperationException("checkUserLoginable is not supported.");
71      }
72  
73      @Override
74      protected void checkCredential(final TypicalLoginAssist<String, FessUserBean, FessUser>.CredentialChecker checker) {
75          throw new UnsupportedOperationException("checkCredential is not supported.");
76      }
77  
78      @Override
79      protected OptionalEntity<FessUser> doFindLoginUser(final String username) {
80          return userBhv.selectEntity(cb -> {
81              cb.query().setName_Equal(username);
82          }).map(user -> (FessUser) user);
83      }
84  
85      // ===================================================================================
86      //                                                                       Login Process
87      //                                                                       =============
88      @Override
89      protected FessUserBean createUserBean(final FessUser user) {
90          return new FessUserBean(user);
91      }
92  
93      @Override
94      protected OptionalThing<String> getCookieRememberMeKey() {
95          // example to use remember-me
96          //return OptionalThing.of(fessConfig.getCookieRememberMeFessKey());
97          return OptionalThing.empty();
98      }
99  
100     @Override
101     protected void saveLoginHistory(final FessUser user, final FessUserBean userBean, final LoginSpecifiedOption option) {
102         asyncManager.async(() -> {
103             insertLogin(user);
104         });
105     }
106 
107     protected void insertLogin(final Object member) {
108     }
109 
110     @Override
111     protected void checkPermission(final LoginHandlingResource resource) throws LoginRequiredException {
112         if (resource.getActionClass().getName().startsWith(Constants.ADMIN_PACKAGE)) {
113             getSavedUserBean().ifPresent(user -> {
114                 if (!user.hasRoles(fessConfig.getAuthenticationAdminRolesAsArray())) {
115                     throw new UserRoleLoginException(RootAction.class);
116                 }
117             });
118         }
119     }
120 
121     // ===================================================================================
122     //                                                                      Login Resource
123     //                                                                      ==============
124     @Override
125     protected Class<FessUserBean> getUserBeanType() {
126         return FessUserBean.class;
127     }
128 
129     @Override
130     protected Class<?> getLoginActionType() {
131         return LoginAction.class;
132     }
133 
134     @Override
135     protected String toTypedUserId(final String userKey) {
136         return userKey;
137     }
138 
139     // ===================================================================================
140     //                                                                     Login Extension
141     //                                                                      ==============
142 
143     @Override
144     protected void resolveCredential(final CredentialResolver resolver) {
145         resolver.resolve(LocalUserCredential.class, credential -> {
146             final LocalUserCredential userCredential = credential;
147             final String username = userCredential.getUser();
148             final String password = userCredential.getPassword();
149             if (!fessConfig.isAdminUser(username)) {
150                 final OptionalEntity<FessUser> ldapUser = ComponentUtil.getLdapManager().login(username, password);
151                 if (ldapUser.isPresent()) {
152                     return ldapUser;
153                 }
154             }
155             return doFindLoginUser(username, encryptPassword(password));
156         });
157         final LoginCredentialResolver loginResolver = new LoginCredentialResolver(resolver);
158         for (final SsoAuthenticator auth : ComponentUtil.getSsoManager().getAuthenticators()) {
159             auth.resolveCredential(loginResolver);
160         }
161     }
162 
163     public static class LoginCredentialResolver {
164         private final TypicalLoginAssist<String, FessUserBean, FessUser>.CredentialResolver resolver;
165 
166         public LoginCredentialResolver(final CredentialResolver resolver) {
167             this.resolver = resolver;
168         }
169 
170         public <CREDENTIAL extends LoginCredential> void resolve(final Class<CREDENTIAL> credentialType,
171                 final Function<CREDENTIAL, OptionalEntity<FessUser>> oneArgLambda) {
172             resolver.resolve(credentialType, credential -> oneArgLambda.apply(credential));
173         }
174     }
175 
176     protected OptionalEntity<FessUser> doFindLoginUser(final String username, final String cipheredPassword) {
177         return userBhv.selectEntity(cb -> {
178             cb.query().setName_Equal(username);
179             cb.query().setPassword_Equal(cipheredPassword);
180         }).map(user -> (FessUser) user);
181     }
182 }