]> BookStack Code Mirror - bookstack/blob - app/Services/Ldap.php
Major permission naming refactor and database migration cleanup
[bookstack] / app / Services / Ldap.php
1 <?php namespace BookStack\Services;
2
3
4 /**
5  * Class Ldap
6  * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
7  * Allows the standard LDAP functions to be mocked for testing.
8  * @package BookStack\Services
9  */
10 class Ldap
11 {
12
13     /**
14      * Connect to a LDAP server.
15      * @param string $hostName
16      * @param int    $port
17      * @return resource
18      */
19     public function connect($hostName, $port)
20     {
21         return ldap_connect($hostName, $port);
22     }
23
24     /**
25      * Set the value of a LDAP option for the given connection.
26      * @param resource $ldapConnection
27      * @param int $option
28      * @param mixed $value
29      * @return bool
30      */
31     public function setOption($ldapConnection, $option, $value)
32     {
33         return ldap_set_option($ldapConnection, $option, $value);
34     }
35
36     /**
37      * Search LDAP tree using the provided filter.
38      * @param resource   $ldapConnection
39      * @param string     $baseDn
40      * @param string     $filter
41      * @param array|null $attributes
42      * @return resource
43      */
44     public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
45     {
46         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
47     }
48
49     /**
50      * Get entries from an ldap search result.
51      * @param resource $ldapConnection
52      * @param resource $ldapSearchResult
53      * @return array
54      */
55     public function getEntries($ldapConnection, $ldapSearchResult)
56     {
57         return ldap_get_entries($ldapConnection, $ldapSearchResult);
58     }
59
60     /**
61      * Search and get entries immediately.
62      * @param resource   $ldapConnection
63      * @param string     $baseDn
64      * @param string     $filter
65      * @param array|null $attributes
66      * @return resource
67      */
68     public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
69     {
70         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
71         return $this->getEntries($ldapConnection, $search);
72     }
73
74     /**
75      * Bind to LDAP directory.
76      * @param resource $ldapConnection
77      * @param string   $bindRdn
78      * @param string   $bindPassword
79      * @return bool
80      */
81     public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
82     {
83         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
84     }
85
86 }