API Requested Event (after a user enables 2FA)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
bkonia
User
Posts: 144

API Requested Event (after a user enables 2FA)

Post by bkonia »

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.


arbei
User
Posts: 9719

Post by arbei »

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,

  1. Copy the sample code of MyEventListener, implement your postUpdate method, add your class in Global Code,
  2. Copy the sample code for registering an event listener and add it to the page you want to use the event listener, e.g. in Page_Load server event.

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.


bkonia
User
Posts: 144

Post by bkonia »

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:

  • doctrine/event-manager
  • doctrine/common

Then, I tried enabling 2FA for a user, but nothing was logged.


arbei
User
Posts: 9719

Post by arbei »

You need to use the event manager of the connection, e.g. in Database_Connected

$eventManager = $conn->getEventManager();
$eventManager->addEventListener([Events::postUpdate], new postUpdateListener());

bkonia
User
Posts: 144

Post by bkonia »

OK, I finally got it working by doing the following:

  1. In the Namespace_Use event, I added use Doctrine\ORM\Event\PostUpdateEventArgs;
  2. Got rid of $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?


bkonia
User
Posts: 144

Post by bkonia »

I guess the solution is to use the preUpdate event, which supports the hasChangedField() method and allows you to access the old field values.


Post Reply