Reputation: 3783
I'm trying to use the access_denied_url
parameter in security.yml
The problem is that... it does nothing. When I access to /mon-equipement
as anonymous, it keeps redirecting me to /login
Here is my security.yml
file :
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
always_use_default_target_path: false
default_target_path: /mon-equipement
target_path_parameter: _target_path
use_referer: false
logout: true
anonymous: true
access_denied_url: /
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/mon-equipement, role: ROLE_USER }
- { path: ^/admin/, role: ROLE_ADMIN }
I'm using FOSUserBundle with Symfony2.3.16
Upvotes: 1
Views: 3742
Reputation: 1948
First you have to know that access_denied_url is only redirecting non anonymous users. For instance it will redirecte a user with ROLE_MEMBER if it tries to acces a page whose path is only for ROLE_ADMIN.
Here the solution:
You have to create a service (my_entry_point) that will be triggered at the entry-point listener (see security.yml below) and which will redirect the user to the page you want (target_page_for_redirection)
# app/config/security.yml
security:
firewalls:
main:
entry_point: my_entry_point # listener triggered if no token is set while an authentification is needed (access_control)
pattern: ^/
.
#src/Acme/UserBundle/Ressources/Config/services.yml
service
my_entry_point:
class: Acme\UserBundle\Redirection\EntryPointRedirection
arguments: [@router] #needed for URL redirection
.
<?php
namespace Acme\UserBundle\Redirection;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse;
class EntryPointRedirection implements AuthenticationEntryPointInterface
{
protected $router;
public function __construct($router)
{
$this->router = $router;
}
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse($this->router->generate('target_page_for_redirection'));
}
}
Upvotes: 4
Reputation: 4491
I think your access_controll
section should looks like this:
access_control:
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
# ... here the other routes
Your problem: It redirects you on the /
page, but you have no access to this page. Therefore it redirects you on the login page.
UPDATE:
You can also define access_denied_url
for all firewalls:
# app/config/security.yml
security:
access_denied_url: /
Upvotes: 4