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.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.direction.FessConfig;
26  import org.codelibs.fess.util.ComponentUtil;
27  
28  public class LdapUser implements FessUser {
29  
30      private static final long serialVersionUID = 1L;
31  
32      protected Hashtable<String, String> env;
33  
34      protected String name;
35  
36      protected String[] permissions = null;
37  
38      public LdapUser(final Hashtable<String, String> env, final String name) {
39          this.env = env;
40          this.name = name;
41      }
42  
43      @Override
44      public String getName() {
45          return name;
46      }
47  
48      @Override
49      public String[] getPermissions() {
50          if (permissions == null) {
51              final FessConfig fessConfig = ComponentUtil.getFessConfig();
52              final String baseDn = fessConfig.getLdapBaseDn();
53              final String accountFilter = fessConfig.getLdapAccountFilter();
54              final String groupFilter = fessConfig.getLdapGroupFilter();
55              if (StringUtil.isNotBlank(baseDn) && StringUtil.isNotBlank(accountFilter)) {
56                  permissions =
57                          ArrayUtils.addAll(ComponentUtil.getLdapManager().getRoles(this, baseDn, accountFilter, groupFilter),
58                                  fessConfig.getRoleSearchUserPrefix() + getName());
59              } else {
60                  permissions = StringUtil.EMPTY_STRINGS;
61              }
62          }
63          return permissions;
64      }
65  
66      @Override
67      public String[] getRoleNames() {
68          final FessConfig fessConfig = ComponentUtil.getFessConfig();
69          return stream(getPermissions()).get(
70                  stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchRolePrefix())).map(s -> s.substring(1))
71                          .toArray(n -> new String[n]));
72      }
73  
74      @Override
75      public String[] getGroupNames() {
76          final FessConfig fessConfig = ComponentUtil.getFessConfig();
77          return stream(getPermissions()).get(
78                  stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchGroupPrefix())).map(s -> s.substring(1))
79                          .toArray(n -> new String[n]));
80      }
81  
82      public Hashtable<String, String> getEnvironment() {
83          return env;
84      }
85  
86      @Override
87      public boolean isEditable() {
88          return ComponentUtil.getFessConfig().isLdapAdminEnabled(name);
89      }
90  
91  }