Custom services¶
Contents
Custom services allow you to add simple extensions to the LoginAPI core. We already a provide a number of different interfaces as extension points. Simple add a class in the custom directory and implement the interface of interest.
Currently we provide the following custom services:
Name | Description
|
---|---|
LogBackend | If you want to provide your own backend for log messages.
DbLogger is already an example shipped with the SDK.
|
StateChangedListener | If you want to react on state changes - most important if a client got online
(like our example NewsletterSubscriber))
|
LocationService | For a location based setup implement this interface to provide your own
location IDs based on MAC addresses, VLANs, IACBOX IDs and others
|
RedirectService | For a custom redirect service implement this interface to provide your own
redirects based on your business logic.
|
To load a custom service add your class name to the config entry custom-services in conf/main.config. Multiple classes have to be space or comma seperated. Please note that the name of a class and file has to be the same (MyService -> filename Custom/MyService.php).
Example Implementation StateChangedListener¶
Execute custom code after a successful login with the payment plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
namespace Iacbox\LoginApi\Custom;
use Iacbox\LoginApi\Core\AbstractService;
use Iacbox\LoginApi\Core\Session;
use Iacbox\LoginApi\Core\StateChangedListener;
/**
* Sample implementation of a custom service
*/
class ExampleCustomService extends AbstractService implements StateChangedListener {
/**
* If you want to do something for example send an HTTP request to a CRM system do it in this method
*/
public function onStateChanged(Session $session, $oldState, $newState) {
if ($oldState != Session::ONLINE && $newState == Session::ONLINE && $session->authPlugin == 'payment') {
//do something here
}
}
}
|
Example Implementation LocationService¶
Execute custom Code for a customized LocationService
The method resolveLocationId() is necessary because it will be called automatically. Don’t change it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php
namespace Iacbox\LoginApi\Custom;
use Iacbox\LoginApi\Core\AbstractService;
use Iacbox\LoginApi\Core\LocationService;
use Iacbox\LoginApi\Core\LoginApiException;
use \PDO;
/**
* This is a sample implementation of a LocationService that gets the location mapping
* from a DB. Currently this can only be used on an external webserver with a DB.
*/
class DbLocationService extends AbstractService implements LocationService {
protected $connector = null;
public function onServiceLoaded() {
$this->connector = $this->factory->getRdmsConnector();
if ($this->connector == null) {
throw new LoginApiException('DbLocationService - no RdmsConnector available! Config present? use-rdbms');
}
}
public function resolveLocationId($requestFields) {
// Add your custom location code here
}
}
|
Example Implementation RedirectService¶
Example Redirect Service
The method doRedirects() of the interface RedirectService is called at the very end after a successful login for each client. You can redirect each client based on your business logic here. This method should exit after sending the redirect via a header.
Attention
Don’t forget that captive browsers of Android smartphones close after a successful login. You will see redirected websites only on iOS and desktop browsers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
namespace Iacbox\LoginApi\Custom;
use Iacbox\LoginApi\Core\AbstractService;
use Iacbox\LoginApi\Core\RedirectService;
use Iacbox\LoginApi\Core\LoginApiException;
class ExampleRedirectService extends AbstractService implements RedirectService {
protected $loginApi;
public function onServiceLoaded() {
$this->loginApi = $this->factory->getLoginApi();
}
public function doRedirect(Session $session) {
// Add your custom redirect code here
// For example ask a backend where to redirect the user
$url = getRedirectUrlFromBackendXy($session->mac);
header('Location: '.$url);
exit();
}
}
|