ZF-Commons / zfc-rbac

Role-based access control module to provide additional features on top of Zend\Permissions\Rbac
BSD 3-Clause "New" or "Revised" License
181 stars 111 forks source link

Problème avec mon CustomRoleProvider #295

Closed shishi666 closed 9 years ago

shishi666 commented 9 years ago

Hello I have perhaps not understood the doc but I have declared this in my zfc_rbac.global.php file :

'role_provider_manager' => [
    'factories' => [
        'role_db_provider'  =>  'ShishiUser\Factory\RoleProviderFactory', 
     ]
], 
'role_provider' => [
    'role-db-provider' => [
    ], 
], 

My factory :

<?php
namespace ShishiUser\Factory;

use Zend\ServiceManager\FactoryInterface;
use ShishiUser\Role\RoleDbProvider;
class RoleProviderFactory implements FactoryInterface
{
 /* (non-PHPdoc)
     * @see \Zend\ServiceManager\FactoryInterface::createService()
     */
    public function createService(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
    {
        // TODO Auto-generated method stub
        $sm = $serviceLocator->get('ServiceManager');
        $roleRepository = $sm->get('shishi-user-role-repository');
        $roleDbProvider = new RoleDbProvider();
        $roleDbProvider->setRoleRepository($roleRepository);
        return $roleDbProvider;
    }
}
?>

And the RoleProviderClass :

<?php
namespace ShishiUser\Role;

use ZfcRbac\Role\RoleProviderInterface;
use ShishiUser\Repository\RoleRepository;
use ZfcRbac\Exception\RoleNotFoundException;
use Rbac\Role\HierarchicalRole;
use Rbac\Role\Role;
class RoleDbProvider implements RoleProviderInterface{
    public $roleRepository;
    public function getRoles(array $roleNames)
    {
         $roles = $this->roleRepository->findByLibelles($roleNames);
        $rbacRoles = [];
        if (count($roles) >= count($roleNames)) {
            foreach ($roles as $role){
                if ($role->getChilds() !== null){
                    $rbacRole = new HierarchicalRole($role->getRol_libelle());
                    foreach ( (array) $role->getChilds() as $childRole){
                        $rbacRole->addChild($childRole);
                    }
                }else{
                    $rbacRole = new Role($role->getRol_libelle());
                }
                $permissions = ($role->getRol_permissions() !== null) ? [] : $role->getRol_permissions();
                if ($role->getRol_permissions() !== null){
                    foreach ($permissions as $permission){
                        $rbacRole->addPermission($permission->getPerm_libelle());
                    }
                }
                $rbacRoles[] = $rbacRole;
            }
            return $rbacRoles;
        }
        // We have roles that were asked but couldn't be found in database... problem!
        foreach ($roles as &$role) {
            $role = $role->getName();
        }
        throw new RoleNotFoundException(sprintf('Some roles were asked but could not be loaded from database: %s', implode(', ', array_diff($roleNames, $roles))));
    }
    public function setOptions($options)
    {
        $this->options = $options;
        return $this;
    }
    public function setRoleRepository(RoleRepository $roleRepository)
    {
        $this->roleRepository = $roleRepository;
        return $this;
    }
}
?>

and i obtain this error :

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'ZfcRbac\Role\RoleProviderPluginManager::get was unable to fetch or create an instance for role-db-provider'

I try to retrieve roles from my database, could you explain me what I am doing wrong please? Thank you in advance cordially

danizord commented 9 years ago

Ping @bakura10 for transalation :P

bakura10 commented 9 years ago

Shishi, penses à écrire en anglais stp, je suis le seul à être français ici ^^. Pense également à ajouter le coloriage pour ton code, c'est impossible à lire (https://help.github.com/articles/github-flavored-markdown/#syntax-highlighting).

Haha yup @danizord, I'll try to help him ;).

shishi666 commented 9 years ago

I have edited my question

bakura10 commented 9 years ago

Hi,

I think the error come from this:

class RoleProviderFactory implements FactoryInterface
{
 /* (non-PHPdoc)
     * @see \Zend\ServiceManager\FactoryInterface::createService()
     */
    public function createService(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
    {
        // TODO Auto-generated method stub
        $sm = $serviceLocator->get('ServiceManager');
        $roleRepository = $sm->get('shishi-user-role-repository');
        $roleDbProvider = new RoleDbProvider();
        $roleDbProvider->setRoleRepository($roleRepository);
        return $roleDbProvider;
    }
}

This object is constructed using a plugin manager (the role provider plugin manager).

The $serviceLocator you received in the factory is a plugin manager. If you want to retrieve the main service locator, you have to replace:

$sm = $serviceLocator->get('ServiceManager');

to:

$sm = $serviceLocator->getServiceLocator();

getServiceLocator is a method defined on each plugin manager that allows to retrieve the amin plugin manager.

Let me know if that works!

Et merci d'avoir traduit en anglais ;).

shishi666 commented 9 years ago

Hi, I get the same error when i replace this code :

$sm = $serviceLocator->get('ServiceManager');

by

$sm = $serviceLocator->getServiceLocator();

the error :

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'ZfcRbac\Role\RoleProviderPluginManager::get was unable to fetch or create an instance for role-db-provider' in E:\Zend studio 12 Workspace\ShishiBlog\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:555 Stack trace: #0 E:\Zend studio 12 Workspace\ShishiBlog\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php(116): Zend\ServiceManager\ServiceManager->get('role-db-provide...', true) #1 E:\Zend studio 12 Workspace\ShishiBlog\vendor\zf-commons\zfc-rbac\src\ZfcRbac\Factory\RoleServiceFactory.php(56): Zend\ServiceManager\AbstractPluginManager->get('role-db-provide...', Array) #2 [internal function]: ZfcRbac\Factory\RoleServiceFactory->createService(Object(Zend\ServiceManager\ServiceManager), 'zfcrbacservicer...', 'ZfcRbac\\Service...') #3 E:\Zend studio 12 Workspace\ShishiBlog\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php( in E:\Zend studio 12 Workspace\ShishiBlog\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 555

If you want another code by example my module.config.php code, i can give it you

thanks a lot for your help

bakura10 commented 9 years ago

Ha maybe I get it. In your "role_provider" key you called it "role-db-provider", but in your plugin manager config you called it "role_db_provider". Those are different names!

danizord commented 9 years ago

I'll assume that the problem was the incorrect config key. Closing due to lack of activity.