Overriding the User Admin Form - Sonata Sandbox

3,521 views
Skip to first unread message

Cassiano Tartari

unread,
Jan 29, 2013, 1:30:54 PM1/29/13
to sonata...@googlegroups.com
Hi everybody,

   I've been trying for a while override without success the user admin form to exclude some fields and add some news specific for my applicarion but any solution had worked.

   I've tryed:
   Somebody can please give some working example. I'm under sonata sandbox using all the last features.

Thanks!
Cassiano Tartari.

Nelson Jesus Suniaga Romero

unread,
Jan 29, 2013, 2:41:42 PM1/29/13
to sonata...@googlegroups.com
It's very simple: just put the UserAdmin.php class file in YourProject/src/XYZApplication/SomeBundle/Admin/ directory.

Remember the UserAdmin class must extends Sonata\AdminBundle\Admin\Admin
 class.

Greetings!

--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonata-users...@googlegroups.com.
To post to this group, send email to sonata...@googlegroups.com.
Visit this group at http://groups.google.com/group/sonata-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Cassiano Tartari

unread,
Jan 30, 2013, 6:17:59 AM1/30/13
to sonata-users
Thanks I will try this.

Today I tryed this:

1) php app/console sonata:easy-extends:generate SonataUserBundle --dest=src

2) I copied the folder Admin/Model from sonatauser bundle in vendors and changed the namespace to Application\Sonata\UserBundle\Admin\Model

3) Edited the 2 entities Application\Sonata\UserBundle\Entity\User and Application\Sonata\UserBundle\Entity\Group like in attachment.

4) In the config.yml put:
fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User

    group:
        group_class: Application\Sonata\UserBundle\Entity\Group


sonata_user:
#...
    class:                  # Entity Classes
        user:               Application\Sonata\UserBundle\Entity\User
        group:              Application\Sonata\UserBundle\Entity\Group

    admin:                  # Admin Classes
        user:
            class:          Application\Sonata\UserBundle\Admin\Model\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

        group:
            class:          Application\Sonata\UserBundle\Admin\Model\GroupAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

5) Edit UserAdmin to show my new fields like in attachment.

6)
Drop the db and recreate:
php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
php app/console fos:user:create root ro...@example.com password --super-admin

BUT, when I take a look at the db my new user table is empty all the users are created at fos_user_user...

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code
User.php
Group.php
UserAdmin.php

Cassiano Tartari

unread,
Jan 30, 2013, 8:39:08 AM1/30/13
to sonata-users
Nelson,

I tryed right now what you said. I copied the file from vendors Sonata\UserBundle\Admin\Model\UserAdmin to Company\XXXBundle\Admin and changed the namespace. I took off the profile block in the code and nothing happens in the form.

Can you give detailed instructions? I loosing too much time on this...

Thanks!

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Nelson Jesus Suniaga Romero

unread,
Jan 30, 2013, 2:06:10 PM1/30/13
to sonata...@googlegroups.com
I have to check out my own symfony setup, but there's something you didn't mention: clean the symfony cache after make changes in the configuration files.

Cassiano Tartari

unread,
Jan 30, 2013, 2:07:43 PM1/30/13
to sonata-users
I do this all the time hehehe thanks!

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Nelson Suniaga

unread,
Jan 30, 2013, 3:44:24 PM1/30/13
to sonata...@googlegroups.com
I found the issue. You have a little mistake in your User.php file class: is calling and extending to the FOSUser class instead the Sonata User class.

Uncomment the line 5:

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

and delete (or comment) the line 6:

use FOS\UserBundle\Entity\User as FOSUser;

Finally change the father User class, from FOSUser to BaseUser.

Hope it works!

Cassiano Tartari

unread,
Jan 31, 2013, 5:55:11 AM1/31/13
to sonata-users

Thank you Nelson!! But wasn't it.

I found the solurtion.

There were some mistakes. Let me list:

1) There were 2 xml files that create the tables fos_user_group and fos_user_user they were inside the folder Application\Sonata\UserBundle\Resources\config\doctrine so I deleted this folder.

2) In my config.yml there was:
sonata_user:
    #...
    table:
        user_group:           fos_user_user_group


So I changed the  fos_user_user_group to usuario_grupo

3) The table usuario_grupo is cretead inside my entity user: Application\Sonata\UserBundle\Entity\User

    /**
     * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\Group")
     * @ORM\JoinTable(name="usuario_grupo",
     *      joinColumns={@ORM\JoinColumn(name="usuario_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="grupo_id", referencedColumnName="id")}
     * )
     */
    protected $groups;


