LDAPGroupManager.java 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti.ldap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.activiti.engine.ActivitiException;
26
import org.activiti.engine.ActivitiIllegalArgumentException;
27 28 29 30
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
31
import org.activiti.engine.impl.context.Context;
32 33
import org.activiti.engine.impl.persistence.AbstractManager;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
34
import org.activiti.engine.impl.persistence.entity.GroupEntityImpl;
35
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
36 37

/**
38
 * Implementation of the {@link GroupEntityManager} interface specifically for LDAP.
39
 * 
40
 * Note that only a few methods are actually implemented, as many of the operations (save, update, etc.) are done on the LDAP system directly.
41
 * 
42 43
 * @author Joram Barrez
 */
44
public class LDAPGroupManager extends AbstractManager implements GroupEntityManager {
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  protected LDAPConfigurator ldapConfigurator;
  protected LDAPGroupCache ldapGroupCache;

  public LDAPGroupManager(LDAPConfigurator ldapConfigurator) {
    this.ldapConfigurator = ldapConfigurator;
  }

  public LDAPGroupManager(LDAPConfigurator ldapConfigurator, LDAPGroupCache ldapGroupCache) {
    this.ldapConfigurator = ldapConfigurator;
    this.ldapGroupCache = ldapGroupCache;
  }

  @Override
  public Group createNewGroup(String groupId) {
    throw new ActivitiException("LDAP group manager doesn't support creating a new group");
  }
62 63 64 65 66
  
  @Override
  public GroupEntity create() {
    throw new ActivitiException("LDAP group manager doesn't support creating a new group");
  }
67 68

  @Override
69 70 71 72 73 74 75
  public GroupEntity update(GroupEntity entity) {
    throw new ActivitiException("LDAP group manager doesn't support updating a new group");
  }
  
  @Override
  public GroupEntity update(GroupEntity entity, boolean fireUpdateEvent) {
    throw new ActivitiException("LDAP group manager doesn't support updating a new group");
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  }

  @Override
  public boolean isNewGroup(Group group) {
    throw new ActivitiException("LDAP group manager doesn't support inserting or updating a group");
  }

  @Override
  public GroupQuery createNewGroupQuery() {
    return new GroupQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutor());
  }

  @Override
  public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
    // Only support for groupMember() at the moment
    if (query.getUserId() != null) {
      return findGroupsByUser(query.getUserId());
    } else {
      throw new ActivitiIllegalArgumentException("This query is not supported by the LDAPGroupManager");
T
Tijs Rademakers 已提交
95
    }
96 97 98 99
  }

  @Override
  public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
100
    return findGroupByQueryCriteria(query, null).size(); // Is there a generic way to do a count(*) in ldap?
101 102 103 104 105 106 107 108 109 110 111
  }

  @Override
  public List<Group> findGroupsByUser(final String userId) {

    // First try the cache (if one is defined)
    if (ldapGroupCache != null) {
      List<Group> groups = ldapGroupCache.get(userId);
      if (groups != null) {
        return groups;
      }
T
Tijs Rademakers 已提交
112 113
    }

114 115 116
    // Do the search against Ldap
    LDAPTemplate ldapTemplate = new LDAPTemplate(ldapConfigurator);
    return ldapTemplate.execute(new LDAPCallBack<List<Group>>() {
T
Tijs Rademakers 已提交
117

118
      public List<Group> executeInContext(InitialDirContext initialDirContext) {
T
Tijs Rademakers 已提交
119

120
        String searchExpression = ldapConfigurator.getLdapQueryBuilder().buildQueryGroupsForUser(ldapConfigurator, userId);
T
Tijs Rademakers 已提交
121

122 123 124 125 126 127
        List<Group> groups = new ArrayList<Group>();
        try {
          String baseDn = ldapConfigurator.getGroupBaseDn() != null ? ldapConfigurator.getGroupBaseDn() : ldapConfigurator.getBaseDn();
          NamingEnumeration<?> namingEnum = initialDirContext.search(baseDn, searchExpression, createSearchControls());
          while (namingEnum.hasMore()) { // Should be only one
            SearchResult result = (SearchResult) namingEnum.next();
T
Tijs Rademakers 已提交
128

129
            GroupEntity group = new GroupEntityImpl();
130 131
            if (ldapConfigurator.getGroupIdAttribute() != null) {
              group.setId(result.getAttributes().get(ldapConfigurator.getGroupIdAttribute()).get().toString());
132
            }
133 134
            if (ldapConfigurator.getGroupNameAttribute() != null) {
              group.setName(result.getAttributes().get(ldapConfigurator.getGroupNameAttribute()).get().toString());
T
Tijs Rademakers 已提交
135
            }
136 137 138 139 140
            if (ldapConfigurator.getGroupTypeAttribute() != null) {
              group.setType(result.getAttributes().get(ldapConfigurator.getGroupTypeAttribute()).get().toString());
            }
            groups.add(group);
          }
T
Tijs Rademakers 已提交
141

142
          namingEnum.close();
T
Tijs Rademakers 已提交
143

144 145 146 147
          // Cache results for later
          if (ldapGroupCache != null) {
            ldapGroupCache.add(userId, groups);
          }
T
Tijs Rademakers 已提交
148

149
          return groups;
T
Tijs Rademakers 已提交
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        } catch (NamingException e) {
          throw new ActivitiException("Could not find groups for user " + userId, e);
        }
      }

    });
  }

  @Override
  public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
    throw new ActivitiException("LDAP group manager doesn't support querying");
  }

  @Override
  public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) {
    throw new ActivitiException("LDAP group manager doesn't support querying");
  }

  protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
  }
T
Tijs Rademakers 已提交
175

176 177 178 179 180 181 182 183 184 185 186
  @Override
  public void insert(GroupEntity entity) {
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

  @Override
  public void insert(GroupEntity entity, boolean fireCreateEvent) {
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

  @Override
187
  public GroupEntity findById(String entityId) {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

  @Override
  public void delete(String id) {
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

  @Override
  public void delete(GroupEntity entity, boolean fireDeleteEvent) {
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

  @Override
  public void delete(GroupEntity entity) {
    throw new ActivitiException("Unsupported by LDAP group manager");
  }

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  public LDAPConfigurator getLdapConfigurator() {
    return ldapConfigurator;
  }

  public void setLdapConfigurator(LDAPConfigurator ldapConfigurator) {
    this.ldapConfigurator = ldapConfigurator;
  }

  public LDAPGroupCache getLdapGroupCache() {
    return ldapGroupCache;
  }

  public void setLdapGroupCache(LDAPGroupCache ldapGroupCache) {
    this.ldapGroupCache = ldapGroupCache;
  }

222
}