zfcuser add user role after registration

前端 未结 4 2078
没有蜡笔的小新
没有蜡笔的小新 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:36

    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.

提交回复
热议问题