Forums / Developer / Automatic assignment of user in groups

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();