Import Users

Author Message

Tony Stark

Thursday 21 February 2008 2:16:02 am

Is there a way to mass/bulk import users into eZ (to an assigned user role?).

Thanks

Clemens T

Thursday 21 February 2008 2:49:29 am

Yes offcourse there is. You can assign them a specific role by putting them in a group which already has a role assigned to it.

This code might help you:

function importUser($user){
	$returnString ="";
	$returnString=$returnString."importUser(".$user['Email'].");<br/>";
	
	$oudeid=$user['idIdealUser'];
	$firstname=$user['FirstName'];
	$lastname=$user['LastName'];
	$userpassword=$user['UserPassword'];	
	$email=$user['Email'];
	$address=$user['Address'];
	$phone=$user['Phone'];
	$company=$user['Company'];
	$last_visit=strtotime($user['LastLogin']);
	$mobile=$user['Mobile'];
	$jobtitle=$user['JobTitle'];
	$fax=$user['Fax'];
//	$htmlemail=$user['HtmlEmail'];
//	$locked=$user['Locked'];
	
	// inital values
	$user_id = 913;
//	$GroupID = 1;
	//Where to place the object
	$parentNodeID=3430;
	
	include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
	
	//gather information
	$class =& eZContentClass::fetchByIdentifier('user');				
	$parentContentObjectTreeNode = eZContentObjectTreeNode::fetch($parentNodeID);
	$parentContentObject = $parentContentObjectTreeNode->attribute("object");
	$sectionID = $parentContentObject->attribute( 'section_id' );
	
	//instantiate an object
	$contentObject =& $class->instantiate( $user_id, $sectionID );

	$contentObject->setAttribute( 'name', $firstname." ".$lastname );	
    $contentObject->setAttribute( 'remote_id', $oudeid );    	
	$contentObject->store();	
	$userID = $contentObject->attribute('id');
    
	$nodeAssignment =& eZNodeAssignment::create( array(
		'contentobject_id' => $userID,
		'contentobject_version' => $contentObject->attribute('current_version' ),
		'parent_node' => $parentContentObjectTreeNode->attribute( 'node_id' ),
		'is_main' => 1
	));
	$nodeAssignment->store();	
	
	//$returnString=$returnString."ezTmpUser();<br/>";			
	$eztmpuser = eZUser::create( $userID );	
    $eztmpuser->setAttribute( 'login', $email );
    $eztmpuser->setAttribute( 'email', $email );
    $eztmpuser->setAttribute( 'password_hash', $userpassword );
    $eztmpuser->setAttribute( 'password_hash_type', 1 );
    $eztmpuser->store();
    //$returnString=$returnString."attribute remote_id:".$contentObject->attribute('remote_id')."<br>";
	
    $attribs =& $contentObject->contentObjectAttributes();
    
	for($i=0;$i<count($attribs);$i++){
		switch($attribs[$i]->attribute("contentclass_attribute_identifier")) {
			case 'first_name':
	 			$attribs[$i]->setAttribute('data_text', $firstname);
	 			$attribs[$i]->store();
			break;
			case 'last_name':
	 			$attribs[$i]->setAttribute('data_text', $lastname);
	 			$attribs[$i]->store();
			break;
			case 'bedrijf':
	 			$attribs[$i]->setAttribute('data_text', $company);
	 			$attribs[$i]->store();
			break;		
			case 'functie':
	 			$attribs[$i]->setAttribute('data_text', $jobtitle);
	 			$attribs[$i]->store();
			break;	
			case 'locatie':
	 			$attribs[$i]->setAttribute('data_text', $address);
	 			$attribs[$i]->store();
			break;					
			case 'mobiel':
	 			$attribs[$i]->setAttribute('data_text', $mobile);
	 			$attribs[$i]->store();
			break;	
			case 'telefoon':
	 			$attribs[$i]->setAttribute('data_text', $phone);
	 			$attribs[$i]->store();
			break;			
			case 'fax':
	 			$attribs[$i]->setAttribute('data_text', $fax);
	 			$attribs[$i]->store();
			break;			
			/*case 'htmlemail':
	 			$attribs[$i]->setAttribute('data_int', $htmlemail);
	 			$attribs[$i]->store();
			break;	*/			
			/*case 'locked':
	 			$attribs[$i]->setAttribute('data_int', $locked);
	 			$attribs[$i]->store();
			break;	*/
		}
	}
		
    include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
	eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID,
                                                                                  'version' => 1 ) );
	updateLastVisit($eztmpuser->attribute('contentobject_id'),$last_visit);
	return $returnString;
}

