Skip to content

My Simple OLM

Presentation

OLM stands for Object Ldap Mapping.

msolm is a PHP class generator based on the same principle as msorm, but for LDAP databases.

From an XML file that describes one or more types of LDAP entries, msolm generates a set of PHP classes that represent these entries. This PHP object model can be copied into a project and is usable as is, without any other tools being necessary.

Warning

Classes generated by msolm use the MLdap class from mlib (mlib\net\ldap\MLdap).

The generated model can be extended with additional methods that can be written.

Using the model is very easy and makes the application code very readable.

msolm is organized as follows:

msolm/
  ├── msolm.php
  ├── generators/
     ...
     ...
  └── projects/
        ├── myproject_1/
            └── description.xml
        └── myproject_2/
             └── description.xml

For a given project, you need to create a description.xml file (see here) and run the following command (the msolm.php file must have execution rights):

$ ./msolm.php -g -p myproject

This command is the most basic. It will take the description.xml file located in the myproject/ subfolder of the projects/ folder and create a model/ folder at the same level, which contains all the generated PHP files.

There are several options, including:

  • -g (or --generate)
  • -o (or --output): allows specifying a different destination folder than the model/ subfolder.
  • -u (or --update): allows not overwriting any additional code that may have been written.
  • -h (or -? or --help): displays help summarizing all options.

An Example

The description.xml File

Example:

<?xml version="1.0" encoding="UTF-8"?>

<ldap host="ldap.my-corp.org" port="389" bind_dn="cn=manager,dc=my-corp,dc=org" bind_password="" phpNamespace="ldapsample">    
    <object ou="ou=people,dc=my-corp,dc=org" filter="" phpObjectName="Person">
        <attribute name="uid" rdn="true" phpAttributeName="login" />
        <attribute name="sn" phpAttributeName="name" />
        <attribute name="givenName" phpAttributeName="firstname" />
        <attribute name="displayName" />
        <attribute name="mail" />
        <attribute name="otherMails" multi="true" phpAttributeName="mailAlias" phpAttributePFName="mailAliases"/>
    </object>
</ldap>

What is Generated

model/
└── ldapsample
    ├── Person.php
    ├── PersonManager.php
    ├── core
    │   ├── LdapConnectionProvider.php
    │   ├── MSOLM.php
    │   ├── Person.conf.php
    │   ├── PersonCore.php
    │   ├── PersonManagerCore.php
    │   └── __ldap_params.conf.php
    └── exceptions
        ├── LdapConnectionProviderException.php
        ├── PersonException.php
        └── PersonManagerException.php
  • Each "object" defined in the XML file generates two classes.
  • For each object, the phpObjectName attribute is defined, which will be the name of the class.
    • The phpObjectPFName attribute can also be defined if the plural is not formed by simply adding an "s". 'PF' stands for 'plural form'. This form is used to generate the name of the manager.
  • For each attribute, a phpAttributeName (and possibly a phpAttributePFName if multi-valued) can be defined if you want the PHP class attribute to be named differently from the LDAP attribute. This allows for better abstraction.

Important

In the core/ folder, in addition to the two classes ObjectCore and ObjectManagerCore, there is a third file Object.conf.php.
This file defines the mapping of attribute names.
By default, it contains what is indicated in the XML file.
However, in the development of applications interfaced with an LDAP directory, most of the time we use an existing directory, for which we did not choose the schemas and attribute names (unlike databases where the database design is part of the application development). That's why we offer the possibility to modify these attribute names in a separate configuration file. Some attributes can even be left empty if the directory does not have the information.

Exemple :

<?php
$ldap_base = "ou=people,dc=univ-perp,dc=fr";
$ldap_filter = "";
$ldap_attributes_mapping = array(
        "login" => "uid",  
        "name" => "sn", 
        "firstname" => "givenName", 
        "displayName" => "displayName", 
        "mail" => "mail", 
        "mailAliases" => "otherMails");
?>

How to Use This Model?

The principle remains the same, but the generated classes contain code and attributes that really depend on the definition XML file.

Example:

$persons_manager = ldapsample\PersonsManager::getInstance();

$person = $persons_manager->get('aPersonLogin');

echo $person->getLogin()." : ".$person->getDisplayName().PHP_EOL;
echo "    ".$person->getMail().PHP_EOL;
foreach($person->getMailAliases() as $mailAlias){
    echo "    ".$mailAlias.PHP_EOL;
}

$person->addMailAlias('ceo@mycorp.org');
$persons_manager->update($person);

In the example above, we see that for each "object" defined in XML, we have two PHP classes: one "Object" class and one "ObjectsManager" class.

  • The first represents objects with their attributes (with their getters and setters).
  • The second is a class that allows managing these objects: obtaining one or more objects, checking existence, adding, deleting, or updating them.

These two classes inherit from their "core" equivalents, which contain all the attributes and methods provided by default.

These two classes can therefore be enriched with other attributes or methods that you may need.

Unlike msorm, msolm does not manage the relationships that may exist between objects (values of an attribute of a Person object and belonging to another Group object, for example).