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

eZ debug

Timing: Jan 18 2025 11:16:20
Script start
Timing: Jan 18 2025 11:16:20
Module start 'content'
Timing: Jan 18 2025 11:16:20
Module end 'content'
Timing: Jan 18 2025 11:16:20
Script end

Main resources:

Total runtime0.8859 sec
Peak memory usage4,096.0000 KB
Database Queries205

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0097 587.8125180.8281
Module start 'content' 0.00970.7215 768.6406588.4609
Module end 'content' 0.73120.1546 1,357.1016341.0859
Script end 0.8858  1,698.1875 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00420.4711210.0002
Check MTime0.00150.1660210.0001
Mysql Total
Database connection0.00340.379010.0034
Mysqli_queries0.800790.37922050.0039
Looping result0.00280.31232030.0000
Template Total0.852496.220.4262
Template load0.00210.233020.0010
Template processing0.850395.984620.4252
Template load and register function0.00010.013210.0001
states
state_id_array0.00270.306710.0027
state_identifier_array0.00090.102020.0005
Override
Cache load0.00180.2022320.0001
Sytem overhead
Fetch class attribute can translate value0.00120.136940.0003
Fetch class attribute name0.00170.194070.0002
XML
Image XML parsing0.00100.111040.0002
class_abstraction
Instantiating content class attribute0.00000.0029100.0000
General
dbfile0.00210.2353270.0001
String conversion0.00000.000730.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
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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 25
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs