6
votes

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?

});
4

4 Answers

7
votes

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

5
votes

Maybe it's not the best solution, but it works for me. Add user_role_id option in config scope.

    public function onBootstrap(MvcEvent $mvcEvent)
    {
        $zfcServiceEvents = $mvcEvent->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
        $zfcServiceEvents->attach('register', function($e) use($mvcEvent) {
            $user = $e->getParam('user');
            $em = $mvcEvent->getApplication()->getServiceManager()->get('doctrine.entitymanager.orm_default');
            $config = $mvcEvent->getApplication()->getServiceManager()->get('config');
            $defaultUserRole = $em->getRepository('SamUser\Entity\Role')->find($config['user_role_id']);
            $user->addRole($defaultUserRole);
        });
    }

Maybe someone will offer better solution.

1
votes

This work too.

  public function onBootstrap(MvcEvent $mvcEvent)
     {
         $zfcServiceEvents = $mvcEvent->getApplication()-getServiceManager()->get('zfcuser_user_service')->getEventManager();
         $zfcServiceEvents->attach('register', function($e) use($mvcEvent) {
             $user = $e->getParam('user');
             $em = $mvcEvent->getApplication()->getServiceManager()-get('doctrine.entitymanager.orm_default');
             $defaultUserRole = $em->getRepository('SamUser\Entity\Role')-find('id_of_your_role_on_table_role_for_example: '2'');
             $user->addRole($defaultUserRole);
         });
     }
0
votes

I just used a MySQL trigger

DROP TRIGGER IF EXISTS `user_role_after_insert_trig`;
DELIMITER //
CREATE TRIGGER `user_role_after_insert_trig` AFTER INSERT ON `user`
 FOR EACH ROW begin
  insert into user_role_linker (user_id,role_id) values (new.user_id, 5);
end
//
DELIMITER ;