简体   繁体   中英

Sonata admin: user_block is not displayed

I'm using SonataAdmin and I can't see the user block displayed in the template (on the top right) although I have replaced it with my custom template as mentioned in the configuration here https://sonata-project.org/bundles/admin/3-x/doc/reference/templates.html
I'm using SonataUserBundle and this is my configuration:

sonata_admin:
    templates:
        user_block: ApplicationSonataUserBundle:Default:user_block.html.twig

and these are the versions I'm using:

"require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^5.0.0",
        "sonata-project/admin-bundle": "^3.43",
        "sonata-project/doctrine-orm-admin-bundle": "^3.7",
        "sonata-project/user-bundle": "4.0.0",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.6.4",
        "symfony/symfony": "3.4.*",
        "twig/twig": "^1.0||^2.0"
    },

I get no error however the user icon on the top right is not displayed !
What am I missing here ?

ok so I found that sonata is testing if the user has role_admin to display the user_block as mentioned here https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Resources/views/standard_layout.html.twig#L194
look for this condition:

{% if app.user and is_granted(sonata_admin.adminPool.getOption('role_admin')) %}

and since all users has the role ROLE_USER I just need to give this information to sonata, in the config.yml file, so it will display the user_block to all connected users, like so:

sonata_admin:
    security:
        role_admin: ROLE_USER
sonata_admin:
    security:
        role_admin: foobar_token


class UserBlockVoter extends Voter
{
    protected function supports($attribute, $subject)
    {
        return 'foobar_token' == $attribute;
    }

    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        return true;
    }
}

if changing roles is not the case - create a voter for custom-named "role_admin" attribute, since decision of showing/hiding is based on security checker.

furthermore, you could check "User" instance inside voter with more complex rules than just "ROLE_ADMIN"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM