Script functions reference > LDAP functions > LDAP examples

LDAP examples
The following examples show how the LDAP functions can be used:
Example 39
The script reads a user profile, and queries the fax number of the user.
// Connect to the LDAP server on localhost. Give the name "con" to the connection
ldapconnect("con", "localhost", "","");
// Retrieve information about the user specified in $ourref
$theuser = ldapgetuser("con", "dc=streamserve,dc=com",$ourref, "", "");
/* Retrieve the sn and givenname attributes of the user. Concatenate the two values with a comma in between, and store the string in $refNam.*/
$refNam = ldapgetattrvalue($theuser, "sn", 0) + ", " +
ldapgetattrvalue($theuser, "givenname", 0);
// Retrieve the user’s fax number and store it in $ourfax.
$ourfax = ldapgetattrvalue($theuser,"facsimiletelephonenumber", 0);
/* Here you either proceed with further queries or call LdapDisConnect to terminate the connection to the LDAP server.*/
 
Example 40
This script identifies members of a group, and queries the attributes (email address and common name) of each member.
/* Connect to the LDAP server on localhost. Give the name "con" to the connection. The username used is "Directory Manager" and password is "streamserve"*/
ldapconnect("con", "localhost", "cn=Directory Manager", "streamserve");
/* Find the group entry $ourgroup. The first argument is the connection ID, the second specifies the root of the search tree. The third argument is the common name to search for.*/
$grouplist = ldapfind("con", "ou=Groups, dc=streamserve,dc=com",
"cn=" + $ourgroup);
// Get the first entry (0 based index).
$thegroup = ldapgetentry($grouplist, 0);
// Get the number of values associated to attribute "uniquemember"
$iterator = ldapgetattrvaluecount($thegroup, "uniquemember") - 1;
// For each value of the attribute (each member of the group), do... 
while (num($iterator) > -1)
{
/* The values of the "uniquemember" attribute are distinguished names of different users who are members of the group. Fetch them one by one*/
    $groupmember = ldapgetobjectbydn("con", ldapgetattrvalue($thegroup,
"uniquemember", Num($iterator)) );
/* Get the value of the "mail" attribute for the current group member. Store it in $email*/
    $email =  ldapgetattrvalue($groupmember, "mail", 0);
// Get another of the users attributes...
    $commonname =  ldapgetattrvalue($groupmember, "cn", 0);
// Decrement $iterator to get the next group member.
    $iterator -= 1;
// Send it to the block

CallBlock("Free_Block");
  }
/* Here you either proceed with further queries or call LdapDisConnect to terminate the connection to the LDAP server.*/
 
Example 41
The following example is a variation of Example 40. This script identifies members of a group, and queries the attributes (email address) of each member. It then creates a string with concatenated email addresses for a mass mailing (an email is sent to every member of this group).
/* Connect to the LDAP server on localhost. Give the name "con" to the connection. The username is "Directory Manager" and password  "streamserve".*/
$ourrefs="";
$ourgroup = "Administrator Group";
ldapconnect("con", "localhost", "cn=Directory Manager","streamserve");
/* Find the group entry $ourgroup. The first argument is the connection ID, the second specifies the root of the search tree. The third argument is the common name to search for.*/
$grouplist = ldapfind("con", "ou=StreamServe Groups,dc=streamserve,dc=com", 
"(cn=" + $ourgroup + ")");
// Get the first entry (0 based index).
$theuser = ldapgetentry($grouplist, 0);
// Get the number of values associated to attribute "uniquemember"
$iterator = ldapgetattrvaluecount($theuser, "uniquemember")-1 ; 
log(0,"iterator="+$iterator);
// For each value of the attribute (each member of the group), do...
while (num($iterator) >-1)
{
/* The values of the "uniquemember" attribute are distinguished names of different users who are members of the group. Fetch them one by one.*/
      $groupuser = ldapgetobjectbydn("con", ldapgetattrvalue($theuser,
"uniquemember", num($iterator)) );
/* Get the value of the "mail" attribute for the current group member. Concatenate the value (e-mail address) with a semicolon between the e-mail addresses of different users and store the string in $ourrefs.*/
      $ourrefs = $ourrefs + ldapgetattrvalue($groupuser, "mail", 0) + ";";
// Decrement $iterator to get the next group member.
      $iterator -= 1;
    }
log(0,"ourref="+ $ourrefs);
/* Here you either proceed with further queries or call LdapDisConnect to terminate the connection to the LDAP server.*/
 
Example 42
This script retrieves one attribute (fax number) and updates another attribute (email address).
/* Connect to the LDAP server on localhost. Give the name "con" to the connection. The username used is "Directory Manager" and password is "dmanager"*/
ldapconnect("con", "localhost", "cn=Directory Manager", "dmanager");
/* Find the user entry $ourref. The first argument is the connection ID, the second specifies the root of the search tree. The third argument is the common name to search for.*/
$theuserlist = ldapfind("con", "dc=streamserve,dc=com", "cn=" + $ourref);
// Find the first (and hopefully only) entry in the result.
$theuser = ldapgetentry($theuserlist, 0);
// Get the user’s fax number
$ourfax = ldapgetattrvalue($theuser, "facsimiletelephonenumber", 0);
// Update the e-mail address
ldapreplaceattrvalue($theuser, "mail", $ourref_email);
//Write the changes made to $theuser back to the LDAP server.
ldapupdateentry($theuser);
/* Here you either proceed with further queries or call LdapDisConnect to terminate the connection to the LDAP server.*/
 
OpenText StreamServe 5.6 Updated: 2013-03-01