Viewing entries tagged
Zend Framework 2

Capturing auth events with ZfcUser

Had a hell of a time trying to find how to tap into ZfcUser's auth event (the registration events are really well documented, but this one...).  Figured I'd write about it while the glue-it-back-hair-glue gently massaged onto my scalp dries.

From ZF1 Zend_Db Queries to ZF2 \Zend\Db\Sql\Sql?

From ZF1 Zend_Db Queries to ZF2 \Zend\Db\Sql\Sql?

If you're converting over to ZF1 from ZF2, there's quite a few subtleties to bridge.  I hope this helps you bridge the gap between the two.  By no means thorough, I just wanted to highlight the close-but-not-quite elements that would helped me get kickstarted.

Also, if you're looking to connect two databases to ZF2, this will probably help. 

ZF2, Doctrine2, ZfcUser - customizing the registration form and storing the custom user entity

ZFCUser is a great module, really cleanly written, and while there are some modules out there that'll help you extend ZFCUser with custom profiles, my challenge was one where I had to retrofit an existing Doctrine entity for use with ZFCUser -- let me tell you it worked great.

There are a lot of posts on SO that have to do with granular pieces of extending the ZfcUser form, but nothing I could find that had a usable A to Z for Doctrine2 users.  I added a post on the ZFCUser Wiki about the storage piece, and thought I'd throw it in here to help the next wandering coder.

Assuming that you've done two things:

  •  set up your application.config.php to include ZfcBase, ZfcUser, ZfcUserDoctrineORM
  • created your own user Module (e.g., module/FooUser/) with your entities and schema already set up

Essentially, you have a working ZfcUser, but just need to know how to extend the form and store (quite a few reasonable tutorials out there if you're not here yet.  Find one, set it up, and then come back).

/module/FooUser/config/module.config.php

<?php

return array(
'doctrine' => array(
'driver' => array(
// overriding zfc-user-doctrine-orm's config
'zfcuser_entity' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => __DIR__ . '/../src/FooUser/Entity',
),

'orm_default' => array(
'drivers' => array(
'FooUser\Entity' => 'zfcuser_entity',
),
),
),
),

'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'FooUser\Entity\User',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
),
);

This tells your module config to use the entities you've set up in /modules/FooUser/src/FooUser/Entity/User.php for example, as the default ZfcUser User entity.

/module/FooUser/Module.php

<?php

namespace FooUser;

use Zend\Mvc\MvcEvent;

class Module {

public function getConfig()
{
return include __DIR__ . '/config/module.config.php';

}

public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

public function onBootstrap( MVCEvent $e )
{
$eventManager = $e->getApplication()->getEventManager();
$em = $eventManager->getSharedManager();
$em->attach(
'ZfcUser\Form\RegisterFilter',
'init',
function( $e )
{
$filter = $e->getTarget();
// do your form filtering here
}
);

// custom form fields

$em->attach(
'ZfcUser\Form\Register',
'init',
function($e)
{
/* @var $form \ZfcUser\Form\Register */
$form = $e->getTarget();
$form->add(
array(
'name' => 'username',
'options' => array(
'label' => 'Username',
),
'attributes' => array(
'type' => 'text',
),
)
);

$form->add(
array(
'name' => 'favorite_ice_cream',
'options' => array(
'label' => 'Favorite Ice Cream',
),
'attributes' => array(
'type' => 'text',
),
)
);
}
);

// here's the storage bit

$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();

$zfcServiceEvents->attach('register', function($e) {
$form = $e->getParam('form');
$user = $e->getParam('user');
/* @var $user \FooUser\Entity\User */
$user->setUsername( $form->get('username')->getValue() );
$user->setFavoriteIceCream( $form->get('favorite_ice_cream')->getValue() );
});

// you can even do stuff after it stores
$zfcServiceEvents->attach('register.post', function($e) {
/*$user = $e->getParam('user');*/
});
}
}

Hopefully this helps another soul out!  This is one way to slice the pie that woulda saved me a bunch of time.

Tack BjyAuthorize on top of this, and you're off to the races.

https://packagist.org/packages/bjyoungblood/BjyAuthorize

Good Luck!