Forums / Developer / Assign roles dynamically on login

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.