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.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 = ComponentUtil.getFessConfig().getLdapBaseDn();
53              final String accountFilter = ComponentUtil.getFessConfig().getLdapAccountFilter();
54              if (StringUtil.isNotBlank(baseDn) && StringUtil.isNotBlank(accountFilter)) {
55                  permissions =
56                          ArrayUtils.addAll(ComponentUtil.getLdapManager().getRoles(this, baseDn, accountFilter),
57                                  fessConfig.getRoleSearchUserPrefix() + getName());
58              } else {
59                  permissions = StringUtil.EMPTY_STRINGS;
60              }
61          }
62          return permissions;
63      }
64  
65      @Override
66      public String[] getRoleNames() {
67          final FessConfig fessConfig = ComponentUtil.getFessConfig();
68          return stream(getPermissions()).get(
69                  stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchRolePrefix())).map(s -> s.substring(1))
70                          .toArray(n -> new String[n]));
71      }
72  
73      @Override
74      public String[] getGroupNames() {
75          final FessConfig fessConfig = ComponentUtil.getFessConfig();
76          return stream(getPermissions()).get(
77                  stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchGroupPrefix())).map(s -> s.substring(1))
78                          .toArray(n -> new String[n]));
79      }
80  
81      public Hashtable<String, String> getEnvironment() {
82          return env;
83      }
84  
85      @Override
86      public boolean isEditable() {
87          return ComponentUtil.getFessConfig().isLdapAdminEnabled(name);
88      }
89  
90  }