setting a default locale - locale

I'm trying to implement the locale in the routes.
I can get it to work like host/en/page & host/fr/page with this snippet:
#routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
prefix: /{_locale}
requirements:
_locale: nl|en
But my goal is to be able to use host/page where the locale would not be in the url, but is set as a default locale.
How do I do this? I can't find it anywhere in the documentation.
Edit, trying out the JMSI18nRoutingBundle
additions:
# config.yml
jms_i18n_routing:
default_locale: nl
locales: [nl, en]
strategy: prefix_except_default
.
# security.yml > firewalls section
form_login:
login_path: _login
check_path: _login
csrf_token_generator: security.csrf.token_manager
logout:
path: _logout
target: /
.
# routing.yml
_logout:
path: /logout
options: { i18n: false }
_login:
path: /login
options: { i18n: false }
Routes are prefixed as they should, but I'm unable to use my login and logout routes.

Use https://github.com/schmittjoh/JMSI18nRoutingBundle bundle.
In app/config.yml setup the bundle for default locale.
jms_i18n_routing:
default_locale: nl
locales: [nl, en]
strategy: prefix_except_default

Related

How can you exit a switch_user by Symfony 3.4?

I have a project with the possability to switch from user. The impersonator works, but I can't leave the impersonator. So I stay login with the user where I to switch.
For example: User A is login and switch to user B. When They click on /?_switch_user=_exit, then you expect that user is go back to user A. But the user stay by user B.
What can be the problem?
This information have I included in the security.yml:
firewalls:
main:
pattern: ^/
two_factor:
auth_form_path: 2fa_login
check_path: 2fa_login_check
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
check_path: fos_user_security_check
default_target_path: homepage
always_use_default_target_path: false #true
#csrf_token_generator: security.csrf.token_manager
logout:
path: fos_user_security_logout
target: /
anonymous: true
logout_on_user_change: false
switch_user: { role: ROLE_ADMIN }
Update: The user A is a ROLE_ADMIN and B is ROLE_CONSUMER
Routing:
home_redirect:
path: /{_locale}
defaults: { _controller: AppBundle:Default:index, _locale: 'nl' }
requirements:
_locale: nl|en|fr
app:
resource: '#AppBundle/Controller/'
prefix: /{_locale}
defaults: {_locale: 'nl'}
type: annotation
requirements:
_locale: nl|en|fr
Update 2:
The error what is comming by redirect to exit:
error of wrong user
Thanks for the help!
Since you're linking to root using /?_switch_user=_exit, ensure that your firewall definition covers that path:
main:
pattern: ^/

How do security settings in config.yml and security.yml relate?

