zfcuser add user role after registration

bjyauthorize, doctrine-orm, zend-framework, zfcuser

Solution

Building on DangelZM's answer, and using another reference (see link at end of my post) about the Event Manager, I came up with this solution which organizes the potential ZfcUser event listeners out into a user listener object.

Note: I created my own user module called NvUser, so depending on the name of your module you'll have to replace all references of NvUser to your user module name.

Summary

I created an NvUserListener object that can itself attach event listeners to the shared event manager, and house the event listener callbacks.

Inside NvUser/Module.php:

<?php
namespace NvUser;

use Zend\Mvc\MvcEvent;
use NvUser\Listener\NvUserListener;

class Module
{
    public function onBootstrap(MvcEvent $mvcEvent)
    {
        $em = $mvcEvent->getApplication()->getEventManager();
        $em->attach(new NvUserListener());
    }               
}

Inside NvUser/src/NvUser/Listener/NvUserListener.php:

<?php
namespace NvUser\Listener;

use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\Event;

class NvUserListener extends AbstractListenerAggregate
{
    public function attach(EventManagerInterface $events)
    {
        $sharedManager = $events->getSharedManager();
        $this->listeners[] = $sharedManager->attach('ZfcUser\Service\User', 'register', array($this, 'onRegister'));
        $this->listeners[] = $sharedManager->attach('ZfcUser\Service\User', 'register.post', array($this, 'onRegisterPost'));
    }

    public function onRegister(Event $e)
    {
        $sm = $e->getTarget()->getServiceManager();
        $em = $sm->get('doctrine.entitymanager.orm_default');
        $user = $e->getParam('user');
        $config = $sm->get('config');
        $criteria = array('roleId' => $config['zfcuser']['new_user_default_role']);
        $defaultUserRole = $em->getRepository('NvUser\Entity\Role')->findOneBy($criteria);

        if ($defaultUserRole !== null)
        {
            $user->addRole($defaultUserRole);
        }
    }

    public function onRegisterPost(Event $e)
    {
        $user = $e->getParam('user');
        $form = $e->getParam('form');

        // Do something after user has registered
    }
}

Inside NvUser/config/module.config.php:

<?php
namespace NvUser;

return array(
    'zfcuser' => array(
        'new_user_default_role' => 'user',
    ),
);

References:

Understanding the Zend Framework 2 Event Manager

Problem

I'm using Zend Framework 2 with ZfcUser, BjyAuthorize and Doctrine for the database. Registration etc. works very well so far. My problem is, that registered users have no role assigned, so i want to add the role "user" to the user during registration. I think i could attach this to the "register" event, but i don't know how to do that. I hope someone can help me ... (i used this tutorial for setting up zfcuser etc. http://samminds.com/2013/03/zfcuser-bjyauthorize-and-doctrine-working-together/) ``` public function onBootstrap(MvcEvent $e) { $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager(); $zfcServiceEvents->attach('register', function($e) { $user = $e->getParam('user'); // probably the role must be added here, with $user->addRole(); // but how do i get the user Role Entity to add from DB? }); ```

Original source