Forums / Developer / contrib : ldap group mappings

contrib : ldap group mappings

Author Message

Artturi Markko

Saturday 13 May 2006 6:26:03 am

Hello,

I have set up EzPublish with ldap authentication and I had to map existing ldap groups to the groups I have created in Ez.
From what I've read in the forums, mappings can only be achieved by retrieving an attribute on the ldap user object (employeetype is mentionned).

In consequence, I have written a small piece of code in kernel/classes/datatypes/ezuser/ezldapuser.php in order to retrieve the group memberships of the user who logs in.
<b>With the mappings defined in ldap.ini, this code fills the $extraNodeAssignments variable</b>.

<i>The contrib is in three parts</i> :
- additionnal settings in ldap.ini
- a recursive function that looks for groups (it's recursive to handle the case of nested groups). This function is placed outside of the "eZLDAPUser" class.
- a call to this function in the "loginUser" method of "eZLDAPUser" class

First, here is what I've added in ldap.ini :

LDAPUserGroupAM[]
LDAPUserGroupAM[]=ldapgroup1--ezgroup1
LDAPUserGroupAM[]=ldapgroup2--ezgrp2

Here's the function :

function mapInEzGroups($filter, $LDAPBaseDN, &$ds, &$db, &$ldap2ez, &$extraNodeAssignments, $depth = 0)
{
    $retrieve = array("cn");  // that's the groups name
    $sr2 = ldap_search( $ds, $LDAPBaseDN, $filter, $retrieve );
    $info2 = ldap_get_entries( $ds, $sr2 );       
                           
    $newfilter = '(&(objectClass=group)(|'; 
    $max = count($info2);

    // I loop on the found groups and check if I have a mapping defined for it
    for ( $i = 0; $i < $max; $i++ )
    {
        if ( is_null( $info2[ $i ] ) ) continue;
        $ldapGroupName = $info2[$i]['cn'][0];
        if ( array_key_exists($ldapGroupName, $ldap2ez) ) // is there a mapping ? --> ldap2ez holds the mappings
        {
            $groupName = $ldap2ez[$ldapGroupName];
            $groupQuery = "SELECT ezcontentobject_tree.node_id
                             FROM ezcontentobject, ezcontentobject_tree
                            WHERE ezcontentobject.name like '$groupName'
                              AND ezcontentobject.id=ezcontentobject_tree.contentobject_id
                              AND ezcontentobject.contentclass_id=3";
            $groupObject = $db->arrayQuery( $groupQuery );

            if ( count( $groupObject ) > 0 )
            {
                $extraNodeAssignments[] = $groupObject[0]['node_id'];
            }
        }        
        $newfilter .= '(member=' . $info2[$i]['dn']  . ')';                        
    }

    // If groups are found, I also check if they are nested in other groups, limited to a depth of 30
    if ( $depth < 30 && $max > 0 )
    {    
        $newfilter .= '))';
        mapInEzGroups($newfilter, $LDAPBaseDN, $ds, $db, $ldap2ez, $extraNodeAssignments, ( $depth + 1));
    }    
}

And in the "eZLDAPUser" class :

$LDAPUserGroupAML = $LDAPIni->variable( 'LDAPSettings', 'LDAPUserGroupAML' );

// ...

// ** existing code, just to show where it happens ***//
                // authenticated user
                if  ( !@ldap_bind( $ds, $info[0]['dn'], $password ) )
                {
                    $user = false;
                    return $user;
                }

// ** added code follows :            
                if ( $LDAPUserGroupAM != null )
                {
                    foreach ( $LDAPUserGroupAM as $value)
                    {
                        $r = explode("--", $value);
                        $ldap2ez[$r[0]] = $r[1];                        
                    }
                    // filter looking for groups the user is member of
                    $filter = "(&(objectClass=group)(member=" . $info[0]['dn'] . "))";

                    // this function fills the $extraNodeAssignments variable
                    mapInEzGroups($filter, $LDAPBaseDN, $ds, $db, $ldap2ez, $extraNodeAssignments);
                }

Artturi

Artturi Markko

Saturday 13 May 2006 6:37:56 am

Just a note to mention that while I've tested this code, I would welcome some critics about it.

There's also a catch about how it works in this form :
- upon user creation, mappings are done as I expect
- <b>if user exists in EZ before a mapping is created</b>, this mapping won't apply

This is due to the fact that $extraNodeAssignments is not used when user is updated. It is only used when user is created.
Would somebody know how to improve that because I'm a total newbie in Ez developpment and can't see how to do that ?

Best regards,

Artturi

Artturi Markko

Sunday 14 May 2006 3:38:47 am

Reply to myself : I've found that an existing cronjob script is in charge of the update :

cronjobs/ldapusermanage.php

However, things are not 100% ok for me as group removal does not work as I expect.
If interested, see this post :
http://ez.no/community/forum/developer/node_group_assignment_how_to_remove_correctly

Regards,

Artturi

eZ debug

Timing: Jan 31 2025 03:29:14
Script start
Timing: Jan 31 2025 03:29:14
Module start 'content'
Timing: Jan 31 2025 03:29:14
Module end 'content'
Timing: Jan 31 2025 03:29:15
Script end

Main resources:

Total runtime0.3641 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0090 587.9297370.2969
Module start 'content' 0.00910.0155 958.22661,005.5313
Module end 'content' 0.02450.3395 1,963.75783,900.9375
Script end 0.3640  5,864.6953 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00521.4363200.0003
Check MTime0.00140.3755200.0001
Mysql Total
Database connection0.00070.180210.0007
Mysqli_queries0.251268.98381410.0018
Looping result0.00160.44111390.0000
Template Total0.339293.210.3392
Template load0.00080.212010.0008
Template processing0.338492.938510.3384
Override
Cache load0.00060.156810.0006
Sytem overhead
Fetch class attribute can translate value0.00210.574810.0021
XML
Image XML parsing0.00030.069210.0003
General
dbfile0.01524.1830200.0008
String conversion0.00000.002030.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs