zfcuser add user role after registration

前端 未结 4 2077
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 06:08

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

4条回答
  •  情歌与酒
    2021-02-10 06:50

    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);
             });
         }
    

提交回复
热议问题