View Javadoc
1   /*
2    * Copyright 2012-2025 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.Arrays;
21  import java.util.Hashtable;
22  
23  import org.apache.commons.lang3.ArrayUtils;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fess.entity.FessUser;
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.OptionalThing;
30  
31  /**
32   * An LDAP user.
33   */
34  public class LdapUser implements FessUser {
35  
36      private static final long serialVersionUID = 1L;
37  
38      /** The environment for LDAP connection. */
39      protected Hashtable<String, String> env;
40  
41      /** The name of the user. */
42      protected String name;
43  
44      /** The permissions of the user. */
45      protected String[] permissions = null;
46  
47      /**
48       * Constructs a new LDAP user.
49       *
50       * @param env The environment for LDAP connection.
51       * @param name The name of the user.
52       */
53      public LdapUser(final Hashtable<String, String> env, final String name) {
54          this.env = env;
55          this.name = name;
56      }
57  
58      @Override
59      public String getName() {
60          return name;
61      }
62  
63      @Override
64      public String[] getPermissions() {
65          if (permissions == null) {
66              final FessConfig fessConfig = ComponentUtil.getFessConfig();
67              final String baseDn = fessConfig.getLdapBaseDn();
68              final String accountFilter = fessConfig.getLdapAccountFilter();
69              final String groupFilter = fessConfig.getLdapGroupFilter();
70              if (StringUtil.isNotBlank(baseDn) && StringUtil.isNotBlank(accountFilter)) {
71                  final LdapManager ldapManager = ComponentUtil.getLdapManager();
72                  permissions = distinct(ArrayUtils.addAll(ldapManager.getRoles(this, baseDn, accountFilter, groupFilter, roles -> {
73                      permissions = distinct(roles);
74                      ComponentUtil.getActivityHelper().permissionChanged(OptionalThing.of(new FessUserBean(this)));
75                  }), fessConfig.getRoleSearchUserPrefix() + ldapManager.normalizePermissionName(getName())));
76              } else {
77                  permissions = StringUtil.EMPTY_STRINGS;
78              }
79          }
80          return permissions;
81      }
82  
83      @Override
84      public String[] getRoleNames() {
85          final FessConfig fessConfig = ComponentUtil.getFessConfig();
86          return stream(getPermissions()).get(stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchRolePrefix()))
87                  .map(s -> s.substring(1))
88                  .toArray(n -> new String[n]));
89      }
90  
91      @Override
92      public String[] getGroupNames() {
93          final FessConfig fessConfig = ComponentUtil.getFessConfig();
94          return stream(getPermissions()).get(stream -> stream.filter(s -> s.startsWith(fessConfig.getRoleSearchGroupPrefix()))
95                  .map(s -> s.substring(1))
96                  .toArray(n -> new String[n]));
97      }
98  
99      /**
100      * Returns the environment for LDAP connection.
101      *
102      * @return The environment for LDAP connection.
103      */
104     public Hashtable<String, String> getEnvironment() {
105         return env;
106     }
107 
108     @Override
109     public boolean isEditable() {
110         return ComponentUtil.getFessConfig().isLdapAdminEnabled(name);
111     }
112 
113     private static String[] distinct(final String[] values) {
114         if (values == null) {
115             return StringUtil.EMPTY_STRINGS;
116         }
117         if (values.length < 2) {
118             return values;
119         }
120         return Arrays.stream(values).distinct().toArray(n -> new String[n]);
121     }
122 
123 }