Skip to Content Skip to Menu

Community Builder provides some basic API for properly and easily creating or editing existing users while maintaining a complete structure within Joomla and Community Builder it self. Users created and edited in this manner will be completely functional and even be susceptible to basic fields validation!

To create a user you must first Include API externally if you are creating a user outside of a Community Builder plugin followed by including the Plugin & Class. Now that you've loaded the API you can include the following function for easy creation of users.

<?php
function registerUser( string $email, string $firstName = '', string $lastName = '', string $username = '', string $password = '', int $approve = 0, int $confirm = 0, array $usergroups = [] ): bool
{
	global $_CB_framework, $_PLUGINS, $ueConfig;

	if ( ! $email ) {
		// Email address is mandatory so just stop here if we don't have one
		return false;
	}

	$approval					=	( $approve === 2 ? \CBLib\Application\Application::Config()->getInt( 'reg_admin_approval', 0 ) : $approve );
	$confirmation				=	( $confirm === 2 ? \CBLib\Application\Application::Config()->getInt( 'reg_confirmation', 1 ) : $confirm );
	$name						=	[];

	if ( ! $usergroups ) {
		$gids					=	[ $_CB_framework->getCfg( 'new_usertype' ) ];
	}

	cbArrayToInts( $gids );

	$newUser					=	new \CB\Database\Table\UserTable();

	if ( ! $username ) {
		// Generate a random username if username isn't supplied
		$username				=	$newUser->getRandomPassword();
	}

	$newUser->set( 'gids', $gids );
	$newUser->set( 'sendEmail', 0 );
	$newUser->set( 'registerDate', \CBLib\Application\Application::Database()->getUtcDateTime() );
	$newUser->set( 'username', $username );
	$newUser->set( 'firstname', $firstName );
	$newUser->set( 'lastname', $lastName );

	if ( $newUser->getString( 'firstname', '' ) ) {
		$name[]					=	$newUser->getString( 'firstname', '' );
	}

	if ( $newUser->getString( 'lastname', '' ) ) {
		$name[]					=	$newUser->getString( 'lastname', '' );
	}

	$newUser->set( 'name', implode( ' ', $name ) );

	if ( ! $newUser->getString( 'name', '' ) ) {
		// A name of some kind is required by Joomla so lets fallback to username if none is supplied
		$newUser->set( 'name', $newUser->getString( 'username', '' ) );
	}

	$newUser->set( 'email', $email );

	$emailPassword				=	\CBLib\Application\Application::Config()->getInt( 'emailpass', 0 );

	if ( $password ) {
		// Keep track of cleartext password so we can restore it later for emails or trigger behavior
		$passwordClearText		=	$password;
	} else {
		// Generate a random password if none is supplied
		$ueConfig['emailpass']	=	1;

		$newUser->setRandomPassword();

		$passwordClearText		=	$newUser->getString( 'password', '' );
	}

	$newUser->set( 'password', $newUser->hashAndSaltPassword( $passwordClearText ) );

	if ( \CBLib\Application\Application::Config()->getBool( 'reg_ipaddress', true ) ) {
		$newUser->set( 'registeripaddr', \CBLib\Application\Application::Input()->getRequestIP() );
	}

	// Handle approval, confirmation, and block state for the user
	if ( $approval === 0 ) {
		$newUser->set( 'approved', 1 );
	} else {
		$newUser->set( 'approved', 0 );
	}

	if ( $confirmation === 0 ) {
		$newUser->set( 'confirmed', 1 );
	} else {
		$newUser->set( 'confirmed', 0 );
	}

	if ( ( $newUser->getInt( 'confirmed', 1 ) === 1 ) && ( $newUser->getInt( 'approved', 1 ) === 1 ) ) {
		$newUser->set( 'block', 0 );
	} else {
		$newUser->set( 'block', 1 );
	}

	// Establish mandatory Joomla defaults
	if ( $newUser->getInt( 'lastvisitDate' ) === null ) {
		$newUser->set( 'lastvisitDate', $newUser->getDbo()->getNullDate() );
	}

	if ( $newUser->getInt( 'activation' ) === null ) {
		$newUser->set( 'activation', $newUser->getDbo()->getNullDate() );
	}

	if ( $newUser->getInt( 'resetCount' ) === null ) {
		$newUser->set( 'resetCount', 0 );
	}

	if ( $newUser->getString( 'otpKey' ) === null ) {
		$newUser->set( 'otpKey', '' );
	}

	if ( $newUser->getString( 'otep' ) === null ) {
		$newUser->set( 'otep', '' );
	}

	if ( $newUser->getInt( 'requireReset' ) === null ) {
		$newUser->set( 'requireReset', 0 );
	}

	if ( ( $newUser->getString( 'authProvider' ) === null ) && checkJversion( '4.0+' ) ) {
		$newUser->set( 'authProvider', '' );
	}

	$_PLUGINS->trigger( 'onBeforeUserRegistration', [ &$newUser, &$newUser ] );

	if ( ! $newUser->store() ) {
		return false;
	}

	// If we're not confirmed then be sure the activation code is generated and stored
	if ( ( $newUser->getInt( 'confirmed', 1 ) === 0 ) && ( $confirmation !== 0 ) && ( ! $newUser->store() ) ) {
		return false;
	}

	$newUser->set( 'password', $passwordClearText );

	activateUser( $newUser, 1, 'UserRegistration' );

	$ueConfig['emailpass']		=	$emailPassword;

	$_PLUGINS->trigger( 'onAfterUserRegistration', [ &$newUser, &$newUser, true ] );

	return true;
}

Editing a user takes an additional step, you'll first need to first Establishing $user object once the object has been established you'll need to use the following modified function with $user object passed to it.

function editUser( $user, $name ) {
	global $_PLUGINS;

	$oldUserComplete		=	new \CB\Database\Table\UserTable();

	foreach ( array_keys( get_object_vars( $user ) ) as $k ) {
		if ( substr( $k, 0, 1 ) != '_' ) {
			$oldUserComplete->set( $k, $user->get( $k ) );
		}
	}
	
	$user->set( 'name', $name );
	
	$_PLUGINS->trigger( 'onBeforeUserUpdate', array( &$user, &$user, &$oldUserComplete, &$oldUserComplete ) );
	
	if ( ! $user->store() ) {
		return false;
	}
	
	$_PLUGINS->trigger( 'onAfterUserUpdate', array( &$user, &$user, $oldUserComplete ) );
	
	return true;
}

The newly created function will now register a new user or edit an existing user; there are many applications for such API such as external site integrations. In addition to the default Joomla and CB fields you can also add additional fields using the following method.

$user->set( 'FIELD_NAME', FIELD_VALUE );