Is there any way to run some code after a built-in API action?
Specifically, I want to do something after a user enables 2FA. However, it seems none of the PHPMaker events are triggered after an API request completes.
Is there any way to run some code after a built-in API action?
Specifically, I want to do something after a user enables 2FA. However, it seems none of the PHPMaker events are triggered after an API request completes.
I don't see such events in PHPMaker. However, when a user enables 2FA, the user's Profile field will be updated by Doctrine ORM (assumed v2024). So you may use the Doctrine's Lifecycle Events. You may write an event listener for the postUpdate event.
You may try your hand at it, for example,
MyEventListener
, implement your postUpdate
method, add your class in Global Code,Note also that there is a EntityManager() global function. (v2024)
If your code does not work, you may post it (and error messages in log file) for discussions.
In Global Code, I added:
class postUpdateListener
{
public function postUpdate(PostUpdateEventArgs $args)
{
if ($args->getEntity() instanceof users) {
if ($args->hasChangedField('profile')) {
Log("Profile changed");
}
}
}
}
In NameSpace_Use, I added:
use Doctrine\Common\EventManager;
use Doctrine\ORM\Events;
In Database_Connected, I added:
$eventManager = new EventManager();
$eventManager->addEventListener([Events::postUpdate], new postUpdateListener());
In Composer Packages, I added:
Then, I tried enabling 2FA for a user, but nothing was logged.
OK, I finally got it working by doing the following:
use Doctrine\ORM\Event\PostUpdateEventArgs;
$args->hasChangedField('profile')
as hasChangedField()
is not a method of the PostUpdateEventArgs
class, nor any of its ancestor classes.So now, the postUpdateListener
class looks like this:
class postUpdateListener
{
public function postUpdate(PostUpdateEventArgs $args)
{
if ($args->getObject() instanceof Entity\User) {
Log("User changed");
}
}
}
It executes every time the user table is updated. However, unlike PHPMaker's Row_Updated event, there seems to be no way to access the original field values ($rsold), nor any way to determine if a field has changed. Is it possible to get this information inside the event?