I have the following ACL settings in my secirity.yml file
fos_user_resetting:
path: /backend/request
roles: [IS_AUTHENTICATED_ANONYMOUSLY]
super_admin_pages:
path: /backend/.*
roles: [ROLE_SUPER,ROLE_USERS]
Also i found setting access permission using #secirity tag in the routing annotation.
My question is, is it possible to add security (access permission according to roles like in secirity.yml) for each path in routing.yml file
Nope, there is no such argument for securing routes directly in your routing file. The common part between ACL and routing are paths.
See all the details in the Security chapter of Symfony doc.
Related
I have a controller, where I use the #IsGranted(IS_AUTHENTICATED_ANONYMOUSLY) annotation to allow all users to access, and I also have a security.yaml.
But I the annotation does not seem to work.
Controller
/**
* #Route("/example",name="app_example")
* #IsGranted("IS_AUTHENTICATED_ANONYMOUSLY")
*/
public function example(): RedirectResponse
{
/// omit
}
security.yaml
access_control:
- { path: ^/, roles: ROLE_ADMIN }
When I access /example, I'm requested to login.
I know I can manage by moving IS_AUTHENTICATED_ANONYMOUSLY to security.yaml but I want to know the way to use annotation.
The #IsGranted() (from SensioFrameworkExtraBundle) is checked on an event that comes after Symfony Security access control.
Since you have contradictory configurations (your main security configuration demands authentication on all routes, and your the annotation on your controller simply says "no authentication on this route"), the main security configuration "wins".
If you want to have security configuration both in the configuration file and as annotations, the configuration shouldn't overlap and contradict each other.
If they conflict, for the #IsGranted() annotations to work they can be more restrictive than the main security configuration, but not more open.
What does the type: configuration in a Symfony routing file control? What are its valid values?
I can't find this configuration field explicitly documented anywhere. It's referenced indirectly in Symfony's routing documentation.
app_directory:
resource: '../legacy/routing/'
type: directory
and seems related to loading in additional routes. However, its behavior (or all its allowed values) doesn't seem to be explicatly defined anywhere. I can make a guess that it somehow tells Symfony how to load the external routes, but I'd love to know
Is my guess correct?
Are there valid values other than directory or annotation?
Is this formally documented anywhere?
Is there a spot in the Symfony internals that would be a good place to start finding these answers for myself?
You can find how the type works in the Symfony documentation, see code below. It controls if the routes should be loaded from the PHP annotations or the YAML or XML files found in that (bundle) directory.
app_file:
# loads routes from the given routing file stored in some bundle
resource: '#AcmeOtherBundle/Resources/config/routing.yaml'
app_annotations:
# loads routes from the PHP annotations of the controllers found in that directory
resource: '../src/Controller/'
type: annotation
app_directory:
# loads routes from the YAML or XML files found in that directory
resource: '../legacy/routing/'
type: directory
app_bundle:
# loads routes from the YAML or XML files found in some bundle directory
resource: '#AppBundle/Resources/config/routing/public/'
type: directory
I am trying to create a custom bundle that is using a special authentication service. This bundle will be used by all of our projects.
I want to make it so it is needed a little configuration to use it.
My problem appears when i'm trying to add a security config inside my package like so:
# security.yml
security:
providers:
specialauth:
id: AuthBundle\Security\SpecialAuthProvider
firewalls:
main:
logout:
path: '/logout'
When I do this inside my bundle I get this error:
Looked for namespace "security", found none
If I move this security configuration inside my app/config it works ok but I want this config to stay in the AuthBundle so the developers don't have to configure much stuff for every project.
Is this a restriction from symfony not allowing security configs from external bundles or what can the problem be?
You can import your security.yml inside the security file of the project:
app/config/security.yml :
imports:
- { resource: '#AuthBundle/Resources/config/security.yml' }
Since I am new to Symfony and I couldn't manage to find some useful information in google I decided to write to you.
I've read about the way of loading custom DI alias information from a dependency injector in your bundle and how to create a Configuration class that will expose the alias structure. However I am to some extend confused how I can create a file, for example routing.yml, in my AcmeBundle/Resources/config/ folder and read the data from it. E.g:
some_alias:
resource: "#AcmeBundle/Controller/"
type: annotation
prefix: /
I want to make a bundle with routing, independent from the main configuration files in the app folder.
You can create your bundle routing.yml in your WhateverBundle/Resources/config/routing.yml and the in the app/routing.yml just include your bundle's routes.
mybundleorwhatever:
resource: "#WhateverBundle/Resources/config/routing.xml"
Referring to this,
http://symfony.com/doc/current/book/routing.html
we can map url pattern to controller and action
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: { _controller: AcmeBlogBundle:Blog:show }
I want to map the path to external url.
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: "www.example.com/blog"
The requirement is, my current website is in kohana, I am porting it gradually to symfony 2. For my symfony2 app kohana URL are like external urls, I want to configure these urls in routing and use them in standard way,
e.g. in Twig,
<a href="{{ path('blog_show'}}">
Read this blog post.
</a>
So later on when I port my pages to Symfony, I will have to change only routing file so that I could use same blog_show key to refer to url and I wont' have to change all the files where I have used urls.
You can do this by using one of the Symfony framework controllers although I'm not sure how this would work with parameters:
blog_show:
path: /blog/{slug}
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: "http://example.com/blog"
permanent: true
Note that path: /blog/{slug} grabs the slug directly, but path: "http://example.com/blog/{slug}" doesn't work.
Source: http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html
As of Symfony 2.2 this is possible by adding the host constraint to the routes:
routing.yml
user_homepage:
path: /path/to/whatever
host: "sub.domain.ext"
defaults:
_controller: forExampleAnyNamespaceBundle:Controller:action
There's an official blog post on this issue: http://symfony.com/blog/new-in-symfony-2-2-url-host-support-in-the-routing
The router feature of Symfony doesn't work that way...
I suggest you create a Twig extension for this. Read more about this here:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
You could create a function that works very similar to the regular url() function, so you can migrate as easily as possible.
{{ legacyUrl('blog_post', {slug: 'my-blog-post'}) }}
After you migrated the blog to Symfony, all you need to do is create a route called "blog_post" and change "legacyUrl" to "url".
Seems there is no native Symfony way to handle this problem.