Forums / General / Import Users

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 :)