View Javadoc
1   /*
2    * Copyright 2012-2021 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.ldap;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.Hashtable;
21  
22  import org.apache.commons.lang3.ArrayUtils;
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.fess.entity.FessUser;
25  import org.codelibs.fess.mylasta.action.FessUserBean;
26  import org.codelibs.fess.mylasta.direction.FessConfig;
27  import org.codelibs.fess.util.ComponentUtil;
28  import org.dbflute.optional.OptionalThing;
29  
30  public class LdapUser implements FessUser {
31  
32      private static final long serialVersionUID = 1L;
33  
34      protected Hashtable<String, String> env;
35  
36      protected String name;
37  
38      protected String[] permissions = null;
39  
40      public LdapUser(final Hashtable<String, String> env, final String name) {
41          this.env = env;
42          this.name = name;
43      }
44  
45      @Override
46      public String getName() {
47          return name;
48      }
49  
50      @Override
51      public String[] getPermissions() {
52          if (permissions == null) {
53              final FessConfig fessConfig = ComponentUtil.getFessConfig();
54              final String baseDn = fessConfig.getLdapBaseDn();
55              final String accountFilter = fessConfig.getLdapAccountFilter();
56              final String groupFilter = fessConfig.getLdapGroupFilter();
57              if (StringUtil.isNotBlank(baseDn) && StringUtil.isNotBlank(accountFilter)) {
58                  final LdapManager ldapManager = ComponentUtil.getLdapManager();
59                  permissions = ArrayUtils.addAll(ldapManager.getRoles(this, baseDn, accountFilter, groupFilter, roles -> {
60                      permissions = roles;
61                      ComponentUtil.getActivityHelper().permissionChanged(OptionalThing.of(new FessUserBean(this)));
62                  }), fessConfig.getRoleSearchUserPrefix() + ldapManager.normalizePermissionName(getName()));
63              } else {
64                  permissions = StringUtil.EMPTY_STRINGS;
65              }
66          }
67          return permissions;
68      }
69  
70      @Override
71      public String[] getRoleNames() {
72          final FessConfig fessConfig = ComponentUtil.getFessConfig();
73          return stream(getPermissions()).get(stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchRolePrefix()))
74                  .map(s -> s.substring(1)).toArray(n -> new String[n]));
75      }
76  
77      @Override
78      public String[] getGroupNames() {
79          final FessConfig fessConfig = ComponentUtil.getFessConfig();
80          return stream(getPermissions()).get(stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchGroupPrefix()))
81                  .map(s -> s.substring(1)).toArray(n -> new String[n]));
82      }
83  
84      public Hashtable<String, String> getEnvironment() {
85          return env;
86      }
87  
88      @Override
89      public boolean isEditable() {
90          return ComponentUtil.getFessConfig().isLdapAdminEnabled(name);
91      }
92  
93  }