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