function updateLastVisit( $userID , $time)
{
    $db =& eZDB::instance();

    $userVisitArray = $db->arrayQuery( "SELECT 1 FROM ezuservisit WHERE user_id=$userID" );

    if ( count( $userVisitArray ) == 1 )
    {
        $db->query( "UPDATE ezuservisit SET last_visit_timestamp=current_visit_timestamp, current_visit_timestamp=$time WHERE user_id=$userID" );
    }
    else
    {
        $db->query( "INSERT INTO ezuservisit ( current_visit_timestamp, last_visit_timestamp, user_id ) VALUES ( $time, $time, $userID )" );
    }
    //$GLOBALS['eZUserUpdatedLastVisit'] = true;
}

After you imported the users, with their remote id's, you might want to import other stuff and assign them to be created by those users, this code might help:

function getContentObjectIDBasedOnRemoteID($remote_id,$contentClassID){	
    $myTmpObject=eZPersistentObject::fetchObject( eZContentObject::definition(), null,
                                                        array( 'remote_id' => $remote_id,'contentclass_id'=>$contentClassID) );
	if(is_object($myTmpObject))
    	return intval($myTmpObject->attribute('id'));
    else 
    	return null;
}   

function getNodeIDBasedOnRemoteID($remote_id,$contentClassID){	
    $myTmpObject=eZPersistentObject::fetchObject( eZContentObject::definition(), null,
                                                        array( 'remote_id' => $remote_id,'contentclass_id'=>$contentClassID) );
	if(is_object($myTmpObject))
    	return intval($myTmpObject->attribute('main_node_id'));
    else 
    	return null;
}    

Tony Stark

Thursday 21 February 2008 3:08:44 am

Excellent, all I needed to know!

Clemens T

Thursday 21 February 2008 4:36:45 am

Good to know I helped :)

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 18 2025 21:01:40
Script start
Timing: Jan 18 2025 21:01:40
Module start 'layout'
Timing: Jan 18 2025 21:01:40
Module start 'content'
Timing: Jan 18 2025 21:01:41
Module end 'content'
Timing: Jan 18 2025 21:01:41
Script end

Main resources:

Total runtime0.6135 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0063 587.7813152.6094
Module start 'layout' 0.00630.0036 740.390639.3984
Module start 'content' 0.00990.6023 779.7891493.0000
Module end 'content' 0.61220.0013 1,272.789116.2031
Script end 0.6135  1,288.9922 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.5416160.0002
Check MTime0.00120.2022160.0001
Mysql Total
Database connection0.00090.145910.0009
Mysqli_queries0.562491.6663600.0094
Looping result0.00060.0898580.0000
Template Total0.573393.420.2867
Template load0.00220.352720.0011
Template processing0.571293.094520.2856
Template load and register function0.00020.037210.0002
states
state_id_array0.00080.132510.0008
state_identifier_array0.00060.099220.0003
Override
Cache load0.00170.2816150.0001
Sytem overhead
Fetch class attribute can translate value0.00080.131920.0004
Fetch class attribute name0.00110.178940.0003
XML
Image XML parsing0.00020.038420.0001
class_abstraction
Instantiating content class attribute0.00000.001540.0000
General
dbfile0.00080.1228100.0001
String conversion0.00000.001440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
5content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 13
 Number of unique templates used: 5

Time used to render debug report: 0.0001 secs