security - Symfony2 - Can I give an anonymous user a 'ROLE_SOMETHING' -
i'm using symfony framework fos user bundle. i'm using security context determine menu items , other items display.
$securitycontext = $this->get('security.context'); if ($securitycontext->isgranted($report['permission'])){ //add menu item... }
is there way give anonymous user security context of 'role_user'? i've got logged in users working properly.
i tried adding line:
role_hierarchy: is_authenticated_anonymously: role_user
to security.yml hoping it, apparently not. i've googled around little bit , read documentation.
i imagine that:
if ($securitycontext->isgranted($report['permission']) || ($report['permission'] === 'role_user' && $securitycontext->is_anonymous()))
would work, feels kind of hack (and not dry)
edit: intranet site. i've got table contains names of reports. reports should able seen everyone, regardless of if logged in or not. reports require permissions view. don't want create several hundred users when handful need access.
if trying give access people given url why not authorize way ?
you have 2 method achieve this: create firewall authorization or role defined url
1) firewall autorization
firewalls: test: pattern: ^/ws // url or schema url regex anonymous: true
2) url role defined access
access_control: - { path: ^/given-url, roles: is_authenticated_anonymously } // in app/config/security.yml
in both case, non authenticated user , authenticated user have access url
by way , if want test ( in order display user variables ) if user authenticated or not , make test in twig
{% if app.user defined , app.user not null %} user {{ app.user.username }} connected. {% else %} no user connected {% end %}
edit : content based view : juste create route action not match firewall rules
Comments
Post a Comment