I recently did a vendors/install and now my sonata admin is broken with the following error:
Item "roles" for "" does not exist in SonataAdminBundle::standard_layout.html.twig at line 92
Here is the twig code:
{% if app.security %}
{% for role in app.security.token.roles %}
{% if not allowed %}
{% set allowed = role.role == 'ROLE_SONATA_ADMIN' %}
{% endif %}
{% endfor %}
{% endif %}
Anyone have any thoughts?
Thanks!
I searched through admin bundle with blame option and it comes from a commit 7 days ago
So what you have to do is go to deps.lock
Find the adminBundle commit hash(In my case it started with 4a....)
replace the hash with 25b401e6271ee0fd896d700d0328b06994e4e138 which is commit before the commit that cause problem
I tried it and dashboard works correctly
Good luck
EDIT
Be careful not to use php bin/vendor update since it will update the commit hash
Use php bin/vendor install
Here is the fix : https://github.com/Wiakowe/SonataAdminBundle/commit/e139527c05b22176ee3efab987ebe752668d4489
Remove lines 90-99 and replace with the following:
{% if app.security and is_granted('ROLE_SONATA_ADMIN') %}
I had the same problem with the last version of SonataAdminBundle. This problem appears if you allow for everybody opening your admin panel. This is not usuall solution at least for the working site.
To grant access to your admin panel for everybody do the following. Place the path /admin under firewall, add into the file security.yml in the section security.firewals the rows
admin_area:
pattern: ^/admin
anonymous: ~
Thus each client will be authorized as anonymous user with existing token and the errors will not appear.
P.S. Sorry for grammar mistakes, but I think everybody will understand me :)
Related
According to symfony docs, the variable app is available "everywhere" for twig templates. I want to use app.user in a custom error message, but this variable does not actually seem to be available within these templates while in prod mode; it IS available in dev mode.
Example code:
// project/templates/bundles/TwigBundle/Exception/error404.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{{ parent() }}
<div>
<h1>Page not found</h1>
<p>
Hello {{ app.user.name }}, {% <---- DOES NOT WORK %}
The requested page couldn't be located. Checkout for any URL
misspelling.
</p>
</div>
{% endblock %}
This is just a simple example, but illustrates the point. I've also tried to use
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
// do something fancy
{% endif %}
and this also does not work. It also does not matter whether I extend base.html.twig or pull in the parent(), in all cases it does not work. I get no error messages at all. I have cleared by cache.
Any ideas about how to get this information to my template in prod mode?
I found in the documentation that the problem I describe is by design. From the docs:
Security & 404 PagesĀ¶
Due to the order of how routing and security are
loaded, security information will not be available on your 404 pages.
This means that it will appear as if your user is logged out on the
404 page (it will work while testing, but not on production).
The answer is to create my own exception controller (or extend the default ExceptionController).
Is there a way to check if a session exists in the twig file? I want to switch between register or logout in my nav bar based on the session of the user.
I've searched a bit around and I could only found solution that do it in the controller.
Instead of directly checking session variables, I think you want to use what Symfony already has built-in to handle this. Please see this documentation:
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
// show logout link
{% else %}
// show register link
{% endif %}
If you are using a Symfony version earlier than 2.8 you need to check for the existence of app.user first:
{% if app.user and is_granted('IS_AUTHENTICATED_FULLY') %}
I'm looking for the proper syntax with a Drupal 8 conditional display of a link. I've been reading the twig documentation, and it's a significant jump from the PHP templating days so it's slow going. I need to conditionally display a link within the footer of a view.
In the past, this would be straightforward, i.e:
<?php global $user;?>
<?php if (user_access('administer nodes')):?>
<div class="foo">Shorthand link for admins</div>
<?php endif;?>
Getting this to work in Twig has been difficult as I'm unfamiliar with the syntax. Do I need to declare global user in some capacity? From this link here, Symfony 2: How do I check if a user is not logged in inside a template?, it seems that all I'd need to do is:
{% if is_granted('ROLE_ADMIN') -%}
<div class="foo">Shorthand link for admins</div>
{% endif %}
But I get an error when trying to submit that. Is ROLE_ADMIN not defined? How do I get the Symphony? (correct?) roles as defined within the D8 installation? I'm not sure what I'm doing wrong. Any assistance is appreciated.
If you need to check a specific permission try
{% if user.hasPermission('administer nodes') %}
<div class="foo">Shorthand link for admins</div>
{% endif %}
If you try to check the current user is administer in TWIG file , you can use
{% if is_admin %}
<div class="foo">Shorthand link for admins</div>
{% endif %}
I'm building a webiste, where there will be lots of "widgets" displayed on the front page. One of them is the calendar.
I wanted to make a new controller for each this "widget", and then render them all (like the example below), in the Homepage view.
Hovewer - the translations stop working after that. If I visit mywebpage/calendar, they work, but not when I got to mywebpage/home.
This is the code I currently have for my Homepage view.
{% block Calendar %}
{{ render(controller('MyWebsite:Calendar:index')) }}
{% endblock %}
Am I approaching this correctly or not? And why do the translations stop working?
Also - I hope you will understand what the issue is :)
Thanks!
I am surprised that doesn't work, do you have anything else in your app interfering with the Request object?
Maybe passing the locale from the request as an argument to the controller may work (although a bit of a hack)?
{% block Calendar %}
{{ render(controller('MyWebsite:Calendar:index', { _locale: app.request.locale })) }}
{% endblock %}
I new to Symfony.
I want a piece of code only to show on pages that are publicly available. Is it possible for Twig to detect if the page is public?
The page is under IS_AUTHENTICATED_ANONYMOUSLY, but I still want the piece of code to show even if the user is already logged-in.
For Example:
www.somesite.com (shows the code, publicly available)
www.somesite.com/login (user login, publicly available, shows code)
//user is not logged in
www.somesite.com/dosomething (only available to logged-in users, code is hidden)
//user goes to the home page, still logged-in
www.somesite.com (must show the code, publicly available)
I know I could manually include the code for my public pages, but is there a way to automatically detect if the page is outside the firewall?
Please Help. =)
You can hide parts of twig template with a check like so:
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
{# stuff only logged in users can see #}
{% endif %}
Or check for role IS_AUTHENTICATED_FULLY
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{# stuff only logged in users can see #}
{% endif %}
Check the doc here: http://symfony.com/doc/current/book/security.html#retrieving-the-user-object
i guess there is no way to do this. =(