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      = "j.doe@ez.no"

        $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.

eZ debug

Timing: Jan 30 2025 21:37:02
Script start
Timing: Jan 30 2025 21:37:02
Module start 'layout'
Timing: Jan 30 2025 21:37:02
Module start 'content'
Timing: Jan 30 2025 21:37:02
Module end 'content'
Timing: Jan 30 2025 21:37:02
Script end

Main resources:

Total runtime0.0240 sec
Peak memory usage6,144.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 588.1328151.2109
Module start 'layout' 0.00540.0045 739.3438220.6875
Module start 'content' 0.00990.0125 960.03131,001.9141
Module end 'content' 0.02240.0015 1,961.945333.9922
Script end 0.0239  1,995.9375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002611.0126140.0002
Check MTime0.00114.5452140.0001
Mysql Total
Database connection0.00062.544610.0006
Mysqli_queries0.003012.437530.0010
Looping result0.00000.066610.0000
Template Total0.00114.410.0011
Template load0.00083.490210.0008
Template processing0.00020.912810.0002
Override
Cache load0.00062.523710.0006
General
dbfile0.00031.279780.0000
String conversion0.00000.037840.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