本文介绍了注册后,zfcuser添加用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework 2与ZfcUser,BjyAuthorize和Doctrine数据库。注册等到目前为止工作得很好。我的问题是,注册用户没有分配任何角色,所以我想在注册期间向用户添加角色user。



我想我可以附加到注册事件,但我不知道该怎么做。



我希望有人可以帮助我...



(我使用本教程设置zfcuser等。)

  public function onBootstrap(MvcEvent $ e)
{
$ zfcServiceEvents = $ e-> getApplication() - > getServiceManager() - > get('zfcuser_user_service' > getEventManager();
$ zfcServiceEvents-> attach('register',function($ e){
$ user = $ e-> getParam('user');
//可能角色必须在这里添加$ user-> addRole();
//但是如何让用户从DB添加角色实体?

});


解决方案

基于DangelZM的答案,并使用另一个参考链接在我的帖子结尾)关于事件管理器,我想出了这个解决方案,将潜在的ZfcUser事件侦听器组织到用户侦听器对象中。



注意:我创建了自己的名为NvUser的用户模块,因此根据模块的名称,您必须将NvUser的所有引用替换为用户模块名称。



总结



我创建了一个NvUserListener对象,该对象本身可以将事件侦听器附加到共享事件管理器,并容纳事件侦听器回调。



Inside NvUser / Module.php:

  php 
命名空间NvUser;

使用Zend\Mvc\MvcEvent;
使用NvUser\Listener\NvUserListener;

class模块
{
public function onBootstrap(MvcEvent $ mvcEvent)
{
$ em = $ mvcEvent-> getApplication() - > getEventManager();
$ em-> attach(new NvUserListener());
}
}

内部NvUser / src / NvUser / Listener /NvUserListener.php:

 <?php 
命名空间NvUser\Listener;

使用Zend\EventManager\AbstractListenerAggregate;
使用Zend\EventManager\EventManagerInterface;
使用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'));
}

公共函数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');

//在用户注册后执行某项操作
}
}

内部NvUser / config / module.config.php:

  php 
命名空间NvUser;

返回数组(
'zfcuser'=>数组(
'new_user_default_role'=>'user',
),
);



参考文献:




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 assigned, so i want to add the role "user" to the user during registration.

I think i could attach this to the "register" event, but i don't know how to do that.

I hope someone can help me ...

(i used this tutorial for setting up 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');
        // probably the role must be added here, with $user->addRole();
        // but how do i get the user Role Entity to add from DB?

});
解决方案

Building on DangelZM's answer, and using another reference (see link at end of my post) about the Event Manager, I came up with this solution which organizes the potential ZfcUser event listeners out into a user listener object.

Note: I created my own user module called NvUser, so depending on the name of your module you'll have to replace all references of NvUser to your user module name.

Summary

I created an NvUserListener object that can itself attach event listeners to the shared event manager, and house the 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');

        // Do something after user has registered
    }
}

Inside NvUser/config/module.config.php:

<?php
namespace NvUser;

return array(
    'zfcuser' => array(
        'new_user_default_role' => 'user',
    ),
);

References:

Understanding the Zend Framework 2 Event Manager

这篇关于注册后,zfcuser添加用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:31