Right way to listen for changes in a user profile

Hi there, I have the requirement to build a functionality where I’ll send a mail to an admin user if any user belonging to a given group has taken any changes, my first approach was to create a plugin and attach it to the OnUserProfileSave event, but after testing I realized that both in my code, as well as MODx internally, there were calls to $profile->save();, thus generating the event every time as the modUserProfile is intended to do it:

public function save($cacheFlag = null)
{
$isNew = $this->isNew();

    if ($this->xpdo instanceof modX) {
        $this->xpdo->invokeEvent('OnUserProfileBeforeSave', array(
            'mode'        => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
            'userprofile' => &$this,
            'cacheFlag'   => $cacheFlag,
        ));
    }

    $saved = parent:: save($cacheFlag);

    if ($saved && $this->xpdo instanceof modX) {
        $this->xpdo->invokeEvent('OnUserProfileSave', array(
            'mode'        => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
            'userprofile' => &$this,
            'cacheFlag'   => $cacheFlag,
        ));
    }

    return $saved;
}

Having said that, what should be the right way to catch the and event related to a profile change?

It depends, where your users change their profile.
If they change it in the manager, then maybe you can use the event OnUserFormSave.
If they change it in the frontend using the “Login” extra, then maybe you can write a postHook-snippet.

2 Likes

It makes perfect sense, I’ll give it a try, thanks a lot!