avadaneidanut/ldapquery

一个轻量级包,用于轻松构建LDAP高级过滤查询。

v1.0.0 2016-05-18 20:33 UTC

This package is not auto-updated.

Last update: 2024-09-24 21:39:38 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

LDAP查询构建器是一个简单的工具,可以轻松生成LDAP过滤查询。快速示例

$query = \LdapQuery\Builder::create()->where('attrBar', 'value')
    ->where('attrFoo', '<>' 'value2')
    ->orWhere('attrBaz', [1, 2, 3, 4, 5, 6, 7, 8, 9])
    ->where(function($builder) {
        $builder->where('bla', 'bla2')
            ->orWhere('bla3', 'bla1');
    })
    ->stringify()
;

输出

(&(|(&(attrBar=value)(!(attrFoo=value2)))(|(attrBaz=1)(attrBaz=2)(attrBaz=3)(attrBaz=4)(attrBaz=5)(attrBaz=6)(attrBaz=7)(attrBaz=8)(attrBaz=9)))(|(bla=bla2)(bla3=bla1)))

如果您想检查生成的查询并且不想手动分离组,只需

$builder = new \LdapQuery\Builder;
$builder->where('attrBar', 'value')
    ->where('attrFoo', '<>' 'value2')
    ->orWhere('attrBaz', [1, 2, 3, 4, 5, 6, 7, 8, 9])
    ->where(function($builder) {
        $builder->where('bla', 'bla2')
            ->orWhere('bla3', 'bla1');
    });

$builder->toArray(); # will generate a nice output

输出

(&
   (|
      (&
         (attrBar=value)
         (!
             (attrFoo=value2)
         )
      )
      (|
         (attrBaz=1)
         (attrBaz=2)
         (attrBaz=3)
         (attrBaz=4)
         (attrBaz=5)
         (attrBaz=6)
         (attrBaz=7)
         (attrBaz=8)
         (attrBaz=9)
      )
   )
   (|
      (bla=bla2)
      (bla3=bla1)
   )
)

与symfony ldap组件一起使用

use LdapQuery\Builder;
use Symfony\Component\Ldap\LdapClient;

$client = new LdapClient('ldap.example.com');
$client->bind('uid=AB1234,ou=people,o=world', 'secretpassword');

$builder = new Builder;

$details = $client->find(
    'ou=people,o=world',
    (string)$builder->where('uid', 'AB123*')->where('cn', '~=','*Danut*'),
    ['uid','cn','mail','office','mobile']
);

Builder类上的可用方法

/**
 * Add a where clause to the LDAP Query. Defaulted to & logical, acts like a andWhere.
 * 
 * @param  string|Closure    $attribute
 * @param  string|array|null $operator
 * @param  string|array|null $value
 * @param  string|null       $wildcard
 * @param  bool              $escape
 * @param  string            $logical
 *
 * @return $this
 *
 * @throws GrammarException
 */
public function where($attribute, $operator = null, $value = null, $wildcard = null, $escape = true, $negation = false, $logical = '&');
    
/**
 * Add a or where clause to the LDAP Query.
 * 
 * @param  string|Closure   $attribute
 * @param  string|array|null $operator
 * @param  string|array|null $value
 * @param  string|null       $wildcard
 * @param  bool              $escape
 *
 * @return $this
 *
 * @throws GrammarException
 */
public function orWhere($attribute, $operator = null, $value = null, $wildcard = null, $escape = true, $negation = false);

/**
 * Convert Group object to a string, LDAP valid query group.
 * 
 * @return string
 */
public function stringify();

/**
 * Convert Builder object to array.
 * 
 * @return array
 */
public function toArray();

动态where子句

LdapQuery.Builder类具有许多动态的"where子句",这些子句会自动转换为调用"where"或"orWhere"方法,并将方法名称中的参数翻译成方法参数。例如,这可以像其他方法一样使用

$builder->orWhereBegins('attribute', 'value'); will be translated in
$builder->orWhere('attribute', 'value', null, 'begins', true);

可用的动态where子句

// whereRaw - where attribute unescaped value
$builder->whereRaw('foo', 'bar*');
print $builder; // (foo=bar*)

// orWhereRaw - or where attribute unescaped value
$builder->where('foo', 'bar')
    ->orWhereRaw('foo', 'baz*'); 
print $builder; // (|(foo=bar)(foo=baz*))
// whereNot - where attribute not value
$builder->whereNot('foo', 'bar');
print $builder; // (!(foo=bar))

// whereNotRaw - where attribute not unescaped value
$builder->whereNotRaw('foo', 'bar*');
print $builder; // (!(foo=bar*))

