1

我正在使用bjyauthorize并且对现有用户非常有效。我现在需要动态添加用户。

我可以在表中手动设置User和之间的连接,但是如何按照新用户的方式设置?也许我错过了一些微不足道的事情?RoleUserRoleLinker

谢谢!

4

2 回答 2

5

ZfcUser User服务在用户插入数据库后触发一个register.post事件,因此您只需要监听该事件。您可以通过在模块引导程序中将侦听器附加到共享事件管理器来做到这一点。

您的侦听器接收到的事件实例包含用户服务本身,加上user数据,以及form注册期间使用的数据,这应该足以建立链接

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getApplication();
    $events = $app->getEventManager();
    $shared = $events->getSharedManager();
    $sm = $app->getServiceManager();

    $shared->attach('ZfcUser\Service\User', 'register.post', function ($e) use ($sm) {
         $userService = $e->getTarget();
         $newUser = $e->getParam('user');
         $registrationForm = $e->getParam('form');
         // do something with the new user info, eg, assign new user role...
    });
}
于 2013-05-23T11:19:35.723 回答
0

这是完整的解决方案:

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    //You need a copy of the service manager and it has to be set as a member for the lambda function to call it
    $this->sm = $e->getApplication()->getServiceManager();

    $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();

    $zfcServiceEvents->attach('register.post', function($e) {

        /** @var \User\Entity\UserPdo $user */
        $user = $e->getParam('user');

        //This is the adapter that both bjyAuthorize and zfcuser use
        $adapter     = $this->sm->get('zfcuser_zend_db_adapter');

        //Build the insert statement
        $sql = new \Zend\Db\Sql\Sql($adapter);

        //bjyAuthorize uses a magic constant for the table name
        $insert = new \Zend\Db\Sql\Insert('user_role_linker');
        $insert->columns(array('user_id', 'role_id'));
        $insert->values(array('user_id' => $user->getId(), 'role_id' => 'user'), $insert::VALUES_MERGE);

        //Execute the insert statement
        $adapter->query($sql->getSqlStringForSqlObject($insert), $adapter::QUERY_MODE_EXECUTE);
于 2015-03-25T02:42:05.867 回答