Aurora documentation

Log users into Helpdesk programmatically

The sample found here demonstrates use of Aurora API for logging user into Helpdesk account. If user is not found in Helpdesk accounts list, the account will be created and user will need to check their email to complete registration. And in case if user account is located and verified, they will be logged into that account. Complete API reference on CApiHelpdeskManager class is available.

In order to make use of Aurora API, you'll need to load the API library and make sure CApi class can be used:

include_once __DIR__.'/../libraries/afterlogic/api.php';

if (class_exists('CApi') && CApi::IsValid())
{
	...

It is assumed that the code itself is located in a subdirectory of main Aurora directory. If that's not the case, you'll need to adjust the filesystem path which points to api.php file. And of course, you can supply full filesystem path there, e.g.:

include_once '/var/www/aurora/libraries/afterlogic/api.php';

In order to log specific user into their Helpdesk account, LoginToHelpdeskAccount method is used. It takes 3 parameters: email address, account password and tenant hash. The password is used solely for managing Helpdesk account, it doesn't have to match actual email account password. Tenant value is only used on multi-tenant scenario while by default there are no tenants enabled so you just leave those values empty.

Below, please find sample code for logging a particular user into Aurora Helpdesk. For the sake of simplicity, email, password and tenant hash are supplied directly in the code. In actual application, they will probably be taken from session, POST data, etc.

This sample is also found in the package, see examples/login-to-helpdesk-account.php file. That code is disabled by default. To enable it, remove the line at the beginning which contains exit call.

API reference on CApiHelpdeskManager class

include_once __DIR__.'/../libraries/afterlogic/api.php';

if (class_exists('CApi') && CApi::IsValid())
{
	$sName = 'Username';
	$sEmail = 'user@domain.com';
	$sPassword = 'account-password-here';
	$sTenantHash = '';

	try
	{
		// Getting required API class
		$oApiIntegratorManager = CApi::Manager('integrator');
		$oApiHelpdeskManager = CApi::Manager('helpdesk');

		$iIdTenant = $oApiIntegratorManager->GetTenantIdByHash($sTenantHash);

		// checking existence of user
		$oUser = $oApiHelpdeskManager->GetUserByEmail($iIdTenant, $sEmail);

		if($oUser)
		{
			// attempting to obtain object for account we're trying to log into
			$oAccount = $oApiIntegratorManager->LoginToHelpdeskAccount($iIdTenant, $sEmail, $sPassword);

			if ($oAccount)
			{
				// populating session data from the account
				$oApiIntegratorManager->SetHelpdeskUserAsLoggedIn($oAccount, false);

				// redirecting to Aurora Helpdesk
				CApi::Location('../?helpdesk');
			}
			else
			{
				// login error
				echo $oApiIntegratorManager->GetLastErrorMessage();
			}
		}
		else
		{
			// registering helpdesk account
			if (!!$oApiIntegratorManager->RegisterHelpdeskAccount($iIdTenant, $sEmail, $sName, $sPassword))
			{
				echo 'account successfully registered, please confirm registration and login';
			}
		}
	}
	catch (Exception $oException)
	{
		$iCode = $oException->getCode();

		// if logged into Helpdesk account
		if($iCode === CApiErrorCodes::HelpdeskManager_AccountSystemAuthentication)
		{
			// redirecting to Aurora Helpdesk
			CApi::Location('../?helpdesk');
		}
		else
		{
			// login error
			echo $oException->getMessage();
		}
	}
}
else
{
	echo 'AfterLogic API isn\'t available';
}