Entry(3pm) | User Contributed Perl Documentation | Entry(3pm) |
Mozilla::LDAP::Entry.pm - Object class to hold one LDAP entry.
use Mozilla::LDAP::Conn; use Mozilla::LDAP::Entry;
The LDAP::Conn object is used to perform LDAP searches, updates, adds and deletes. All such functions works on LDAP::Entry objects only. All modifications and additions you'll do to an LDAP entry, will be done through this object class.
The LDAP::Entry object class is built on top of the Tie::Hash standard object class. This gives us several powerful features, the main one being to keep track of what is changing in the LDAP entry. This makes it very easy to write LDAP clients that needs to update/modify entries, since you'll just do the changes, and this object class will take care of the rest.
We define local functions for STORE, FETCH, DELETE, EXISTS, FIRSTKEY and NEXTKEY in this object class, and inherit the rest from the super class. Overloading these specific functions is how we can keep track of what is changing in the entry, which turns out to be very convenient. We can also easily "loop" over the attribute types, ignoring internal data, or deleted attributes.
Most of the methods here either return the requested LDAP value, or a status code. The status code (either 0 or 1) indicates the failure or success of a certain operation. 0 (False) meaning the operation failed, and a return code of 1 (True) means complete success.
One thing to remember is that in LDAP, attribute names are case insensitive. All methods in this class are aware of this, and will convert all attribute name arguments to lower case before performing any operations. This does not mean that the values are case insensitive. On the contrary, all values are considered case sensitive by this module, even if the LDAP server itself treats it as a CIS attribute.
The LDAP::Entry class implements many methods you can use to access and modify LDAP entries. It is strongly recommended that you use this API as much as possible, and avoid using the internals of the class directly. Failing to do so may actually break the functionality.
To create a completely new entry, use the new method, for instance
$entry = Mozilla::LDAP::Entry->new() $entry->setDN("uid=leif,ou=people,dc=netscape,dc=com"); $entry->{objectclass} = [ "top", "person", "inetOrgPerson" ]; $entry->addValue("cn", "Leif Hedstrom"); $entry->addValue("sn", "Hedstrom"); $entry->addValue("givenName", "Leif"); $entry->addValue("mail", "leif@netscape.com); $conn->add($entry);
This is the minimum requirements for an LDAP entry. It must have a DN, and it must have at least one objectclass. As it turns out, by adding the person and inetOrgPerson classes, we also must provide some more attributes, like CN and SN. This is because the object classes have these attributes marked as "required", and we'd get a schema violation without those values.
In the example above we use both native API methods to add values, and setting an attribute entire value set directly. Note that the value set is a pointer to an array, and not the array itself. In the example above, the object classes are set using an anonymous array, which the API handles properly. It's important to be aware that the attribute value list is indeed a pointer.
Finally, as you can see there's only only one way to add new LDAP entries, and it's called add(). It normally takes an LDAP::Entry object instance as argument, but it can also be called with a regular hash array if so desired.
This is the main functionality of this module. Use these methods to do any modifications and updates to your LDAP entries.
The optional third argument is a flag, indicating that we want to add the attribute without checking for duplicates. This is useful if you know the values are unique already, or if you perhaps want to allow duplicates for a particular attribute. The fourth argument (again optional) is a flag indicating that we want to perform DN normalization on the attribute. The final, fifth, optional argument indicates that the attribute values are case insensitive (CIS).
To add a CN to an existing entry/attribute, do:
$entry->addValue("cn", "Leif Hedstrom");
$dn = "uid=Leif, dc=Netscape, dc=COM"; $entry->addDNValue("uniqueMember", $dn);
will only add the DN for "uid=leif" if it does not exist as a DN in the uniqueMember attribute.
$entry->attrModified("cn");
$entry->copy("cn", "description");
if ($entry->exists("jpegphoto")) { # do something special }
print "The DN is: ", $entry->getDN(), "\n";
Just like setDN, this method also has an optional argument, which indicates we should normalize the DN before returning it to the caller.
@someArray = $entry->getValues("description"); $scalval = $entry->getValues("cn");
if ($entry->hasValue("objectclass", "person", 1)) { # do something }
The (optional) third argument indicates if the string comparison should be case insensitive or not, and the (optional) fourth argument indicats whether we should normalize the string as if it was a DN. The first two arguments are the name and value of the attribute, respectively.
if ($entry->isAttr("cn")) { # do something }
The code section will only be executed if these criterias are true:
1. The name of the attribute is a non-empty string. 2. The name of the attribute does not begin, and end, with an underscore character (_). 2. The attribute has one or more values in the entry.
if (! $entry->isDeleted("cn")) { # do something }
if ($entry->isModified("cn")) { # do something }
if ($entry->matchValue("objectclass", "pers", 1)) { # do something }
$entry->move("cn", "sn");
dn: uid=leif,ou=people,dc=netscape,dc=com objectclass: top objectclass: person objectclass: inetOrgPerson uid: leif cn: Leif Hedstrom mail: leif@netscape.com
The above would be the result of
$entry->printLDIF();
If you need to write to a file, open and then select() it. For more useful LDIF functionality, check out the Mozilla::LDAP::LDIF.pm module.
$entry->remove("mailAlternateAddress");
$entry->removeValue("objectclass", "nscpPerson");
$dn = "uid=Leif, dc=Netscape, dc=COM"; $entry->removeDNValue("owner", $dn);
will remove the owner "uid=leif,dc=netscape,dc=com", no matter how it's capitalized and formatted in the entry.
$entry->setDN("uid=leif,ou=people,dc=netscape,dc=com");
There is an optional third argument, a boolean flag, indicating that we should normalize the DN before setting it. This will assure a consistent format of your DNs.
$entry->setValues("cn", "Leif Hedstrom", "The Swede"); $entry->setValues("mail", @mailAddresses);
or if it's a single value attribute,
$entry->setValues("uidNumber", "12345");
$entry->{cn} = [ "Leif Hedstrom", "The Swede" ]; $numVals = $entry->size("cn");
This will set $numVals to two (2). The only argument is the name of the attribute, and the return value is the size of the value array.
To delete an LDAP entry from the LDAP server, you have to use the delete method from the Mozilla::LDAP::Conn module. It will actually delete any entry, if you provide an legitimate DN.
Again, there's no functionality in this object class to rename the entry (i.e. changing it's DN). For now, there is a way to modify the RDN component of a DN through the Mozilla::LDAP::Conn module, with modifyRDN. Eventually we hope to have a complete rename method, which should be capable of renaming any entry, in any way, including moving it to a different part of the DIT (Directory Information Tree).
There are plenty of examples to look at, in the examples directory. We are adding more examples every day (almost).
Installing this package is part of the Makefile supplied in the package. See the installation procedures which are part of this package.
This package can be retrieved from a number of places, including:
http://www.mozilla.org/directory/ Your local CPAN server
Most of this code was developed by Leif Hedstrom, Netscape Communications Corporation.
None. :)
Mozilla::LDAP::Conn, Mozilla::LDAP::API, and of course Perl.
2024-03-31 | perl v5.38.2 |