Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role Handler isGranted usage in parent Admin Class not working #2605

Closed
webdevilopers opened this issue Dec 8, 2014 · 4 comments
Closed

Comments

@webdevilopers
Copy link
Contributor

I have a parent Admin Class Contract and a child Admin Class Bundle.

My roles are working correctely

role_hierarchy:
    ROLE_ADMIN:
        - ROLE_SONATA_ADMIN_CONTRACT_LIST
        - ROLE_SONATA_ADMIN_BUNDLE_LIST
    ROLE_SUPER_ADMIN:
        - ROLE_ADMIN 

when calling the links separately:

Now I am trying to link both lists:

         if ($this->isGranted('ROLE_SONATA_ADMIN_BUNDLE_LIST')) {
            $listMapper
                ->add('_action', 'actions', array(
                    'actions' => array(
                        'show' => array(),
                        'edit' => array(),
                        'show_bundles' => array('template' => 'AcmeContractBundle:ContractAdmin:list__bundles.html.twig'),
                    ))
                )
            ;
        } 

but the link does not appear. Is isGranted limited to the ROLEs on the current Admin Class e.g. LIST, SHOW etc?

Using the securityContext would work:

if ($this->securityContext->isGranted('ROLE_SONATA_ADMIN_BUNDLE_LIST')) {

I'm wondering because the docs mentions a similar EMAIL example:
Yon can also create your own permissions, for example EMAIL (which will turn into role ROLE_SONATA_ADMIN_DEMO_FOO_EMAIL).
http://sonata-project.org/bundles/admin/master/doc/reference/security.html#usage

My complete code:

Possibly related issues:

@core23
Copy link
Member

core23 commented Feb 13, 2016

Ping @webdevilopers

@webdevilopers
Copy link
Contributor Author

I'm no longer maintaining the project with the example. Will close issue for now but keep an eye on it. I have a similar project coming up.

@webdevilopers
Copy link
Contributor Author

I may be an old issue but I stumbled upon something similar again.
I think this issue is created due to the fact that the internal admin class method "isGranted" only checks sonata roles and NOT the inherited roles from the hierarchy.

As mentioned here by @MaxDamage:

Generic, non-entity roles, like ROLE_USER, ROLE_ADMIN, ROLE_SUPER_ADMIN, ROLE_{CUSTOM_STRING} should be checked using the default Symfony security context.
Entity actions roles like ROLE_SONATA_USER_ADMIN_USER_LIST, ROLE_SONATA_USER_ADMIN_USER_VIEW, ROLE_{CUSTOM_SONATA_ADMIN_SERVICE_NAME}_{ACTION} can be checked using the Sonata Admin helper or the Symfony security context.

@kunicmarko20
Copy link
Contributor

We should add support for both Sonata and Symfony roles.

public function isGranted(AdminInterface $admin, $attributes, $object = null)
{
if (!is_array($attributes)) {
$attributes = [$attributes];
}
foreach ($attributes as $pos => $attribute) {
$attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
}
$allRole = sprintf($this->getBaseRole($admin), 'ALL');
try {
return $this->authorizationChecker->isGranted($this->superAdminRoles)
|| $this->authorizationChecker->isGranted($attributes, $object)
|| $this->authorizationChecker->isGranted([$allRole], $object);
} catch (AuthenticationCredentialsNotFoundException $e) {
return false;
}
}

Foreach adds soanta perfix for roles, so maybe we can check before it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants