Symfony 4 fosuserbundle - symfony

I begin with Symfony 4 and I want to install FosUserBundle with this link :
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
First :
My problem is that I don't know where to find the "app/config/config.yml" file to uncomment the translator and to configure :
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
Second :
I think that I have to create the security.yml file in "config/packages/" directory, is that right ?
Third :
And in which file to add the route ?
Could you help me, please ? :)

I've resolved the problem followed this:
download FOSUserBundle using composer:
composer require friendsofsymfony/user-bundle "~2.0"
At the end of the installation you will have the following error message :
The child node "db_driver" at path "fos_user" must be configured.
Create your User class
Create src/Entity/User.php as custom user class who extend the FOSUserBundle BaseUser class.
<?php
//src/Entity/User.php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Configure your application's security.yml
Modify config/packages/security.yaml to setup FOSUserBundle security
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
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 }
Configure the FOSUserBundle
Create a new file config/packages/fos_user.yaml for the configuration of FOSUserBundle
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: App\Entity\User
from_email:
address: "vincent#vfac.fr"
sender_name: "vincent#vfac.fr"
Update config/packages/framework.yaml to add templating configuration
framework:
templating:
engines: ['twig', 'php']
Import FOSUserBundle routing
Create config/routes/fos_user.yaml
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
Update your database schema
If not already done, you must create your database
php bin/console doctrine:database:create
Update the schema with the informations from your User class entity
php bin/console doctrine:schema:update --force
At this point, all is installed and configured to use FOSUserBundle in Symfony 4. Run the following command to check if all is ok
composer update
If you don't have any error message, you can test !
You can run the web server to test your application
php bin/console server:start
all tutorial here:
https://vfac.fr/blog/how-install-fosuserbundle-with-symfony-4

This is the solution I found to work.
First:
app/config/config.yml doesn't exist anymore instead the configs have been moved to the config folder. For the FOS User Bundle the correct location: /config/packages/fos_user.yaml. As already noted, use the dev-master version FOSUserBundle, it supports 4(still a little work in progress but good enough).
Second:
You are correct, a simple solution is do a composer require security and the recipe will take care of that for you.
https://symfony.com/doc/current/security.html for more info.
Third:
The default FOS User Bundle routes:
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
More info on the FOS routing (step 6)is helpful
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
Also, I recommend looking at the yaml samples in symfony routing documentation. It may make things a little more clear when configuring the routes with relation to FOS User Bundle.
https://symfony.com/doc/current/routing.html

ok i have same problem and it should be like that
first:as #sean Baharmi says you should create /config/packages/fos_user.yaml and inter configuration like this
fos_user:
db_driver: orm
firewall_name: main
user_class: App\Entity\Users
from_email:
address: "hello#youmail.com"
sender_name: "Sender Name"
then in framework.yaml you should enter following because of FOSUserBundle dependencies
templating:
engines: ['twig', 'php']
also for add routing in /config/rourtes/routes.yaml add
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
then it is ready to work
hope works for you

You can't use FOSUSerBundle on Symfony4 at the moment. Support has not been merged yet. You can follow development here.

If you want to use FOSUserBundle with Symfony4 you can try the patch Ryan provided here.

Related

User Role based routes in Symfony

I've got five types of roles in my Symfony project.
I want to create restrictions for each role, so that they will have access to specific routes. They will not be able to access others paths. I want to do this in an efficient way like Laravel uses middleware in the routes for access. Is there any way like this in Symfony?
You could do that in different ways, and it depends on your Symfony version.
In the older Symfony versions, the most common way to restrict access to certain routes is adding into config/packages/security.yaml some parameters such as:
access_control:
- { path: '^/agent', roles: ROLE_AGENT }
- { path: '^/profile', roles: ROLE_USER }
- { path: '^/admin', roles: ROLE_ADMIN }
- { path: '^/admin/statistics', roles: ROLE_SUPER_ADMIN }
Also, it's good practice to define the role hierarchy into the same file config/packages/security.yaml :
`role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN`
Hence, the users with a SUPER_ADMIN_ROLE will have access to routes restricted to ROLE_ADMIN.
However, my recommendation is if you've several parts to restrict define the general access roles into the config/packages/security.yaml file such as and define a role hierarchy as I said before:
access_control:
- { path: '^/admin', roles: ROLE_ADMIN }
- { path: '^/profile', roles: ROLE_USER }
Then restrict the other routes using annotations thanks to SensioFrameworkExtraBundle, for example:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
/**
* Require ROLE_ADMIN_SYSTEMS for *every* controller method in this class.
*
* #IsGranted("ROLE_ADMIN_SYSTEMS")
*/
public function YourFunction() { }
For more information look into official Symfony Docs that are very clear. Symfony Security official documentation
I hope this could help you, if you've any other doubts just tell me,
Kind regards.

Install FOS UserBundle

I'm following a tutorial to install this FOSUserBundle to my project.
I got my information from this french tutorial: http://www.tutodidacte.com/symfony2-installer-fosuserbundle
So I did those commands:
php ./composer.phar require friendsofsymfony/user-bundle "~2.0#dev"
php composer.phar update
Then I created a new Bundle,
php bin/console generate:bundle
Bundle Namespace : Kingdom/UserBundle
But after doing that, in the AppKernel i can see the new UserBundle, mais the FOSUserBundle isn't here.
I try to add it by myself writting it in the file; but after when i try to create an entity we can see something is clearly wrong.
Sorry for the presentation of this below...I haven't succeed to print it correctly.
php bin/console generate:doctrine:entity
Fatal error: Uncaught exception 'Symfony\Component\Config\Definition\Exception\I
nvalidConfigurationException' with message 'The child node "db_driver" at path "
fos_user" must be configured.' in C:\wamp\www\Kingdom\vendor\symfony\symfony\src
\Symfony\Component\Config\Definition\ArrayNode.php:240
Stack trace:
0 C:\wamp\www\Kingdom\vendor\symfony\symfony\src\Symfony\Component\Config\Defin
ition\BaseNode.php(303): Symfony\Component\Config\Definition\ArrayNode->finalize
Value(Array)
1 C:\wamp\www\Kingdom\vendor\symfony\symfony\src\Symfony\Component\Config\Defin
ition\Processor.php(37): Symfony\Component\Config\Definition\BaseNode->finalize(
Array)
2 C:\wamp\www\Kingdom\vendor\symfony\symfony\src\Symfony\Component\Config\Defin
ition\Processor.php(50): Symfony\Component\Config\Definition\Processor->process(
Object(Symfony\Component\Config\Definition\ArrayNode), Array)
3 C:\wamp\www\Kingdom\vendor\friendsofsymfony\user-bundle\DependencyInjection\F
OSUserExtension.php(51): Symfony\Component\Config\Definition\Processor->processC
onfigur in C:\wamp\www\Kingdom\vendor\symfony\symfony\src\Symfony\Component\Conf
ig\Definition\ArrayNode.php on line 240
It seems you haven't properly configured the bundle. Follow the steps here:
http://symfony.com/doc/current/bundles/FOSUserBundle/index.html
In your case, it seems you are missing at least this:
http://symfony.com/doc/current/bundles/FOSUserBundle/index.html#step-5-configure-the-fosuserbundle
EDIT: After reading your question again, it seems you're not loading the bundle properly, as described here:
http://symfony.com/doc/master/bundles/FOSUserBundle/index.html#step-2-enable-the-bundle
After Installation A bundle needs to be enabled in AppKernel.php file. FOSUserBundle needs a bit of configuration to make it work properly I have written a simple and easy guide on it:
https://www.cloudways.com/blog/implement-fosuserbundle-in-symfony-3-1/
Installation:
composer require friendsofsymfony/user-bundle "~2.0#dev"
Enabling Bundle in AppKernel.php
After installing FOSUserBundle you must enable it in the project. Go to app/config/AppKernel.php and add the highlighted line in the bundles array.
$bundles = [
...
new FOS\UserBundle\FOSUserBundle(),
]
Create User Entity
Create the User entity as you are creating above :)
Configuring Security.yml file
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
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_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
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 }
Configure FOSUserBundle in a Config
Add this config in config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
Importing Route files of FOSUserBundle
Add this to import routes in Routing.yml
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
Updating Database Schema
The last step is to update the database schema to create table in the database.
php bin/console doctrine:schema:update --force
Now move to the app URL add /login in it you will see the login page.

FOSUserBundle how to extend handler correctly

It throws this error:
Unrecognized option "handler" under "fos_user.registration.form"
And the files are:
//services.yml
app.form.registration:
class: AppBundle\Form\AccomodationFrontSignUpType
tags:
- { name: form.type, alias: app_user_registration }
app.form.handler.registration:
class: AppBundle\Form\Handler\RegistrationFormHandler
arguments: ["#fos_user.registration.form", "#request", "#fos_user.user_manager", "#fos_user.mailer", "#fos_user.util.token_generator"]
scope: request
public: false
//config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
group:
group_class: AppBundle\Entity\Group
service:
mailer: swiftmailer.mailer.strato
from_email:
address: webmyhomepage#strato.com
sender_name: myHomepage
registration:
form:
handler: app.form.handler.registration
type: app_user_registration
confirmation:
template: FOSUserBundle:Registration:email.txt.twig
enabled: true
Any help is welcome, thanks
The error means that there is an invalid parameter in your config. The key handler in your config
-> handler: app.form.handler.registration
if you take a look inside the fos user bundle DependencyInjection folder, Configuration.php file, that option does not exist. so its invalid :).
There you can see witch options are available. In my current version of this bundle i have this options available under form: type, name, validation_groups.
Also you can see the default configuration for a bundle with:
php app/console debug:config FOSUserBundle

