Override Sonata Users services Menu Group - symfony

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.

Related

Symfony Sonata Admin - Security issue with Role and on_top

i got a problem with security and menu.
I have a menu point "Besuchsmanagement" set on_top: true
Now i have roles for this.
My problem is, that the on_top always be shown, if role is granted or not.
ROLES:
role_hierarchy:
ROLE_ADMIN_VISIT_READER:
- ROLE_ADMIN_VISIT_LIST
- ROLE_ADMIN_VISIT_VIEW
ROLE_ADMIN_VISIT_CREATOR:
- ROLE_ADMIN_VISIT_READER
- ROLE_ADMIN_VISIT_CREATE
- ROLE_ADMIN_VISIT_EDIT
ROLE_ADMIN_VISIT_ADMIN:
- ROLE_ADMIN_VISIT_ALL
ROLE_USER:
- ROLE_ADMIN_VISIT_ADMIN
services.yml
admin.visit:
class: ifabrik\VisitBundle\Admin\VisitAdmin
arguments: [~, ifabrik\VisitBundle\Entity\Visit, ~]
tags:
- { name: sonata.admin, group: Besuchsmanagement, manager_type: orm, on_top: true, icon: '<i class="fa fa-map-signs" aria-hidden="true"></i>' }
When i remove on_top - then roles are correct.
Instead of passing on_top in the service definition you should be configuring your admin menu in your app/config.yml.
Here is an example :
sonata_admin:
dashboard:
groups:
Customers Relationship Management:
label: libio.menu_label.crm
icon: '<i class="fa fa-address-card"></i>'
on_top: true
roles: [ROLE_SUPER_ADMIN]
items:
- route: admin_librinfo_crm_organism_list
label: libio.menu_label.organisms_list
As you can see you can specify the roles to wich the menu entry will be shown.
To find out the route execute the command
app/console (bin/console on sf3) debug:router

Cannot override serializer config in SonataUserBundle to hide properties

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' }

SonataAdminBundle custom group icon

I'm using SonataAdminBundle to generate a CRUD for my Page entity.
With the yaml file bellow the menu is displayed in the sidebar.
I would like to change the group default icon which is "fa fa-folder".
But I don't find the option to do it.
sonata.admin.page:
class: FM\AppBundle\Admin\Page\Page
tags:
- { name: sonata.admin, manager_type: orm, group: "CMS", label: "Pages" }
arguments:
- ~
- FM\AppBundle\Entity\Page\Page
- ~
"icon" option:
icon: "<i class=\"fa fa-folder\"></i>"
In your example:
sonata.admin.page:
class: FM\AppBundle\Admin\Page\Page
tags:
- { name: sonata.admin, manager_type: orm, group: "CMS", label: "Pages", icon: "<i class=\"fa fa-folder\"></i>" }
arguments:
- ~
- FM\AppBundle\Entity\Page\Page
- ~

LiipImagineBundle imagine_filter not working

I have a problem with the imagine_filter that works in one page and not in the others even if I use it exactly the same way with the same photo.
In the first page, where my filter works, I have this src:
http://myserver.com/media/cache/shooting/photo_preview/75/55cb71cc8ba26-00001.jpg
However, on the page where the filter doesn't work, I have this src:
:///media/cache/shooting/photo_preview/75/55cb71cc8ba26-00001.jpg
Has anybody already have a problem like that?
Moreover, the first src only works on app.php and not on app_dev.php.
On my application, I'm using Gaufrette to upload photos to S3 and then Liip to apply the filters. The cache is on my server. This is my configuration:
"liip/imagine-bundle": "1.3.*#dev",
"knplabs/gaufrette": "0.1.*",
"knplabs/knp-gaufrette-bundle": "0.1.*#dev",
"aws/aws-sdk-php": "2.8.*#dev",
<--- The services --->
services:
mycompany.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
-
key: %amazon_aws_key%
secret: %amazon_aws_secret_key%
region: %amazon_aws_region%
mycompany.liip_imagine.binary.loader.stream.shooting:
class: '%liip_imagine.binary.loader.stream.class%'
arguments:
- 'gaufrette://shooting/'
tags:
- { name: 'liip_imagine.binary.loader', loader: 'stream.shooting' }
<--- Gaufrette --->
knp_gaufrette:
adapters:
shooting:
aws_s3:
service_id: mycompany.aws_s3.client
bucket_name: %amazon_s3_bucket%
options:
directory: shooting
filesystems:
shooting:
adapter: shooting
alias: shooting_filesystem
stream_wrapper: ~
<--- Liip --->
liip_imagine:
resolvers:
default:
web_path: ~
shooting:
web_path:
cache_prefix: /media/cache/shooting
controller:
filter_action: mycompany_imagine.controller:filterAction
filter_sets:
photo_preview:
data_loader: stream.shooting
cache: shooting
quality: 50
filters:
upscale: { min: [690, 690] }
thumbnail: { size: [690, 690], mode: outbound}
<--- Twig --->
<img src="{{ photo.imagepath | imagine_filter('photo_preview') }}" alt="">
I got an answer on Github. I just needed to change my resolver:
https://github.com/liip/LiipImagineBundle/issues/203

Confused with FOSUser SonataUser and my extension

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.

Resources