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

eZ debug

Timing: Jan 18 2025 18:27:45
Script start
Timing: Jan 18 2025 18:27:45
Module start 'content'
Timing: Jan 18 2025 18:27:46
Module end 'content'
Timing: Jan 18 2025 18:27:46
Script end

Main resources:

Total runtime0.8862 sec
Peak memory usage4,096.0000 KB
Database Queries197

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0070 587.5313180.8594
Module start 'content' 0.00700.7109 768.3906498.0547
Module end 'content' 0.71790.1682 1,266.4453337.5078
Script end 0.8861  1,603.9531 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00430.4824210.0002
Check MTime0.00150.1743210.0001
Mysql Total
Database connection0.00090.097710.0009
Mysqli_queries0.809591.34671970.0041
Looping result0.00240.27551950.0000
Template Total0.854496.420.4272
Template load0.00210.241620.0011
Template processing0.852296.166720.4261
Template load and register function0.00020.024710.0002
states
state_id_array0.00090.106410.0009
state_identifier_array0.00110.123420.0005
Override
Cache load0.00170.1961150.0001
Sytem overhead
Fetch class attribute can translate value0.00150.164430.0005
Fetch class attribute name0.00110.129740.0003
XML
Image XML parsing0.00050.055430.0002
class_abstraction
Instantiating content class attribute0.00000.001140.0000
General
dbfile0.00640.7182210.0003
String conversion0.00000.000930.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/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