4) The last that I did was, like above, change the name of the variable $grupos to $groups (original) because when I use the same name that in original class I will override the variable so my table usuario_grupo will be used!

So now my DB is using only my personal tables.

Just to explain, I didn't extend the Sonata\UserBundle\Entity\BaseUser class because when I do this my user table is created using all that original fields, and if you examine the code you gonna see that BaseUser extends FOS\UserBundle\Entity\User that is the base for the authentication. Now I have the essencial fields for the authentication and my personal for the system.



Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Nelson Jesus Suniaga Romero

unread,
Jan 31, 2013, 6:38:17 AM1/31/13
to sonata...@googlegroups.com
That's great, but the config.yml values doesn't match with the ones in the second message. The SonataAdmin bundle extends the functionality of FOSUser bundle, for example in the authentication login form (check the security.yml file).

It's kind of confusing why you need SonataAdmin in your project. Have a read again of the SonataAdmin documentation just to be clear.

Cassiano Tartari

unread,
Jan 31, 2013, 6:48:47 AM1/31/13
to sonata-users
I don't get what you said...

I have a specific file to the sonata_user configuration, sonata_user.yml that is imported inside config.yml, this is the entire content:

sonata_user:
    security_acl:             false
    table:
        user_group:           usuario_grupo

    impersonating:
        route:                page_slug
        parameters:           { path: / }

    google_authenticator:
        server:               %sonata_user.google_authenticator.server%
        enabled:              true

    manager_type:             orm

    profile:
        form:
            type:                 sonata_user_profile
            handler:              sonata.user.profile.form.handler.default
            name:                 sonata_user_profile_form
            validation_groups:
                # Defaults:
                - Profile
                - Default


    class:                  # Entity Classes
        user:               Application\Sonata\UserBundle\Entity\User
        group:              Application\Sonata\UserBundle\Entity\Group

    admin:                  # Admin Classes
        user:
            class:          Application\Sonata\UserBundle\Admin\Model\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

        group:
            class:          Application\Sonata\UserBundle\Admin\Model\GroupAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle



=========================================================================================

My security.yml:


security:
    encoders:
        Application\Sonata\UserBundle\Entity\User:
            algorithm: sha512
            encode_as_base64: false
            iterations: 1

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH, ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT, ROLE_SONATA_PAGE_ADMIN_BLOCK_EDIT]

        SONATA:
            # if you are not using acl then this line must be uncommented
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT
            - ROLE_SONATA_PAGE_ADMIN_BLOCK_EDIT

    providers:
        fos_userbundle:
            id: fos_user.user_manager

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # This firewall is used to handle the admin login area
        # This part is handled by the Sonata User Bundle
        admin:
            pattern:      /admin(.*)
            context:        user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                default_target_path: /admin/dashboard
                failure_path:   null
                always_use_default_target_path: true
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:    true

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:      .*
            context:        user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:       true
            anonymous:    true

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_USER] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }



I'm using the Sonata Admin because I want to make a intranet system for a company and it provide to me structure for menus, user login,...

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


sonata_user.yml
security.yml

Nelson Jesus Suniaga Romero

unread,
Jan 31, 2013, 5:35:58 PM1/31/13
to sonata...@googlegroups.com
What I mean was that the config.yml you posted didn't have the user_group section included. In other words was impossible to anyone to give you a hand cause the config.yml wrong line wasn't included into the message.

The other thing is the Sonata User class inheritance. If you want to use SonataAdmin and override the user class you should to extend the FOSUser class, cause SonataAdmin generate a bunch of forms, included the user login form.

In fact, FOSUser have its own forms, login, password resetting, data updating, etc. But Sonata Admin use its own forms. Just to make it clear, have a look of the Sonata Sandbox Demo login page, that's what you should use because you have less work to do.

http://demo.sonata-project.org/admin/login

Hope I've been clear on this.

Cassiano Tartari

unread,
Feb 1, 2013, 5:38:09 AM2/1/13
to sonata-users
Sorry, I kind of noob yet in Symfony. I really don't get 100% what you said.

Let me try to explain what I know, you can check after if it is correct. Sonata Admin is a backend painel to, usually, administrate a website/blog/... Sonata User is a bundle that extends FOSUser and give to the Sonata Admin a login form (like your link) and user administration (form to register, edit,...).

My intention was to use the Sonata User interface but override the user registration, take off a lot of fields and add some for my system. So, studying the code I saw that I could extend FOSUser class instead of BaseUser because I don't want to create a lot of fields inside my database. But the idea is keep using the Sonata User interface.

What I need? Make a internal system to a company that has at least 3 kind of users (I studying hard on this but still stuck) and studying all php frameworks, symfony was the one that appeared to be the best.

If you install the sonata-sandbox you gonna see that the config.yml file has a lot of file imports, sorry if in the beginning I didn't put all the code, I can do it if somebody want.

And thanks for the attention.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Cassiano Tartari

unread,
Feb 4, 2013, 12:24:32 PM2/4/13
to sonata...@googlegroups.com
Hi!

One more doubt!

I want to display the group option in the form like a radio button, I got it with this:
                    ->add('groups', 'entity', array(
                                'class' => 'ApplicationSonataUserBundle:Group',
                                'property' => 'name',
                                'expanded' => true,
                                'query_builder' => function(EntityRepository $er) {
                                    return $er->createQueryBuilder('g')
                                            ->orderBy('g.name', 'ASC');
                                },
                            ))

But in the moment that I submit the form a exception is thrown saying to write the setGroups()I want to know how to set this many to many field...

Thanks!

Nelson Jesus Suniaga Romero

unread,
Feb 4, 2013, 4:35:40 PM2/4/13
to sonata...@googlegroups.com
Hello

You should have a method setGroups() in your User class, like this:

    /**
     * Sets the user groups
     *
     * @param array $groups
     */
    public function setGroups($groups)
    {
        foreach ($groups as $group) {
            $this->addGroup($group);
        }
    }


I'm wondering why it's missing in that class

Cassiano Tartari

unread,
Feb 5, 2013, 5:30:43 AM2/5/13
to sonata-users
Thanks Nelson!

But seems that is not working. When I do the registration and go check it out on the list, the group user wasn't filled...

My Group class is like is:

<?php

namespace Application\Sonata\UserBundle\
Entity;

use FOS\UserBundle\Entity\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="grupos")
 */
class Group extends BaseGroup {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="
AUTO")
     */
    protected $id;

    public function __toString() {
        return $this->name;
    }


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}


In the form is calling the field like I wrote before. And the addGroup() function is like yours in my User entity.

Nelson Jesus Suniaga Romero

unread,
Feb 5, 2013, 3:48:15 PM2/5/13
to sonata...@googlegroups.com
Try changing entity by sonata_type_model in the configureFormFields method and let me know if it works.

By the way, what version of Symfony are you using?. You should put it in your email signature, along the PHP, Apache and Sonata bundles versions. It really helps everybody to help you out.

Nelson Jesus Suniaga Romero

unread,
Feb 5, 2013, 4:11:33 PM2/5/13
to sonata...@googlegroups.com
Take a look of the original UserAdmin father model class:

vendor/sonata-project/user-bundle/Sonata/UserBundle/Admin/Model/UserAdmin.php

Do the same with the GroupAdmin class.

vendor/sonata-project/user-bundle/Sonata/UserBundle/Admin/Model/GroupAdmin.php

Cassiano Tartari

unread,
Feb 6, 2013, 5:35:08 AM2/6/13
to sonata-users
Nelson,

I know this, when I started to override the UserAdmin class I copied all the code from these sources. But what I really want, like I said before, is change the field type to a radio button, or something that I can set just one option. Using sonata_type_model the field turns into a list of check boxes that you can make multiple choices.

And using simple gmail I can't make multiple signatures for the same e-mail address, but I will try to put always my versions.

I using Symfony 2.1.7, PHP 5.4.6, Apache/2.2.22, and all the bundles in dev-master version.

I hope that after I can run this inside a windows server with IIS and SQL Server 2012.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


Nelson Suniaga

unread,
Feb 6, 2013, 5:49:40 PM2/6/13
to sonata...@googlegroups.com
Change the checkboxes for radio buttons seems like an easy job, it's just an HTML element change, but definitely it's not!. Adding options to the add() method only affects the UL element that contains the many INPUT elements. Another alternative would be change the widget type to choice_list, but then it complains about the array to integer casting. Take a look on this http://symfony.com/doc/current/reference/forms/types/choice.html

By the way, why restrict to just one the number of groups associated to an user?. Maybe you should change the User - Group relationship to many-to-one instead of many-to-many (it will take longer, though).
Reply all
Reply to author
Forward
0 new messages