Automatic assignment of user in groups

Author Message

Lex 007

Thursday 10 March 2005 8:50:38 am

Hello,

I would like to write a cronjob to automatically assign users to user groups according to their attributes, after each user creation or modification (using EZ publish 3.5.1).

My main to problems are :
- Flushing all groups of a user (except main location)
- Adding a user to an existing node

So far I have found a function to add an object to an existing node :

function & copyto (& $object,$node){
	$nodeAssignment =& eZNodeAssignment::create( array
		    	(
                  'contentobject_id' => $object->attribute( 'id' ),
                  'contentobject_version' => $object->attribute( 'current_version' ),
                  'parent_node' => $node,
                  'is_main' => 0
                 )
            );
    $nodeAssignment->store();
    $treenodegewerk = & eZContentObjectTreeNode::addChild($object->attribute( 'id' ),$node,true);
    
	$operationResult = eZOperationHandler::execute(
									'content', 'publish', array( 
									'object_id' => $object->attribute( 'id' ),
									'version' => $object->attribute( 'current_version' ) ) );
	 return eZContentObjectTreeNode::findNode($node,$object->attribute('id'),true); 
}

The function *almost* works fine : when I add a node assignment to a user, the $user->groups() shows me that the group has been added. BUT ... in the admin interface the assignment is not shown : there is just a sort of blank line displayed in the "Location" tab of the user.

Thanks for your help.

Lex 007

Friday 11 March 2005 12:51:14 am

Still with teh same problem ...

I have noticed that the table 'ezcontentobject_tree' is not filled with the right data (paths and version are missing). I tried to modified the code above but it didn't change anything ...

Please help !

Lex 007

Friday 11 March 2005 2:46:46 am

I made it. Code based on the action.php provided in eZ Publish. Too bad I didn't get any answer from this Forum once again ...

Alex

// *****************************************************************************************************
// 
// FUNCTION addLocation : add node location for an object
// $objectID  : object ID of the object we want to move
// $newNodeID : node ID of the new location we want to add for this object
//
// *****************************************************************************************************
function addLocation($objectID,$newNodeID) {

    $object =& eZContentObject::fetch( $objectID );
    $nodeID = $object->attribute("main_node_id");
    $version =& $object->currentVersion();
    $class =& $object->contentClass();
    
    $existingNode =& eZContentObjectTreeNode::fetch( $nodeID );
    
    $selectedNodeIDArray = array($newNodeID);
    $assignedNodes =& $version->nodeAssignments();
    $assignedIDArray = array();
    $setMainNode = false;
    $hasMainNode = false;
    foreach ( $assignedNodes as $assignedNode )
    {
        $assignedNodeID = $assignedNode->attribute( 'parent_node' );
        if ( $assignedNode->attribute( 'is_main' ) )
            $hasMainNode = true;
        $assignedIDArray[] = $assignedNodeID;
    }
    if ( !$hasMainNode )
        $setMainNode = true;
    
    $mainNodeID = $existingNode->attribute( 'main_node_id' );
    $objectName = $object->attribute( 'name' );
    foreach ( $selectedNodeIDArray as $selectedNodeID )
    {
        if ( !in_array( $selectedNodeID, $assignedIDArray ) )
        {
            $isPermitted = true;
            $parentNode =& eZContentObjectTreeNode::fetch( $selectedNodeID );
            $parentNodeObject =& $parentNode->attribute( 'object' );
    
            $canCreate = $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1;
            if ( $isPermitted )
            {
                $isMain = 0;
                if ( $setMainNode )
                    $isMain = 1;
                $setMainNode = false;
                $nodeAssignment =& $version->assignToNode( $selectedNodeID, $isMain );
                $newNode =& $parentNode->addChild( $object->attribute( 'id' ), 0, true );
                $newNode->setAttribute( 'sort_field', $nodeAssignment->attribute( 'sort_field' ) );
                $newNode->setAttribute( 'sort_order', $nodeAssignment->attribute( 'sort_order' ) );
                $newNode->setAttribute( 'contentobject_version', $version->attribute( 'version' ) );
                $newNode->setAttribute( 'contentobject_is_published', 1 );
                $newNode->setAttribute( 'main_node_id', $mainNodeID );
                $newNode->setName( $objectName );
                $newNode->updateSubTreePath();
                $newNode->store();
                eZContentObjectTreeNode::updateNodeVisibility( $newNode, $parentNode, false );
            }
        }
    }
    include_once( 'kernel/content/ezcontentoperationcollection.php' );
    eZContentOperationCollection::clearObjectViewCache( $objectID, true );
    eZContentObject::expireTemplateBlockCacheIfNeeded();
} // END function addLocation

Heiner Wurbs

Friday 18 March 2005 10:02:25 am

Thanks very much for your posting, it helped me lot!!!
Heiner

Lex 007

Monday 21 March 2005 12:19:13 am

You're welcome :)

Milad P

Tuesday 25 July 2006 8:10:27 am

I worked well, but how do i remove a location correctly from a user? With my current code i can remove locations but i can't add same user to the group again.


$thenode =& eZContentObjectTreeNode::fetch( $nodeID );
 $thenode->remove();

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 15:58:04
Script start
Timing: Jan 18 2025 15:58:04
Module start 'layout'
Timing: Jan 18 2025 15:58:04
Module start 'content'
Timing: Jan 18 2025 15:58:05
Module end 'content'
Timing: Jan 18 2025 15:58:05
Script end

Main resources:

Total runtime0.9942 sec
Peak memory usage4,096.0000 KB
Database Queries68

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0045 588.0313152.6406
Module start 'layout' 0.00450.0026 740.671939.4609
Module start 'content' 0.00710.9853 780.1328583.3672
Module end 'content' 0.99240.0018 1,363.500020.1563
Script end 0.9942  1,383.6563 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00320.3246160.0002
Check MTime0.00130.1322160.0001
Mysql Total
Database connection0.00050.048310.0005
Mysqli_queries0.936894.2227680.0138
Looping result0.00070.0744660.0000
Template Total0.961896.720.4809
Template load0.00220.217820.0011
Template processing0.959796.524320.4798
Template load and register function0.00010.011410.0001
states
state_id_array0.00100.102210.0010
state_identifier_array0.00110.106020.0005
Override
Cache load0.00190.1868320.0001
Sytem overhead
Fetch class attribute can translate value0.00050.045430.0002
Fetch class attribute name0.00100.101670.0001
XML
Image XML parsing0.00190.186930.0006
class_abstraction
Instantiating content class attribute0.00000.0019100.0000
General
dbfile0.00240.2384160.0001
String conversion0.00000.000840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
8content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 25
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs