Override FOSUserBundle routes Symfony2 - symfony

I would like to override some routes from FOSUserBundle
MyBundle/Resources/config/routing/security.yml
fos_user_security_login:
path: /{_locale}/login
defaults: { _controller: FOSUserBundle:Security:login }
requirements:
_locale: %locales%
fos_user_security_check:
path: /login_check
defaults: { _controller: FOSUserBundle:Security:check }
requirements:
_locale: %locales%
fos_user_security_logout:
path: /{_locale}/logout
defaults: { _controller: FOSUserBundle:Security:logout }
requirements:
_locale: %locales%
But it does not works, route are not found
MyBundle/Resources/config/routing/security.xml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="fos_user_security_login" pattern="/{_locale}/login">
<default key="_controller">FOSUserBundle:Security:login</default>
</route>
<route id="fos_user_security_check" pattern="/login_check">
<default key="_controller">FOSUserBundle:Security:check</default>
<requirement key="_method">POST</requirement>
</route>
<route id="fos_user_security_logout" pattern="/{_locale}/logout">
<default key="_controller">FOSUserBundle:Security:logout</default>
</route>
</routes>
This works but I don't know how to pass my locales parameter from parameter.yml

First of all the the yaml routes are not working because the FOSUserBundle Routes are defined in xml.
So your yaml routes won't imported.
here the FOSUserBundle Routes:
https://github.com/FriendsOfSymfony/FOSUserBundle/tree/master/Resources/config/routing
If the FOSUserBundle is the parent bundle of your userbundle you are able to rewrite the FOSUserBundle routing resources.
How to do this is explained here:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-etc
Further more to answer to the last point how to pass the locale into the route is described here:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-etc
<route id="contact" path="/{_locale}/contact">
<default key="_controller">AcmeDemoBundle:Contact:index</default>
<requirement key="_locale">%locales%</requirement>
</route>

Related

SonataAdminBundle redirect / (homepage) to /admin/dashboard

I'm using SonataAdminBundle and I want my secured area to be under /admin, and I want to have a homepage on / that is a list of objects.
But when I go on / I'm automatically redirected to /admin/dashboard.
This works fine if my "homepage" is under another url (e.g : /homepage)
Here is my config.yml:
sonata_admin:
title: 'Versus'
title_logo: 'logo_title.png'
templates:
# other configuration options
search: '#SonataAdmin/Core/search.html.twig'
search_result_block: 'template/Admin/Search/block_search_result.html.twig'
edit: 'template/Admin/CRUD/edit.html.twig'
global_search:
empty_boxes: fade
sonata_block:
blocks:
# enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
sonata.admin.block.search_result:
contexts: [admin]
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: admin
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
Here is my security.yml :
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout: true
anonymous: true
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
And here is my routing.yml :
admin_area:
resource: '#SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
front:
resource: "#AppBundle/Controller/*"
type: annotation
prefix: /
I don't know what is causing the redirection, and how I can prevent it.

Symfony 4 translation are not working

the problem is not symfony, is me lol, I need help cause is not working fine, when i change my locale the text is still in english, there is my code
translation.yaml
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks:
- '%locale%'
framework.yaml
framework:
secret: '%env(APP_SECRET)%'
default_locale: en
csrf_protection: true
On my FrontController
`/**
* #Route("/{_locale}/",
* requirements={"_locale"="en|es"},
* name="index")
*/
public function index()
{
if($this->getUser()){
return $this->redirectToRoute('index');
}else{
$register= new Register();
$form = $this->createForm(RegisterType::class, $register,array(
'empty_data'=>'user_register',
'validation_groups' => array('default', 'empty_data'),
));
}`
my messages.es.xlf
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="sign_in">
<source>Sign in</source>
<target>Entrar</target>
</trans-unit>
</body>
</file>
on my template
{{ 'Sign in'|trans }}
well, the problem, is i change my url http://...../en/ to http://..../es/ is not working, but if i change my fallback manuale to es on translation.yaml is working fine, but then dons't work on /en/ any idea??

Symfony is caching user information in my navbar, so it is sharred between users

I'm using the native cache system on my project.
I have the following code in my navbar :
<ul class="nav navbar-nav">
<li>
Recherche
</li>
{% if is_granted("ROLE_USER") %}
<li>
Ajouter
</li>
{% endif %}
</ul>
But if I open my site in 2 différents browsers and I loggeg in on one browser, I can see these links on the other browser as well.
It seems to be due to the cache bcause I have to files in my PHP sessions folder (one for each browser).
Any idea ?
Thanks for your help
Actually, I'm not sur it's a caching issue.
If I open my dev app in two browsers, in the debug bar, I can see that my session is shared...
I'm using the FOSuserBundle to manage users and authentication.
Here is my config.yml :
session:
# handler_id set to null will use default session handler from php.ini
handler_id: session.handler.native_file
save_path: '%kernel.root_dir%/sessions'
And here my security.yml :
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
in_memory:
memory: ~
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
logout:
target: /
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/add, role: ROLE_USER }
- { path: ^/vote, role: ROLE_USER }
I really don't understand what's going on...
Using edge side includes (ESI) you can specify different caching requirements for specific elements of a page, rather than the page as a whole. See: http://symfony.com/doc/current/book/http_cache.html#using-edge-side-includes
My config.yml:
session:
lifetime: 3600
save_path: %kernel.root_dir%/var/sessions
esi: { enabled: true }
My twig file:
<div class="header-left">
{% render 'MyUserBundle:Account:menu' with {}, {'standalone': true} %}
</div>
This works in Symfony 2.1, but there will probably be subtle changes in later versions.
I have finally found the solution.
It was something due to my app.php and app_dev.php files.
I tried some config 1 day, and I had forgotten......
Here is what I had in my code :
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/AppCache.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
AppCache was the problem.
Thanks for your help guys !

hwioauthbundle configure for google

i try configure this bundle for authication with google api. I see documentation and search in google, but still can't configure.
Now i have this error :
The service "hwi_oauth.security.oauth_utils" has a dependency on a non-existent service "hwi_oauth.resource_ownermap.main".
Now i only want show in my default page link for google and when i click for it, i want get response from google and show it in var dump..
routing.yml:
hwi_oauth_redirect:
resource: "#HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix: /login
hwi_oauth_login:
resource: "#HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login
google_login:
path: /login/check-google
config.yml
hwi_oauth:
connect:
account_connector: defaultbundle.oauth_provider
firewall_names: [secured_area]
resource_owners:
google:
type: google
client_id: ****
client_secret: ****
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
security.yml
secured_area:
pattern: ^/
anonymous: ~
oauth:
resource_owners:
google: "/login/check-google"
login_path: /login
use_forward: false
failure_path: /login
oauth_provider:
service: defaultbundle.oauth_provider
logout: true
anonymous: true
# access_control:
# - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
services.yml
defaultbundle.oauth_provider:
class: Public\defaultBundle\Auth\OAuthProvider
arguments: [#doctrine]
base.html
<a href="{{ path('hwi_oauth_service_redirect', {'service': 'google' }) }}">
<span>Google</span>
</a>
OAuthProvider.php
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
class OAuthProvider extends OAuthUserProvider
{
}
The firewall name in hwi_oauth configuration in config.yml (currently: main) is wrong.
It must match the firewall name(s) from the security.yml (currently: secured_area) that uses ouath.
hwi_ouath:
connect:
account_connector: defaultbundle.oauth_provider
firewall_name: secured_area # <---- here is the change (if using 0.3)
firewall_names: [secured_area] # <--- if using 0.4
...
Documentation:
Version 0.3
Version 0.4

Symfony2 login form redirects to home page instead of redirecting to referer URL

I got a Symfony2 project set up with a form-based login.
This is what my security.yml configuration looks like:
firewalls:
login:
pattern: ^/login$
security: false
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
default_target_path: /home
logout:
path: /logout
target: /
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/login, access: "isAnonymous()" }
- { path: ^/login_check, access: "isAnonymous()" }
- { path: ^/register, access: "isAnonymous()" }
Generally, logging in and out works just fine, but when logging in, I'm getting redirected to / instead of the requested URL (referer). This happens regardless of whether the use_referer is set to true or false.
When setting always_use_default_target_path to true, I get redirected to the default path (/home) instead of /, which does make sense.
Can anybody give me a hint on why I'm being redirected to / instead of the referer URL?
Thanks!
Ok, it was my bad... I somehow forgot that some time ago I added the following line to my login action's form template (login.html.twig):
<input type="hidden" name="_target_path" value="/" />
After commenting it, suddenly everything is working again... ;-)

Resources