Trying to use RoleUserBridge
so that when registering using zfcuser
, user_role_linker
database table will get updated automatically.
I have done the following:
1) successfully copied the directory in vendor.
2) copy linker.config.php to config/autoconfig directory
But, registering a new user using zfcuser doesn't add a table entry to user_role_linker database.
I checked the user_role_mapper
function inside RoleUserBridge/Module.php
's factories
'factories' => array(
'user_role_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new Mapper\RoleMapper();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator(new \ZfcUser\Mapper\UserHydrator());
return $mapper;
},
)
This gets called (I put a die()
to test).
Next, I put a die in RoleUserBridge\Mapper\RoleMapper.php
's insert function. But, this never gets called.
So, what's happening is that the factory is creating an instance, but the insert function never gets executed.
public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
{
$options = $this->getOptions();
$tableName = $options['user_role_linker'];
$result = parent::insert($entity, $tableName, $hydrator);
var_dump($entity);
var_dump($hydrator);
var_dump($result);
die();
return $result;
}
Maybe I'm not linking something correctly...
My load order is:
'modules' => array(
// loads all 3rd party modules first
'ZfcBase',
'ZfcUser',
'ZfcAdmin',
'BjyAuthorize',
'RoleUserBridge',
...
Please advice!
edit on Nov 8th based on Rajeeb's feedback
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$serviceManager = $e->getApplication()->getServiceManager();
//zfcuser_user_service --- provided by ZfcUser module
$userService = $serviceManager->get('zfcuser_user_service');
//handling "register.post" event
$userService->getEventManager()->attach('register.post',
function(\Zend\EventManager\Event $e) use ($serviceManager) {
//get user entity from event params
$user = $e->getParam('user');
//this is my own "userrole" service, you can target to "userrolelinker" service
$userRoleService = $serviceManager->get('UserRole\Service\UserRoleService');
//below line is actually calling the insert functionallity via service
//if you would like can also be done by "mapper" class also without
//the involvement of "service" class
die('boo');
$userRoleService->insertUserRole(array(
'userId' => $user->getId(),
'roleId' => 3 //my target role id for the new users
));
}
);
}
Cheers,
Justin