Create a new user by script

Create a new user by script

Monday 27 April 2009 7:41:55 am - 2 replies

Author Message

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

You must be logged in to post messages in this topic!

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