I am trying to set up user and security management in a first test application of mine and I have come to be a bit lost as to what does what.
My setup thus far: Symfony 2.5, SonataUserBundle (and with it FOSUserBundle)
In my app/config/config.yml, I have the following settings that I make out to be relevant in terms of managing site security (most taken over from the setup instructions of the various bundles I included):
imports:
- { resource: security.yml }
[...]
fos_user:
firewall_name: main
[...]
security:
# FOSUserBundle config
# cf. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-4-configure-your-applications-securityyml
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
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
# end of FOSUserBundle config
access_control:
# URL of FOSUserBundle which need to be available to anonymous users
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Admin login page needs to be access without credential
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# Secured part of the site
# This config requires being logged for the whole site and having the admin role for the admin part.
# Change these rules to adapt them to your needs
- { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
- { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
My app/config/security.yml looks as follows:
security:
# added with Sonata User Bundle
encoders:
FOS\UserBundle\Model\UserInterface: sha512
# end
providers:
in_memory:
memory: ~
# added with Sonata User Bundle
fos_userbundle:
id: fos_user.user_manager
# end
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
# added with Sonata User Bundle
# -> custom firewall for the admin area of the URL
admin:
pattern: /admin(.*)
context: user
form_login:
provider: fos_userbundle
login_path: /admin/login
use_forward: false
check_path: /admin/login_check
failure_path: null
logout:
path: /admin/logout
anonymous: true
# -> end custom configuration
# default login area for standard users
# This firewall is used to handle the public login area
# This part is handled by the FOS User Bundle
main:
pattern: /(.*)
context: user
form_login:
provider: fos_userbundle
login_path: /login
use_forward: false
check_path: /login_check
failure_path: null
logout: true
anonymous: true
# end
default:
anonymous: ~
# Sonata
acl:
connection: default
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
SONATA:
- ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are using acl then this line must be commented
Here are my questions:
Precedence of configurations
Based on my understanding of the "patterns" of Symfony thus far, anything in security.yml is loaded first and would thus take precedence over any new definitions for identical parameters further down in my config.yml. Is that correct?
Duplicate definitions
It seems to me that the following are defined twice, once in security.yml, once in config.yml:
The provider for FOSUserBundle (different values, fos_user.user_manager and fos_user.user_provider.username)
The encoder for FOS\UserBundle\Model\UserInterface
The pattern for the main firewall (^/vs. .*)
Are these indeed defining the same? Is it safe to assume that in all these cases, only those settings defined in security.yml apply?
Best practices
How should security-related definitions generally be divided between security.yml and config.yml (and other potential locations)?
As Cerad mentionned in a comment, you have the same section security: in both files.
Look at the start of the app/config/config.yml file:
imports:
- { resource: security.yml }
This means that the security.yml file will be imported when the config.yml file will be parsed by Symfony2. So you can keep only the security: section in the app/config/security.yml file in order to define the security configuration.
This is the default configuration, see these files in the official GitHub repository:
app/config/config.yml (no security: section)
app/config/security.yml (contains a security: section)

Security in Symfony2

I've created a Bundle named "User". In the general routing I added /user to every UserBundle url. So, for example, when I define the route /list in the UserBundle the real path is http://myapp.loc/user/list.
I'm trying to force user to login to use this website, an I'm doing this:
security:
firewalls:
login_firewall:
pattern: ^/user/login
anonymous: ~
secured_area:
pattern: ^/
form_login:
login_path: user_login
check_path: user_login_check
default_target_path: /
logout:
path: /user_logout
target: user_login
remember_me:
key: atipics-soft2012
lifetime: 3600
access_control:
- { path: ^/, roles: ROLE_USER }
providers:
users:
entity: { class: MyApp\UserBundle\Entity\User, property:email }
encoders:
MyApp\UserBundle\Entity\User: { algorithm: sha512, iterations: 10 }
I'm getting an error like this:
InvalidConfigurationException: Invalid configuration for path
"security.firewalls.secured_area": The check_path "user_login_check"
for login method "form_login" is not matched by the firewall pattern
"^/".
What could be the problem?
Of couse I've added this route in the routes file.
I don't know if it's a bug or was intented this way, but check_path doesn't accept route names. You have to set it to a path.

Symfony-2 > login & logout routes with placeholders

I am using Symfony-2 to implement my application.
I need my login and logout routes to have some placeholders, but I don't manage to define it well in routing.yml and security.yml files.
I want to have something like:
www.mysite.com/{client_slug}/panel
and under it other secured pages:
www.mysite.com/{client_slug}/panel/.*
When someone tries to navigate to one of these pages, he/she should be redirected to:
www.mysite.com/{client_slug}/login
and after clicking logout, user should be redirected to something like:
www.mysite.com/{client_slug}/goodbye
I tried several things (http://forum.symfony-project.org/viewtopic.php?f=23&t=37809) but at the moment the only thing I achieved was that in my login url the text {client_slug} appears:
www.mysite.com/my-cliend-slug/panel
redirects to
www.mysite.com/{client_slug}/login
security.yml
firewalls:
main:
pattern: /.*
form_login:
check_path: /login_check
login_path: _security_login
logout:
path: /logout
target: /goodbye
security: true
anonymous: true
routing.yml
_security_login:
pattern: /{_client_slug}/login
defaults: { _controller: MyAppBackendBundle:Security:login }
_security_check:
pattern: /login_check
_security_logout:
pattern: /logout
_admin_panel:
pattern: /{_client_slug}/panel
defaults: { _controller: MyAppBackendBundle:AdminPanel:index }
Any ideas?
I had the exact same problem. I've read everything from here (http://forum.symfony-project.org/viewtopic.php?f=23&t=37809) and for me it works with a few extra lines.
Here is my security.yml
login_area:
pattern: ^/[A-Za-z0-9\-\_]+/login$
anonymous: ~
secured_area:
pattern: ^/[A-Za-z0-9\-\_]+/.*
form_login:
login_path: login
check_path: login_check
logout:
path: logout
target: /
remember_me:
key: "%secret%"
lifetime: 31536000
path: /
domain: ~
The login and login_path definitions:
login:
pattern: /{_client}/login
defaults: { _controller: GNCApplicationBaseBundle:Security:login }
login_check:
pattern: /{_client}/login_check
And I made an EventListener, which will be called at the kernel.request event:
acme.system.client.listener:
class: Acme\System\ClientBundle\EventListener\ClientListener
arguments: [#router, #doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 10 }
The important attribute is the priority. In the Symfony Documentation (http://symfony.com/doc/current/reference/dic_tags.html#kernel-event-listener) it shows that the RouterListener starts with a priority 32 and the Firewall at 8. So I choose to call my custom EventListener right before the Firewall and set the _client-attribute manually in the router-context:
public function onKernelRequest(GetResponseEvent $event) {
$clientIdentifier = $event->getRequest()->attributes->get('_client');
$this->router->getContext()->setParameter('_client', $clientIdentifier);
}
And it works well for me.
I'm currently using Symfony 2.2.0-RC3.

Check_path isn't behind symfony's firewall , how to correct this?

I am trying to authenticate against symfony2 firewall , here is my security config
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
user: { password: user, roles: [ 'ROLE_USER' ] }
admin: { password: admin, roles: [ 'ROLE_ADMIN' ]}
#main:
#entity: { class: Surgeworks\CoreBundle\Entity\User, property: username}
firewalls:
public:
pattern: .*
security: false
anonymous: true
form_login:
check_path: /{_locale}/admin/logincheck
login:
pattern: ^/{_locale}/admin/login$
security: false
anonymous: ~
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
anonymous: ~
secured_area :
provider: in_memory
pattern: ^/{_locale}/admin/.*
form_login:
check_path: /{_locale}/admin/logincheck
login_path: /{_locale}/admin/login
logout:
path : /{_locale}/admin/logout
target : /{_locale}/admin/
remember_me:
key: aSecretKey
lifetime: 3600
path: /admin/
domain: ~ # Defaults to the current domain from $_SERVER
access_control:
- { path: ^/{_locale}/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/{_locale}/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/ar/admin/logincheck, roles: ROLE_ADMIN }
- { path: ^/(ar|en|fr)/admin/, roles: ROLE_ADMIN }
and here is my routing in DaghoSiteBundle/Resources/config/routing.yml:
_admin:
pattern: /admin/
defaults: { _controller: DaghoSiteBundle:Login:login , _locale : ar }
requirements:
_locale: (ar|en|fr)
login:
pattern: /admin/login
defaults : { _controller: DaghoSiteBundle:Login:login , _locale : ar }
requirements:
_locale: (ar|en|fr)
logincheck:
pattern: /admin/logincheck
#defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
#requirements:
#_locale: (ar|en|fr)
logout:
pattern: /admin/logout
I can't login through these setting ,
it always throw an exception
Unable to find the controller for path "/ar/admin/logincheck". Maybe
you forgot to add the matching route in your routing configuration
and even if had setup the route check_path page >> i would be able to view the check_path without being redirected to login page ..
/en/admin >> login page
/en/logincheck >> display the logincheck template (i.e /en/logincheck isn't behind firewall )
how to fix , or debug this issue , please advise
UPDATE:
sorry I might forget to write that i had prefixed my bundle with the {_locale} like this
in my routing.yml
DaghoSiteBundle:
resource: "#DaghoSiteBundle/Resources/config/routing.yml"
prefix: /{_locale}
requirements:
_locale: ar|en|fr
defaults: { _locale: ar }
I think you must change your route patterns to include your _locale parameter. Instead of, for example, pattern: /admin/logincheck you should use pattern: {_locale}/admin/logincheck
You can also debug your routes by using a console command app/console router:debug.
I am not 100% sure this fixes your problem, but I hope it helps you gather more info about your issue.
Why dont you try
logincheck:
pattern: /admin/login_check
#defaults: { _controller: DaghoSiteBundle:Login:logincheck , _locale: ar }
#requirements:
#_locale: (ar|en|fr)
This solved my problem, but my situation is simple and doesn't have the {_locale} functionality, but maybe it will help you anyway.
From the Security chapter of the Symf2 Book:
Common Pitfalls #3: Be sure /login_check is behind a firewall.
In your security.yml file it looks like your check_path route is /{_locale}/admin/logincheck, and your secured path is anything that's behind ^/{_locale}/admin/.*
so that seems to be good.
You might want to try removing the .* (do you really need it?)
and further down the file in the access control section you provide a specific entry to make sure the check_path requires authentication:
- { path: ^/ar/admin/logincheck, roles: ROLE_ADMIN }
Maybe try specifying this instead:
- { path: ^/{_locale}/admin/logincheck, roles: ROLE_ADMIN }
But actually, I was wondering, can you even use {placeholders} in the security.yml pattern values? I know you can in the routing file, but I'm not sure if the security yml works the same way? I don't see it being used in any examples in the main symf2 book in the security or routing chapters?

Resources