In Symfony2 (v2.3.4) - FOSUserBundle conflicts with Controller Route generated by CRUD

First of all im still learning so don't be angry at me for asking this question (and for my English - im trying my best).
Im going through book tutorial which is written for Symfony 2.0.10, but for each exercise i'm using newest Symfony 2.3.4 project, solving out eventually changes (learning that way) with a good results but finally i'm stuck.
The problem is that the point of exercise is to "make an app which is accessible only for logged users" using FOSUserBundle and CRUD panel. (without registering and all that unnecessary stuff)
Like in the tutorial, i created a bundle (My/BackendBundle), deleted its controller and views, then i created a entity called MyBackendBundle:Mountain and populate db with my data. Next i created CRUD panel for entity i've created before, so the new controller appears wth all those "show", "new" "edit" etc methods. The important thing is that generated controller class (which is named MountainController because of "MyBackendBundle:Mountain" entity) have this #Routing annotation before class:
/**
* Mountain controller.
*
* #Route("/mountain")
*/
class MountainController extends Controller
{
...
But tutorial ordered to delete this annotation in order to use simply Project/web/ address instead of Project/web/mountain. so i did.
Then i created and admin account and change my routing.yml to looks like this:
routing.yml
my_backend:
resource: "#MyBackendBundle/Controller/"
type: annotation
prefix: /
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
Next step of tut is to modify the security.yml to looks like this:
security.yml
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
#id: fos_user.user_manager
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
# the user is redirected here when he/she needs to login
login_path: /login
# if true, forward the user to the login form instead of redirecting
use_forward: false
# submit the login form here
check_path: /login_check
# by default, the login form *must* be a POST, not a GET
post_only: true
#remember_me: false
# login success redirecting options (read further below)
always_use_default_target_path: false
default_target_path: /
target_path_parameter: _target_path
use_referer: false
# login failure redirecting options (read further below)
failure_path: null
failure_forward: false
# field names for the username and password fields
username_parameter: _username
password_parameter: _password
# csrf token options
csrf_parameter: _csrf_token
intention: authenticate
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_ADMIN }
Next steps is about to add logout link to base.html.twig and to override the login page, but this probably doesn't matter because problems already started. When i try to run my app i've got Unable to find Mountain entity. exception. which is pointed to this function in MountainController:
/**
* Finds and displays a Mountain entity.
*
* #Route("/{id}", name="mountain_show")
* #Method("GET")
* #Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBackendBundle:Mountain')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Mountain entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
I'm almost sure it has something to do with the #Route("/{id}", name="mountain_show") annotation which is generated by CRUD because the "login_path" from security.yml which is "/login" fits to #Route pattern of showAction. So the action instead of getting the id of record to show (which should be a number), receives an text or i don't know what, and tries to find id with negative results.
Ps. The example from tutorial (on Symfony 2.0.10) working because the "showAction" generated by CRUD has route: #Route("/{id}/show", name="mountain_show")
which isn't conflicts.
So if there is anybody who can help me with this i will be very appreciated.
If there is any more info i can give to better explain my problem just say. Regards. KB
symfony's routing will try to match the first matching route found ... in your case the other controller's annotation routes are configured before/above FOSUserBundle's ones ... Therefore symfony will first try to match /{id} and then /login.
just move FOSUserBundle's routes before your other controller's route in your configuration to fix this issue.

Fr3D LDAP BUNDLE with symfony2

I have integrated FOS BUndle and Fr3d Bundle with symfony2. Here is the bug I have got
MappingException: No mapping file found named 'Acme.UserBundle.Entity.User.php' for class 'Acme\UserBundle\Entity\User'.
Here is my config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Acme\UserBundle\Entity\User
fr3d_ldap:
client:
user:
baseDn: cn=admin, dc=test, dc=local
filter: (&(ObjectClass=Person))
attributes: # Specify ldap attributes mapping [ldap attribute, user object method]
service:
user_manager: fos_user.user_manager # Overrides default user manager
ldap_manager: acme.ldap.ldap_manager # Overrides default ldap manager
Please help me
In your config.yml, you need to update your user_class setting under fos_user...
fos_user:
user_class: YourVendorName\UserBundle\Entity\User
Make sure that the user_class property is pointing to a valid entity in your user bundle (and obviously make sure you have a UserBundle)

Resources