I followed official tutorials to install FOSUser then SonataUser bundles and my app/Application/Sonata/UserBundle/Entity extension.
Now I'm having 4 tables: fos_user, fos_user_group, fos_user_user and fos_user_user_group.
my security.yml
security:
providers:
fos_userbundle:
id: fos_user.user_manager
my config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Me\UserBundle\Entity\User
# user_class: Application\Sonata\UserBundle\Entity\User
my /app/Application/Sonata/UserBundle/Resources/config/doctrine/User.orm.xml
...
<entity name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
...
I also have created my UserBundle like it's written fosuser docs.
So users are authenticated with fos_user but sonata admin shows users from fos_user_user
What could be wrong in my config ?
I've spend couple of hours founding that both fos_user and sonata_user should be registred in config.yml:
fos_user:
db_driver: orm
firewall_name: main
user_class: App\UserBundle\Entity\User
group:
group_class: App\UserBundle\Entity\Group
sonata_user:
class:
user: Me\UserBundle\Entity\User
group: Me\UserBundle\Entity\Group
I finally restarted the whole FOSUser & SonataUser/Admin installation by following this good tutorial step by step.
I think my error was to extend FOSUser with my bundle while Sonata extends it already with easy extend.
So I completely removed my UserBundle.
Related
I'm new to Symfony, I follow a tutorial, the part about security and user management but I'm stucked with a problem that seems to come from my routing.
I just created a login form that is actually working, when I go on /app_dev.php/login, the form shows up, I can fill it, but when I submit it, I got the following error :
No route found for "GET /" (from "http://dev-05/ANTOINE/Symfony/web/app_dev.php/login")
404 Not Found - NotFoundHttpException
1 linked Exception:
ResourceNotFoundException ยป
After getting this error, if I go back on the home page, I can see I am connected, so it's working, but the redirection is not .
According to the documentation, this comes from the routing that might be wrongly configured, but I don't know where I made a mistake.
Here's my form, my security.yml and my routing.yml files :
{% extends "AKMUserBundle::layout.html.twig" %}
{% block akmuser_body %}
{% if error %}
<div class="alert alert-danger">{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Login : </label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Mot de passe :</label>
<input type="password" id="password" name="_password" />
<br />
<input type="submit" value="Connexion" />
</form>
{% endblock %}
security.yml :
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:
memory:
users:
user: { password: userpass, roles: ['ROLE_USER'] }
admin: { password: adminpass, roles: ['ROLE_ADMIN'] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
form_login:
login_path: login
check_path: login_check
logout:
path: logout
target: /platform
routing.yml :
akm_platform:
resource: "#AKMPlatformBundle/Resources/config/routing.yml"
prefix: /platform
login:
path: /login
defaults:
_controller: AKMUserBundle:Security:login
login_check:
path: /login_check
logout:
path: /logout
I'm aware that .yml files are very sensitive and need 4 spaces instead of the usual indentation, so I rewrote the files line by line, with the spaces, but it is still not working.
I hope someone can help me :p
If you need some informations don't hesitate!
Edit : Here is my result of the php bin/console debug:router
Edit 2 : To get rid of my problem I just had to add the default_target_path in my security.yml :
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:
memory:
users:
user: { password: userpass, roles: ['ROLE_USER'] }
admin: { password: adminpass, roles: ['ROLE_ADMIN'] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
form_login:
login_path: login
check_path: login_check
default_target_path: akm_platform_home
logout:
path: logout
target: /platform
Refresh your cache, console command:php bin/console cache:clear, if you are using older symfony it's app/console instead of bin/console. You can debug your routes with a command: php bin/console debug:router. This is the main system console and in my case I use it on Windows. You must be in the project folder for them to work.
I am not sure where you get redirected to "/", i recently started working in Symfony and most issues were with refreshing cache and wrong yml and route names. However in your case it may be that symfony goes to route / on successful login, you can add default_target_path: your_homepage_route_name or /where_you_want_to_go
it may be what's the issue here.
Since you're new, when you include routes and define a prefix for them, you can easily forget that you set it, which is why router debugging is great since you can see all the info there very easily. When working in symfony always have a console window open if not working in an edior with a built in console. I think JetBeans has it, all of those tutorials are done in it. PS, youtube tutorials for symfony are great, for example Symfony and PHP Programming channel has a good beginner tutorial.
I make user of the Sonata User Bundle. According to the documentation under section 2.5 Extending the Bundle they want me to generate a complete new bundle for my user and group entities. I think this is completely unnecessary and I don't want that extra bundle. So I've created my User and Group entities in my AppBundle and I extend them from the Sonata\UserBundle\Entity\BaseUser entities.
After this, I've changed my fos_user user_class and group_class to my new entities.
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
group:
group_class: AppBundle\Entity\Group
group_manager: sonata.user.orm.group_manager
service:
user_manager: sonata.user.orm.user_manager
Everything works perfectly, my user and group tables in my database is generated correctly, I can create users through the fos user command line, and I can log in.
In the menu is an automatically generated user group that contain the user and group entities (see the image below). Now the only problem is to override the services for this entities to them to use my own entity classes, because when I click now on one of them they want the entities in the extended bundle that I don't want. How can I tell sonata to make use of my own services? Or even, how can I just remove or hide the Users (with Users and Groups) completely?
After some digging in Sonata User Bundle files, I see that the entities can be set with a parameter. So all I had to do was to add;
parameters:
sonata.user.admin.user.entity: AppBundle\Entity\User
sonata.user.admin.group.entity: AppBundle\Entity\Group
in my config.yml file.
"Or even, how can I just remove or hide the Users (with Users and Groups) completely?"
So, we have SonataUserBundle and our AppBundle.
In both of them we have User and Group Entity. And we don't want to use entities from sonata - we just extend them. But SonataUserBundle has already had the UserAdmin and GroupAdmin classes inside.
That's why, after installing SonataUserBundle in the admin menu appear two services:
As you know, every sonata admin class we declare in the services.yml file.
In SonataUserBundle we have another files, which sonata developers declare services in. In case of using doctrine orm we should look at admin_orm.xml file, which lies in this path:
vendor/sonata-project/user-bundle/Resources/config/admin_orm.xml
Inside the file we can find the declaration of the admin services - UserAdmin and GroupAdmin:
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="sonata.user.admin.groupname">sonata_user</parameter>
<parameter key="sonata.user.admin.label_catalogue">SonataUserBundle</parameter>
<parameter key="sonata.user.admin.groupicon"><![CDATA[<i class='fa fa-users'></i>]]></parameter>
</parameters>
<services>
<service id="sonata.user.admin.user" class="%sonata.user.admin.user.class%">
<tag name="sonata.admin" manager_type="orm" group="%sonata.user.admin.groupname%" label="users" label_catalogue="%sonata.user.admin.label_catalogue%" label_translator_strategy="sonata.admin.label.strategy.underscore" icon="%sonata.user.admin.groupicon%"/>
<argument/>
<argument>%sonata.user.admin.user.entity%</argument>
<argument>%sonata.user.admin.user.controller%</argument>
<call method="setUserManager">
<argument type="service" id="fos_user.user_manager"/>
</call>
<call method="setTranslationDomain">
<argument>%sonata.user.admin.user.translation_domain%</argument>
</call>
</service>
<service id="sonata.user.admin.group" class="%sonata.user.admin.group.class%">
<tag name="sonata.admin" manager_type="orm" group="%sonata.user.admin.groupname%" label="groups" label_catalogue="%sonata.user.admin.label_catalogue%" label_translator_strategy="sonata.admin.label.strategy.underscore"/>
<argument/>
<argument>%sonata.user.admin.group.entity%</argument>
<argument>%sonata.user.admin.group.controller%</argument>
<call method="setTranslationDomain">
<argument>%sonata.user.admin.group.translation_domain%</argument>
</call>
</service>
</services>
As you can see here the ids of our services:
sonata.user.admin.user
sonata.user.admin.group
The simplest method to overwrite them is to create the same services (I mean we will use this ids ) in our services.yml file.
Yes, you can have an argument with me, that this method is stupid, but as I said it's not the only one.
So Sonata services in OUR services.yml will looks like this:
sonata.user.admin.user:
class: "%sonata.user.admin.user.class%"
arguments: [~, "%sonata.user.admin.user.entity%", "%sonata.user.admin.user.controller%"]
tags:
- { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "%sonata.user.admin.label_catalogue%", label: "users", icon: "<i class=\"fa fa-users\"></i>" }
calls:
- [ setUserManager, [ "#fos_user.user_manager" ] ]
- [ setTranslationDomain, [ "%sonata.user.admin.user.translation_domain%" ] ]
sonata.user.admin.group:
class: "%sonata.user.admin.group.class%"
arguments: [~, "%sonata.user.admin.group.entity%", "%sonata.user.admin.group.controller%"]
tags:
- { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "%sonata.user.admin.label_catalogue%", label: "groups" }
calls:
- [ setTranslationDomain, [ "%sonata.user.admin.group.translation_domain%" ] ]
Now you can update the admin dashboard and see, that nothing have happened. But the our purpose was to disable this service from our dashboard and menu. Let's do the trick. Add
show_in_dashboard: false
tags:
- { show_in_dashboard: false, name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "%sonata.user.admin.label_catalogue%", label: "users", icon: "<i class=\"fa fa-user\"></i>" }
to the declaration of this service in services.yml.
Therefore our services will look like this:
sonata.user.admin.user:
class: "%sonata.user.admin.user.class%"
arguments: [~, "%sonata.user.admin.user.entity%", "%sonata.user.admin.user.controller%"]
tags:
- { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "%sonata.user.admin.label_catalogue%", label: "users", icon: "<i class=\"fa fa-user\"></i>", show_in_dashboard: false }
calls:
- [ setUserManager, [ "#fos_user.user_manager" ] ]
- [ setTranslationDomain, [ "%sonata.user.admin.user.translation_domain%" ] ]
sonata.user.admin.group:
class: "%sonata.user.admin.group.class%"
arguments: [~, "%sonata.user.admin.group.entity%", "%sonata.user.admin.group.controller%"]
tags:
- { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "%sonata.user.admin.label_catalogue%", label: "groups", show_in_dashboard: false }
calls:
- [ setTranslationDomain, [ "%sonata.user.admin.group.translation_domain%" ] ]
After this trivial manipulations Sonata services will completely disappear from your Dashboard.
I am using SonataUserBundle and JMSSerializerBundle and I would like to hide the token and other properties of my serialized object.
The file I want to ovvride in SonataUserBundle is Resources/config/serializer/Model.User.xml .
Here is my configuration:
app/config.yml
jms_serializer:
metadata:
auto_detection: true
directories:
- { path: %kernel.root_dir%/Resources/SoantaUserBundle/serializer, namespace_prefix: 'Sonata\UserBundle' }
- { path: %kernel.root_dir%/Resources/FOSUserBundle/serializer, namespace_prefix: 'FOS\UserBundle' }
and in app/Resources/SonataUserBundle/serializer I have tried 2 files.
Model.User.xml
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<class name="Sonata\UserBundle\Model\User" exclusion-policy="all" xml-root-name="user">
<property name="token" type="string" expose="false" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
</class>
</serializer>
Model.User.yml
Sonata\UserBundle\Model\User:
exclusion_policy: ALL
properties:
token:
expose: false
Both files dont seem to work.
I have managed to hide some properties from the FOSUserBundle, but seems I have troubles hiding the ones related to SonataUserBundle. I'm not sure if it's relevant but I would like to mention that I am using also am using HWIOauthBundle.
Any help will be greatly appreciated.
First, I don't think you need autodetection since you're specifying also the directories. Then you have a couple of typos in the sonata directory path:
jms_serializer:
metadata:
directories:
- { path: %kernel.root_dir%/Resources/SonataUserBundle/serializer, namespace_prefix: 'Sonata\UserBundle' }
I am setting up the user management for a website with FOSUser for the first time and I'm having some trouble figuring out where I mess up.
The role is in the database, in the roles column, like this:
a:1:{i:0;s:10:"ROLE_ADMIN";}
When I do a var_dump($this->getUser()) from the controller, I get this:
(...)
["roles":protected]=> array(1) { [0]=> string(10) "ROLE_ADMIN" }
(...)
So everything's fine over here too.
When I try either if ($this->get('security.context')->isGranted('ROLE_ADMIN')) from the controller or {% if is_granted('ROLE_ADMIN') %} from a twig template, Symfony doesn't detect the role. The profiler also tells me there is only the ROLE_USER role.
Here is my app/config/security.yml file:
security:
providers:
main:
id: fos_user.user_provider.username
encoders:
Site\UserBundle\Entity\User: sha512
role_hierarchy:
ROLE_MODERATOR: [ROLE_USER]
ROLE_ADMIN: [ROLE_MODERATOR]
firewalls:
main:
pattern: ^/
anonymous: true
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
logout:
path: fos_user_security_logout
target: /
remember_me:
key: %secret%
default:
anonymous: ~
It looks like you didn't clear the cache. Try to clear cache for your environment
php app/console cache:clear --env=prod #for prod env
or
php app/console cache:clear #for dev env
To help confused passers-by:
In case you have followed the Symfony security tutorial at http://symfony.com/doc/current/security/entity_provider.html#create-your-user-entity, you may have implemented a method like this:
public function getRoles()
{
return array('ROLE_USER');
}
If you have switched to FOSUserBundle later on, you may have forgotten to remove this method. For me, this was the (home-made) issue why my users' roles didn't load properly.
If i am not confuse then you have in your line in security.yml
role_hierarchy:
ROLE_MODERATOR: [ROLE_USER]
ROLE_ADMIN: [ROLE_MODERATOR]
which describe that in both roles you have entered the value
ROLE_USER
so you should do like following lines
role_hierarchy:
ROLE_MODERATOR: [ROLE_USER]
ROLE_ADMIN: [ROLE_ADMIN]
ROLE_SUPER_ADMIN: [ROLE_MODERATOR,ROLE_ADMIN]
I finished installing sonata-admin bundle + fosuser bundle.
after creating users using the command line , i found out that when i login using admin admin its gives me bad credentials , so after debugging for a while i found out that the fos:user:create save users to fos_user_user not user table .
how to fix this and make the fos:user:create save created users in user table .
here is a snippet of my config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
#user_class: Sizar\JobeetBundle\Entity\SizarUser
group:
group_class: Application\Sonata\UserBundle\Entity\Group
sonata_user:
class: # Entity Classes
user: Application\Sonata\UserBundle\Entity\User
if needed any configs then i will add it in the comments below
the User.php class
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
class User extends BaseUser {
protected $id;
public function __construct() {
parent::__construct();
}
public function getId() {
return $this->id;
}
}
the orm User file
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>
You will have to update you mapping config, so that doctrine knows which table it is mapped to
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping ...>
<entity name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
...
</entity>
</doctrine-mapping>
Here you will need to update the "table" property to the correct database table. Possibly you will need to recreate the database.
See FosUserBundle - Install docs for further info about the setup of your entities for the usage with FosUserBundle