Assign roles dynamically on login

Author Message

Sergei Chusmah

Thursday 17 April 2008 1:41:26 am

I'd like to know which is the best way to assign roles to users from php code.

I've created a custom loginhandler, and depending on the password the user types on login, they should be assigned to a role or other.

I think one way is to assign the user to a group which has the role needed, but this involves the publishing of a new version of the user object (is there another way?) Something like this (code from the ldap login handler):

                   if ( $defaultUserPlacement != $parentNodeID )
                    {
                        $newVersion =& $contentObject->createNewVersion();
                        $newVersion->assignToNode( $defaultUserPlacement, 1 );
                        $newVersion->removeAssignment( $parentNodeID );
                        $newVersionNr = $newVersion->attribute( 'version' );
                        include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
                        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID,
                                                                                                     'version' => $newVersionNr ) );
                    }

But this doesn't seem to be working for me... maybe I misplaced a object id for a node id or something... does anyone know if this is the correct approach?

Is there a way for assigning roles directly (without changing the user's group)?

Many thanks. (BTW I'm using 3.6)

Nicolas Lescure

Thursday 17 April 2008 4:13:51 am

You're right. Move the user in the user group which has the role you need. When you move it, you don't have to publish another version of the user.

Use this :

eZContentObjectTreeNodeOperations::move( $userObject->mainNodeID(), $GroupNodeId )

It should work.

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 31 2025 06:09:41
Script start
Timing: Jan 31 2025 06:09:41
Module start 'layout'
Timing: Jan 31 2025 06:09:41
Module start 'content'
Timing: Jan 31 2025 06:09:41
Module end 'content'
Timing: Jan 31 2025 06:09:41
Script end

Main resources:

Total runtime0.0175 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0047 588.1406151.2109
Module start 'layout' 0.00470.0034 739.351636.6641
Module start 'content' 0.00810.0075 776.015690.1875
Module end 'content' 0.01560.0019 866.203129.9922
Script end 0.0175  896.1953 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002514.0584140.0002
Check MTime0.00116.1188140.0001
Mysql Total
Database connection0.00073.948710.0007
Mysqli_queries0.002715.433530.0009
Looping result0.00000.164410.0000
Template Total0.00116.510.0011
Template load0.00094.874110.0009
Template processing0.00031.630610.0003
Override
Cache load0.00063.384810.0006
General
dbfile0.002112.195580.0003
String conversion0.00000.057140.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

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

Time used to render debug report: 0.0001 secs