Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Framework 2-bjyauthorize-将新用户链接到角色_Php_Authentication_Zend Framework2_Bjyauthorize - Fatal编程技术网

Php Zend Framework 2-bjyauthorize-将新用户链接到角色

Php Zend Framework 2-bjyauthorize-将新用户链接到角色,php,authentication,zend-framework2,bjyauthorize,Php,Authentication,Zend Framework2,Bjyauthorize,我使用的是bjyauthorize,我对现有用户非常有用。我现在需要动态添加用户 我可以在表UserRoleLinker中手动设置User和Role之间的连接,但我该如何设置新用户的连接?也许我错过了一些琐碎的事情 谢谢 ZfcUserUser服务在用户插入数据库后触发一个寄存器。post事件,因此您只需监听该事件。您可以通过将侦听器附加到模块引导中的共享事件管理器来实现这一点 侦听器接收到的事件实例包含用户服务本身,加上注册期间使用的用户数据和表单,这应该足以创建链接 public funct

我使用的是
bjyauthorize
,我对现有用户非常有用。我现在需要动态添加用户

我可以在表
UserRoleLinker
中手动设置
User
Role
之间的连接,但我该如何设置新用户的连接?也许我错过了一些琐碎的事情


谢谢

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

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

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

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

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

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

以下是完整的解决方案:

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

以下是完整的解决方案:

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

谢谢你!你也有链接本身的例子吗?恐怕罗恩没有,我已经有一段时间没有使用bjyauth/zfcuser了,我在修补它的时候从来没有达到链接角色的程度。谢谢!你有链接本身的例子吗?恐怕没有Ron,我已经有一段时间没有使用bjyauth/zfcuser了,而且我在修补它的时候从来没有达到链接角色的程度。