// orWhereNotRaw - or where attribute not unescaped value
$builder->where('foo', 'bar')
  ->orWhereNotRaw('foo', 'baz*');
print $builder;  // (|(foo=bar)(!(foo=baz*)))
// whereBegins - where attribute begins with value
$buidler->whereBegins('foo', 'b'); 
print $builder; // (foo=b*)

// whereBeginsRaw - where attribute begins with unescaped value
$builder->whereBeginsRaw('foo', 'b)');
print $builder; // (foo=b)*)

// whereBeginsNot - where attribute not begins with value
$builder->whereNotBegins('foo', 'b');
print $builder; // (!(foo=b*))

// whereBeginsNotRaw - where attribute not begins with unescaped value
$builder->whereNotBeginsRaw('foo', 'b()');
print $builder; // (!(foo=b()*))

// orWhereBegins - or where attribute begins with value
$builder->where('foo', 'bar')
    ->orWhereBegins('foo', 'b');
print $builder; // (|(foo=bar)(foo=b*))

// orWhereBeginsRaw - or where attribute begins with unescaped value
$builder->where('foo', 'bar')
    ->orWhereBeginsRaw('foo', 'b()');
print $builder; // (|(foo=bar)(foo=b()*))

// orWhereBeginsNot - or where attribute not begins with value
$builder->where('foo', 'bar')
    ->orWhereBeginsNot('foo', 'b');
print $builder; // (|(foo=bar)(!(foo=b*)))

// orWhereBeginsNotRaw - or where attribute not begins with unescaped value
$builder->where('foo', 'bar')
    ->orWhereBeginsNotRaw('foo', 'b()');
print $builder; // (|(foo=bar)(!(foo=b()*)))
// whereEnds - where attribute ends with value
$buidler->whereEnds('foo', 'b'); 
print $builder; // (foo=*b)

// whereEndsRaw - where attribute ends with unescaped value
$builder->whereEndsRaw('foo', 'b)');
print $builder; // (foo=*b))

// whereEndsNot - where attribute not ends with value
$builder->whereNotEnds('foo', 'b');
print $builder; // (!(foo=*b))

// whereEndsNotRaw - where attribute not ends with unescaped value
$builder->whereNotEndsRaw('foo', 'b()');
print $builder; // (!(foo=*b()))

// orWhereEnds - or where attribute ends with value
$builder->where('foo', 'bar')
    ->orWhereEnds('foo', 'b');
print $builder; // (|(foo=bar)(foo=*b))

// orWhereEndsRaw - or where attribute ends with unescaped value
$builder->where('foo', 'bar')
    ->orWhereEndsRaw('foo', 'b()');
print $builder; // (|(foo=bar)(foo=*b()))

// orWhereEndsNot - or where attribute not ends with value
$builder->where('foo', 'bar')
    ->orWhereEndsNot('foo', 'b');
print $builder; // (|(foo=bar)(!(foo=*b)))

// orWhereEndsNotRaw - or where attribute not ends with unescaped value
$builder->where('foo', 'bar')
    ->orWhereEndsNotRaw('foo', 'b()');
print $builder; // (|(foo=bar)(!(foo=*b())))
// whereLike - where attribute like value
$buidler->whereLike('foo', 'b'); 
print $builder; // (foo=*b*)

// whereLikeRaw - where attribute like unescaped value
$builder->whereLikeRaw('foo', 'b)');
print $builder; // (foo=*b)*)

// whereLikeNot - where attribute not like value
$builder->whereNotLike('foo', 'b');
print $builder; // (!(foo=*b*))

// whereLikeNotRaw - where attribute not like unescaped value
$builder->whereNotLikeRaw('foo', 'b()');
print $builder; // (!(foo=*b()*))

// orWhereLike - or where attribute like value
$builder->where('foo', 'bar')
    ->orWhereLike('foo', 'b');
print $builder; // (|(foo=bar)(foo=*b*))

// orWhereLikeRaw - or where attribute like unescaped value
$builder->where('foo', 'bar')
    ->orWhereLikeRaw('foo', 'b()');
print $builder; // (|(foo=bar)(foo=*b()*))

// orWhereLikeNot - or where attribute not like value
$builder->where('foo', 'bar')
    ->orWhereLikeNot('foo', 'b');
print $builder; // (|(foo=bar)(!(foo=*b*)))

// orWhereLikeNotRaw - or where attribute not like unescaped value
$builder->where('foo', 'bar')
    ->orWhereLikeNotRaw('foo', 'b()');
print $builder; // (|(foo=bar)(!(foo=*b()*)))

安装

LdapQuery需要使用composer安装

$ composer require avadaneidanut/ldapquery

开发

想要贡献?太好了!迫不及待想听到您的声音!

许可协议

MIT