Example:
$filter = Horde_Ldap_Filter::combine('or', array($filter1, $filter2));
If the array contains filter strings instead of filter objects, they
will be parsed. public static combine ( string $operator, array | Horde_Ldap_Filter | string $filters ) : Horde_Ldap_Filter | ||
$operator | string | The logical operator, either "and", "or", "not" or the logical equivalents "&", "|", "!". |
$filters | array | Horde_Ldap_Filter | string | Array with Horde_Ldap_Filter objects and/or strings or a single filter when using the "not" operator. |
return | Horde_Ldap_Filter |
/** * Tries to find a DN for a given kolab mail address. * * @param string $mail The mail address to search for. * * @return string The corresponding dn or false. * @throws Horde_Group_Exception */ protected function _dnForMail($mail) { try { $filter = Horde_Ldap_Filter::combine('and', array(Horde_Ldap_Filter::create('objectclass', 'equals', 'kolabInetOrgPerson'), Horde_Ldap_Filter::create('mail', 'equals', $mail))); $search = $this->_ldap->search($this->_params['basedn'], $filter, array('dn')); if ($search->count()) { return $search->shiftEntry()->dn(); } } catch (Horde_Ldap_Exception $e) { throw new Horde_Group_Exception($e); } throw new Horde_Group_Exception(sprintf('Error searching for user with the email address "%s"', $mail)); }
/** * Checks if $userId exists in the LDAP backend system. * * @author Marco Ferrante, University of Genova (I) * * @param string $userId User ID for which to check * * @return boolean Whether or not $userId already exists. */ public function exists($userId) { $params = array('scope' => $this->_params['scope']); try { $uidfilter = Horde_Ldap_Filter::create($this->_params['uid'], 'equals', $userId); $classfilter = Horde_Ldap_Filter::build(array('filter' => $this->_params['filter'])); $search = $this->_ldap->search($this->_params['basedn'], Horde_Ldap_Filter::combine('and', array($uidfilter, $classfilter)), $params); if ($search->count() < 1) { return false; } if ($search->count() > 1 && $this->_logger) { $this->_logger->log('Multiple LDAP entries with user identifier ' . $userId, 'WARN'); } return true; } catch (Horde_Ldap_Exception $e) { if ($this->_logger) { $this->_logger->log('Error searching LDAP user: '******'ERR'); } return false; } }
/** * Returns the DN of a user. * * The purpose is to quickly find the full DN of a user so it can be used * to re-bind as this user. This method requires the 'user' configuration * parameter to be set. * * @param string $user The user to find. * * @return string The user's full DN. * @throws Horde_Ldap_Exception * @throws Horde_Exception_NotFound */ public function findUserDN($user) { $filter = Horde_Ldap_Filter::combine('and', array(Horde_Ldap_Filter::build($this->_config['user']), Horde_Ldap_Filter::create($this->_config['user']['uid'], 'equals', $user))); $search = $this->search(isset($this->_config['user']['basedn']) ? $this->_config['user']['basedn'] : null, $filter, array('attributes' => array($this->_config['user']['uid']))); if (!$search->count()) { throw new Horde_Exception_NotFound('DN for user ' . $user . ' not found'); } $entry = $search->shiftEntry(); return $entry->currentDN(); }
/** * Convert the group element to query format. * * @param Horde_Kolab_Server_Query_Element_Group $group The element to convert. * @param string $operator The element operation. * * @return mixed The query element in query format. * * @throws Horde_Kolab_Server_Exception If the query is malformed. */ public function _convertGroup(Horde_Kolab_Server_Query_Element_Group $group, $operator) { $filters = array(); foreach ($group->getElements() as $element) { $filters[] = $element->convert($this); } try { return Horde_Ldap_Filter::combine($operator, $filters); } catch (Horde_Ldap_Exception $e) { throw new Horde_Kolab_Server_Exception($e->getMessage(), Horde_Kolab_Server_Exception::INVALID_QUERY, $e); } }
/** * This tests the basic combination of filters. */ public function testCombine() { // Setup. $filter0 = Horde_Ldap_Filter::create('foo', 'equals', 'bar'); $this->assertInstanceOf('Horde_Ldap_Filter', $filter0); $filter1 = Horde_Ldap_Filter::create('bar', 'equals', 'foo'); $this->assertInstanceOf('Horde_Ldap_Filter', $filter1); $filter2 = Horde_Ldap_Filter::create('you', 'equals', 'me'); $this->assertInstanceOf('Horde_Ldap_Filter', $filter2); $filter3 = Horde_Ldap_Filter::parse('(perlinterface=used)'); $this->assertInstanceOf('Horde_Ldap_Filter', $filter3); // Negation test. $filter_not1 = Horde_Ldap_Filter::combine('not', $filter0); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_not1, 'Negation failed for literal NOT'); $this->assertEquals('(!(foo=bar))', (string) $filter_not1); $filter_not2 = Horde_Ldap_Filter::combine('!', $filter0); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_not2, 'Negation failed for logical NOT'); $this->assertEquals('(!(foo=bar))', (string) $filter_not2); $filter_not3 = Horde_Ldap_Filter::combine('!', (string) $filter0); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_not3, 'Negation failed for logical NOT'); $this->assertEquals('(!' . $filter0 . ')', (string) $filter_not3); // Combination test: OR $filter_comb_or1 = Horde_Ldap_Filter::combine('or', array($filter1, $filter2)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_or1, 'Combination failed for literal OR'); $this->assertEquals('(|(bar=foo)(you=me))', (string) $filter_comb_or1); $filter_comb_or2 = Horde_Ldap_Filter::combine('|', array($filter1, $filter2)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_or2, 'combination failed for logical OR'); $this->assertEquals('(|(bar=foo)(you=me))', (string) $filter_comb_or2); // Combination test: AND $filter_comb_and1 = Horde_Ldap_Filter::combine('and', array($filter1, $filter2)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_and1, 'Combination failed for literal AND'); $this->assertEquals('(&(bar=foo)(you=me))', (string) $filter_comb_and1); $filter_comb_and2 = Horde_Ldap_Filter::combine('&', array($filter1, $filter2)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_and2, 'combination failed for logical AND'); $this->assertEquals('(&(bar=foo)(you=me))', (string) $filter_comb_and2); // Combination test: using filter created with perl interface. $filter_comb_perl1 = Horde_Ldap_Filter::combine('and', array($filter1, $filter3)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_perl1, 'Combination failed for literal AND'); $this->assertEquals('(&(bar=foo)(perlinterface=used))', (string) $filter_comb_perl1); $filter_comb_perl2 = Horde_Ldap_Filter::combine('&', array($filter1, $filter3)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_perl2, 'combination failed for logical AND'); $this->assertEquals('(&(bar=foo)(perlinterface=used))', (string) $filter_comb_perl2); // Combination test: using filter_str instead of object $filter_comb_fstr1 = Horde_Ldap_Filter::combine('and', array($filter1, '(filter_str=foo)')); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comb_fstr1, 'Combination failed for literal AND using filter_str'); $this->assertEquals('(&(bar=foo)(filter_str=foo))', (string) $filter_comb_fstr1); // Combination test: deep combination $filter_comp_deep = Horde_Ldap_Filter::combine('and', array($filter2, $filter_not1, $filter_comb_or1, $filter_comb_perl1)); $this->assertInstanceOf('Horde_Ldap_Filter', $filter_comp_deep, 'Deep combination failed!'); $this->assertEquals('(&(you=me)(!(foo=bar))(|(bar=foo)(you=me))(&(bar=foo)(perlinterface=used)))', (string) $filter_comp_deep); // Test failure in combination try { Horde_Ldap_Filter::create('foo', 'test_undefined_matchingrule', 'bar'); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('not', 'damaged_filter_str'); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('not', array($filter0, $filter1)); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('not', null); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('and', $filter_not1); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('and', array($filter_not1)); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('and', $filter_not1); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('or', array($filter_not1)); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('some_unknown_method', array($filter_not1)); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('and', array($filter_not1, 'some_invalid_filterstring')); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } try { Horde_Ldap_Filter::combine('and', array($filter_not1, null)); $this->fail('Horde_Ldap_Exception expected.'); } catch (Horde_Ldap_Exception $e) { } }
/** * Returns a list of groups a user belongs to. * * @param string $user A user name. * * @return array A list of groups, with IDs as keys and names as values. * @throws Horde_Group_Exception */ public function listGroups($user) { $attr = $this->_params['gid']; try { if (!empty($this->_params['attrisdn'])) { $user = $this->_ldap->findUserDN($user); } $filter = Horde_Ldap_Filter::create($this->_params['memberuid'], 'equals', $user); $filter = Horde_Ldap_Filter::combine('and', array($this->_filter, $filter)); $search = $this->_ldap->search($this->_params['basedn'], $filter, array($attr)); } catch (Horde_Ldap_Exception $e) { throw new Horde_Group_Exception($e); } $entries = array(); foreach ($search->sortedAsArray(array($attr)) as $entry) { $entries[$entry['dn']] = $entry[$attr][0]; } return $entries; }