Is it possible to perform an automatic redirect to the some route (i.e. /) for the specific route /login
only for users that are AUTHENTICATED
? and How?
I'm using FOSUserBundle.
This is my security configuration:
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
login_path: /accedi
check_path: /login_check
default_target_path: /
oauth:
resource_owners:
facebook: "/login/check-facebook"
google: "/login/check-google"
login_path: /accedi
failure_path: /accedi
default_target_path: /
oauth_user_provider:
service: my_user_provider
logout:
path: /logout
target: /
invalidate_session: false
anonymous: ~
login:
pattern: ^/login$
security: false
remember_me:
key: "%secret%"
lifetime: 31536000 # 365 days in seconds
path: /
domain: ~
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: fos_userbundle
check_path: _security_check
login_path: _demo_login
anonymous: true
oauth_token:
pattern: ^/oauth/v2/token
security: false
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/accedi$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/registrati, role: IS_AUTHENTICATED_ANONYMOUSLY }
As you are using FOSUserBundle the rendering of the login form takes place in
SecurityController::renderLogin()
.The solution is bascially:
IS_AUTHENTICATD_ANONYMOUSLY
I assume you have already created a bundle extending FOSUserBundle which holds your
User
Entity.I assume this bundle is called
YourUserBundle
and is located atsrc/Your/Bundle/UserBundle
.Now copy (not cut) the SecurityController
to (in order to override the one provided by FOSUserBundle)
add the use-statement for
RedirectResponse
and edit therenderLogin()
method like this:Update
Now instead of
security.context
usesecurity.authorization_checker
.NB : I'm using Symfony 3.0.4@dev
This answer is based and the one that @nifr @mirk have provided and the comment of @Ronan.
To prevent the user access to the login page I override the SecurityController like this :
I've also added it to the RegistrationController to do the exact same thing.
Hope it will help some of you.
Another solution based on the @nifr answer. Overwriting only the
renderLogin
function in your bundle controller. See also. How to use Bundle Inheritance to Override parts of a BundleIt seems to me overriding the rendering of the login form is providing an answer in the wrong place. The rendering of the login form is not the one responsible for login. It's a result of the login request. It could have other usages elsewhere in the future and you'd be breaking functionality for those situations.
Overriding the login action seems better to me. That's the actual component responsible for handling the login request.
To do that, override the login action in the Security Controller. Say you have a MyUserBundle in your MyProject project which extends the FOSUserBundle.
I'm using the routing and security to enable this.
If a user is logged in, he get redirected to the dashboard. If not, he will see the login route.
Hope this helps you. :)