Create a new user by script

Author Message

Michaƫl Todorovic

Monday 27 April 2009 7:41:55 am

Hello,
I'm using ez 4.1.0.
I would like to develop a script which will create a user in ez database. I tried to use code in kernel/classes/datatypes/ezuser/eztextfileuser.php but it doesn't work.
How can I create a user with its first name, last name and email ?

Thank you.

Bruce Morrison

Monday 27 April 2009 7:03:17 pm

Try something like

        $first_name = "John"
        $last_name  = "Doe"
        $email      = "[email protected]"

        $ini = eZINI::instance();
        $userClassID      = $ini->variable( "UserSettings", "UserClassID" );
        $userCreatorID    = $ini->variable( "UserSettings", "UserCreatorID" );
        $defaultSectionID = $ini->variable( "UserSettings", "DefaultSectionID" );
        $defaultUserPlacement   = $ini->variable( "UserSettings", "DefaultUserPlacement" );

        $class = eZContentClass::fetch( $userClassID );
        $userObject = $class->instantiate( $userCreatorID, $defaultSectionID );

        $userObject->store();

        $userID = $userObjectID = $userObject->attribute( 'id' );

        $version = $userObject->version( 1 );
        $version->setAttribute( 'modified', time() );
        $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
        $version->store();

        $user = eZUser::create( $userID );
        $user->setAttribute( 'login', $login );
        $user->setAttribute( 'email', $email );
        $user->setAttribute( 'password_hash', "" );
        $user->setAttribute( 'password_hash_type', 0 );
        $user->store();

        $contentObjectAttributes = $version->contentObjectAttributes();

        // TODO - move this to ini settings
        $firstNameIdentifier = 'first_name';
        $lastNameIdentifier = 'last_name';

        $firstNameAttribute = null;
        $lastNameAttribute = null;

        $parentNodeIDs[] = $defaultUserPlacement;
        $parentNodeIDs = array_unique( $parentNodeIDs );

        foreach( $contentObjectAttributes as $attribute )
        {
            if ( $attribute->attribute( 'contentclass_attribute_identifier' ) == $firstNameIdentifier )
            {
                $firstNameAttribute = $attribute;
            }
            else if ( $attribute->attribute( 'contentclass_attribute_identifier' ) == $lastNameIdentifier )
            {
                $lastNameAttribute = $attribute;
            }
        }

       if ( $firstNameAttribute )
        {
            $firstNameAttribute->setAttribute( 'data_text', $first_name );
            $firstNameAttribute->store();
        }
        if ( $lastNameAttribute )
        {
            $lastNameAttribute->setAttribute( 'data_text', $last_name );
            $lastNameAttribute->store();
        }

        $contentClass = $userObject->attribute( 'content_class' );
        $name = $contentClass->contentObjectName( $userObject );

       $userObject->setName( $name );

        reset( $parentNodeIDs );

        // prepare node assignments for publishing new user
        foreach( $parentNodeIDs as $parentNodeID )
        {
            $newNodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $userObjectID,
                'contentobject_version' => 1,
                'parent_node' => $parentNodeID,
                'is_main' => ( $defaultUserPlacement == $parentNodeID ? 1 : 0 ) ) );
            $newNodeAssignment->setAttribute( 'parent_remote_id', $parentNodeID );
            $newNodeAssignment->store();
        }

        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userObjectID, 'version' => 1 ) );

        eZUser::updateLastVisit( $userID );
        // Reset number of failed login attempts
        eZUser::setFailedLoginAttempts( $userID, 0 );

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Michal Slocinski

Friday 07 August 2009 4:31:06 pm

Bruce,

thanks for this :-)

Michal

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