Getting started with ldap
This section provides an overview of what ldap is, and why a developer might want to use it.
It should also mention any large subjects within ldap, and link out to the related topics. Since the Documentation for ldap is new, you may need to create initial versions of those related topics.
Setting up PHP to work with LDAP
After you configured your LDAP server correctly, now we want to connect. For example by using PHP.
- DN = distinguished name. This means, in which part of the database are you working. Can be a user or a group (or even config settings).
- Entry: an entity, for example a user.
- Attribute: something inside an entry, for example name, phone number and email address.
First, we define the following:
Now, we are connected. Next thing we want, is to let the server know that we are a trustworthy person. The easiest solution is to use the root DN OR an existing user with proper permissions to view the database (probably every user in the database can do this by default). We authenticate using the bind function.
First, we declare these options. Depending on your server configuration, you can leave this out.
Assume you are using admin and password is pass123notsafe
That is very nice. We are in. Now we can perform a lot of different operations. For example, we can search, read, write and modify users and even groups.
Imagine we want to display all members of the group "users".
You can use foreach loops to display the data in a nice way.
Okay, got this? Now proceed with some filtering. We want to display only users in the group "bikeowners" who have registered an email address. It's important to know that all users are in cn=users. Next to this, they can also be member of other groups.
And now proceed with
Efficiency in getting entries
An example in which efficiency is addressed, increasingly important with big databases with a lot of attributes per entry. Especially when you are storing pictures in the attribute jpegphoto, it may significantly reduce your loading time when you selectively pull your entries.
Imagine we want to look for users that are in the group "bikeowners". There is a lot of information stored inside these entries, let's say they have the following attributes: cn, uid, name, displayname, mail, initials, mobile, telephonenumber, street, postaladdress, postalcode and jpegphoto. Now we only need their uid, name and initials.
For this, we use an optional 4th parameter of ldap_search:
And voila, running
ldap_get_entries will give you only these data.
Of course, you can pull a whole database with LDAP, and process this further in PHP. However, just as with MySQL it's much more efficient to do the processing server-side. To demonstrate this, we can use an advanced filter.
Take one of above examples as starting point, but change $filter according to the line below. In our example, we want to display the data of users who are active. Normally, the attribute
shadowexpire is used to store this information. However, this may differ among various LDAP systems. Not only we want to display the users that are active, but also their name must start with an "a" and they must live in Amsterdam.
Basically we want to do three things:
- Easiest: must live in Amsterdam. In this example the place of residence is stored in attribute 'postaladdress'
- Name must start with an "a". In this example, UID is composed from the name, so:
- User must be active. This value is stored in the default attribute
shadowexpire, with value
-1. Depending on your server configuration,
shadowexpirecan hold a myriad of values, even dates are possible. If a user is inactive,
1. To be sure we get all users except for those who are really inactive, we don't choose to filter on
shadowexpire = -1. Instead we say we don't want them to be inactive.
Now the most interesting part: combine all three examples. We can do this with brackets, AND, OR and NOT expressions
Finally, we could build in a OR statement using "|", for example if we want all users starting with "a" or "b"
You can combine endlessly and build quite impressive filters, enjoy trying!