i'm using zend framework 2 zfcuser, bjyauthorize , doctrine database. registration etc. works far. problem is, registered users have no role assigned, want add role "user" user during registration.
i think attach "register" event, don't know how that.
i hope can me ...
(i used tutorial setting 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'); // role must added here, $user->addrole(); // how user role entity add db? });
building on dangelzm's answer, , using reference (see link @ end of post) event manager, came solution organizes potential zfcuser event listeners out user listener object.
note: created own user module called nvuser, depending on name of module you'll have replace references of nvuser user module name.
summary
i created nvuserlistener object can attach event listeners shared event manager, , house 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'); // after user has registered } }
inside nvuser/config/module.config.php:
<?php namespace nvuser; return array( 'zfcuser' => array( 'new_user_default_role' => 'user', ), );
Comments
Post a Comment