phpzend-frameworkzend-framework2zfcuserzend-framework3

Cannot find event from looking at ZfcUser User Mapper code


I was reading the UserMapper code from the ZfcUser Zend Framework Module because i thought i could get a general concept of using zends auth system before i build out mine and was curious about something.

In it (click here), on line 19, we have the following code:

$this->getEventManager()->trigger('find', $this, array('entity' => $entity));

I really want to know where i can find the "find" event thats being triggered or is it a placeholder for a possible find event in the future

I looked at the Module file and a few others and cannot locate it


Solution

  • Zfcuser is a very complete module with lot of features that in normal basic authen you wouldn't need. These features include several event that ZfcUser trigger but it don't need it, it is just here in case a user need to perform a specific action on this event.

    More, ZfcUsercan be used with other modules as BjyAuthorize and roleuserbridge to provide more functionnality (acl and user role management...), and these third-party modules can use the events triggered by ZfcUser.

    For example, you want to log every user authentication, you can use the authenticate event this way in your onBootstrap() method: (example from here):

    $sm = $e->getApplication()->getServiceManager();
    $zfcAuthEvents = $e->getApplication()->getServiceManager()->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();
    
    $zfcAuthEvents->attach( 'authenticate', function( $authEvent ) use( $sm ){
        try {
            $remote     = new \Zend\Http\PhpEnvironment\RemoteAddress;
            $remote->setUseProxy( true );
    
            $mapper     =  $sm->get( 'auth_log_mapper' );
            $log_entity = new \FooUser\Entity\UserAuthenticationLog;
            $log_entity->populate(
                    array(
                   'user_id'       => $authEvent->getIdentity(),
                    'auth_time'     => new \DateTime( "now" ),
                    'ip_address'    => $remote->getIpAddress()
                )
            );
    
            $mapper->save( $log_entity );
            return true;
        } catch( \Exception $x ) {
            // handle it
        }
    });
    

    So the find event may not be usefull for ZfcUser, it is not a placeholder for a future development of this module, it is a hook for you, if you need to do something when a user is found in the users repository...