sonata-project / SonataUserBundle

Symfony SonataUserBundle
https://docs.sonata-project.org/projects/SonataUserBundle
MIT License
339 stars 488 forks source link

Refering to "object" in custom template action pings back to BaseUser #1238

Closed kyeno closed 3 years ago

kyeno commented 3 years ago

Sonata packages

sonata-project/admin-bundle              3.76.0          3.76.0             The missing Symfony Admin Generator
sonata-project/block-bundle              3.21.0          4.3.0              Symfony SonataBlockBundle
sonata-project/cache                     2.0.1           2.0.1              Cache library
sonata-project/classification-bundle     3.13.2          3.13.2             Symfony SonataClassificationBundle
sonata-project/datagrid-bundle           2.5.0           3.2.0              Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.9.1           1.9.1              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.23.0          3.23.0             Symfony Sonata / Integrate Doctrine ORM into the Sona...
sonata-project/exporter                  2.3.0           2.3.0              Lightweight Exporter library
sonata-project/form-extensions           1.6.0           1.6.0              Symfony form extensions
sonata-project/formatter-bundle          4.3.0           4.3.0              Symfony SonataFormatterBundle
sonata-project/intl-bundle               2.9.0           2.9.0              Symfony SonataIntlBundle
sonata-project/media-bundle              3.27.0          3.27.0             Symfony SonataMediaBundle
sonata-project/news-bundle               3.14.0          3.14.0             Symfony SonataNewsBundle
sonata-project/translation-bundle        2.6.0           2.6.0              SonataTranslationBundle
sonata-project/twig-extensions           1.4.1           1.4.1              Sonata twig extensions
sonata-project/user-bundle               4.x-dev cb06686 dev-master 0cbbe31 Symfony SonataUserBundle

Symfony packages

symfony/asset                      v4.4.13 v4.4.13 Symfony Asset Component
symfony/browser-kit                v4.4.13 v4.4.13 Symfony BrowserKit Component
symfony/cache                      v4.4.13 v4.4.13 Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.2.0  v2.2.0  Generic abstractions related to caching
symfony/config                     v4.4.13 v4.4.13 Symfony Config Component
symfony/console                    v4.4.13 v4.4.13 Symfony Console Component
symfony/css-selector               v4.4.13 v4.4.13 Symfony CssSelector Component
symfony/debug                      v4.4.13 v4.4.13 Symfony Debug Component
symfony/debug-bundle               v4.4.13 v4.4.13 Symfony DebugBundle
symfony/debug-pack                 v1.0.8  v1.0.8  A debug pack for Symfony projects
symfony/dependency-injection       v4.4.13 v4.4.13 Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.4.13 v4.4.13 Symfony Doctrine Bridge
symfony/dom-crawler                v4.4.13 v4.4.13 Symfony DomCrawler Component
symfony/dotenv                     v4.4.13 v4.4.13 Registers environment variables from a .env file
symfony/error-handler              v4.4.13 v4.4.13 Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.13 v4.4.13 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9  v2.2.0  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.13 v4.4.13 Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.13 v4.4.13 Symfony Filesystem Component
symfony/finder                     v4.4.13 v4.4.13 Symfony Finder Component
symfony/flex                       v1.9.4  v1.9.4  Composer plugin for Symfony
symfony/form                       v4.4.13 v4.4.13 Symfony Form Component
symfony/framework-bundle           v4.4.13 v4.4.13 Symfony FrameworkBundle
symfony/http-client                v4.4.13 v4.4.13 Symfony HttpClient component
symfony/http-client-contracts      v2.2.0  v2.2.0  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.4.13 v4.4.13 Symfony HttpFoundation Component
symfony/http-kernel                v4.4.13 v4.4.13 Symfony HttpKernel Component
symfony/inflector                  v4.4.13 v4.4.13 Symfony Inflector Component
symfony/intl                       v4.4.13 v4.4.13 A PHP replacement layer for the C intl extension that includes additional data...
symfony/mailer                     v4.4.13 v4.4.13 Symfony Mailer Component
symfony/maker-bundle               v1.21.1 v1.21.1 Symfony Maker helps you create empty commands, controllers, form classes, test...
symfony/mime                       v4.4.13 v4.4.13 A library to manipulate MIME messages
symfony/monolog-bridge             v4.4.13 v4.4.13 Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.5.0  Symfony MonologBundle
symfony/options-resolver           v4.4.13 v4.4.13 Symfony OptionsResolver Component
symfony/orm-pack                   v2.0.0  v2.0.0  A pack for the Doctrine ORM
symfony/phpunit-bridge             v5.1.5  v5.1.5  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme     v1.18.1 v1.18.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.18.1 v1.18.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.18.1 v1.18.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.18.1 v1.18.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.18.1 v1.18.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                    v4.4.13 v4.4.13 Symfony Process Component
symfony/profiler-pack              v1.0.5  v1.0.5  A pack for the Symfony web profiler
symfony/property-access            v4.4.13 v4.4.13 Symfony PropertyAccess Component
symfony/property-info              v4.4.13 v4.4.13 Symfony Property Info Component
symfony/routing                    v4.4.13 v4.4.13 Symfony Routing Component
symfony/security-acl               v3.1.0  v3.1.0  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.13 v4.4.13 Symfony SecurityBundle
symfony/security-core              v4.4.13 v4.4.13 Symfony Security Component - Core Library
symfony/security-csrf              v4.4.13 v4.4.13 Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.13 v4.4.13 Symfony Security Component - Guard
symfony/security-http              v4.4.13 v4.4.13 Symfony Security Component - HTTP Integration
symfony/serializer                 v4.4.13 v4.4.13 Symfony Serializer Component
symfony/serializer-pack            v1.0.3  v1.0.3  A pack for the Symfony serializer
symfony/service-contracts          v2.2.0  v2.2.0  Generic abstractions related to writing services
symfony/stopwatch                  v4.4.13 v4.4.13 Symfony Stopwatch Component
symfony/string                     v5.1.5  v5.1.5  Symfony String component
symfony/swiftmailer-bundle         v3.4.0  v3.4.0  Symfony SwiftmailerBundle
symfony/templating                 v4.4.13 v4.4.13 Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.4.13 v4.4.13 Symfony Translation Component
symfony/translation-contracts      v2.2.0  v2.2.0  Generic abstractions related to translation
symfony/twig-bridge                v4.4.13 v4.4.13 Symfony Twig Bridge
symfony/twig-bundle                v4.4.13 v4.4.13 Symfony TwigBundle
symfony/twig-pack                  v1.0.0  v1.0.0  A Twig pack for Symfony projects
symfony/validator                  v4.4.13 v4.4.13 Symfony Validator Component
symfony/var-dumper                 v4.4.13 v4.4.13 Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.13 v4.4.13 A blend of var_export() + serialize() to turn any serializable data structure ...
symfony/web-link                   v4.4.13 v4.4.13 Symfony WebLink Component
symfony/web-profiler-bundle        v4.4.13 v4.4.13 Symfony WebProfilerBundle
symfony/webpack-encore-bundle      v1.7.3  v1.7.3  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       v4.4.13 v4.4.13 Symfony Yaml Component

PHP version

PHP 7.4.6 (cli) (built: Jun  1 2020 16:44:58) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

Subject

When trying to integrate PixSortableBehaviorBundle with SonataUserBundle, so I can freely move users around (they're displayed on a subpage), things crash.

Analyzing deeper, it actually treats the object Entity within custom template as BaseUser, and not as my extended User class.

My sonata_user.yaml config:

sonata_user:
    manager_type:   orm

    class:
        user:   App\Entity\User
        group:  App\Entity\UserGroup

    admin:
        user:
            class:          App\Admin\UserAdmin
            controller:     PixSortableBehaviorBundle:SortableAdmin
            translation:    SonataUserBundle

    impersonating:
        route:  index

parameters:
    sonata.user.admin.user.class:   App\Admin\UserAdmin

Part of my listMapper from my custom App\Admin\UserAdmin.php

        $listMapper
            ->add('_action', null, [

                'actions'   => [

                    // NOTE: This takes BaseUser instead of User as an argument
                    'move'      => [

                        'template'                  => 'Admin/_sort.html.twig',
                        'enable_top_bottom_buttons' => false
                    ],
                    'edit'      => [],
                    'delete'    => []
                ],
                'label'     => 'Actions'
            ])
        ;

Crashing contents of Admin/_sort.html.twig template:

{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
    {% set current_position = currentObjectPosition(object) %}
    {% set last_position = lastPosition(object) %}
...

Steps to reproduce

Enable custom Twig template within _actions array in ListMapper, and refer to object within that template.

Expected results

object being representation of App\Entity\User, not Sonata\UserBundle\Entity\BaseUser

NOTE: It works perfectly on any other, hand-made Entities not extending SonataUserBundle.

Actual results

An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col 13 near 'position) as': Error: Class Sonata\UserBundle\Entity\BaseUser has no field or association named position").

core23 commented 3 years ago

So you added the sortable sniped to the user admin?

What would you expect, a normal user isn't sortable at all.

kyeno commented 3 years ago

Uhhmmm... maybe I forgot to paste the a portion of my App\Entity\User:

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

...cut

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User extends BaseUser
{
... cut
    /**
     * @var int $position
     *
     * Display position on the team page
     * @ORM\Column(name="position", type="integer", length=3)
     * @Gedmo\SortablePosition
     */
    private $position;
... cut
}

It actually doesn't matter if it's sortable here. What does matter is that the template's object refers to BaseUser and not User Entity.

wbloszyk commented 3 years ago

Here is tutorial how to make it step by step (I using it). If you cant reproduce it one to one then we have issue. Otherwise you should be able to modify this code to own usage.

https://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_sortable_listing.html

Also check where is your template: Admin/_sort.html.twig should be in templates/Admin/_sort.html.twig @App/Admin_sort.html.twig should be in src/Resources/views/Admin/_sort.html.twig

kyeno commented 3 years ago

@wbloszyk I have followed that guide precisely. Works like a charm for any other entities except extended User entity, since then that problem appears. My template loads properly, it is in templates/Admin/_sort.html.twig, I just copied and slightly modified the original Pix's sortable one but definitely not in a manner that could break anything (simple HTML modifications; core references to object remained the same).

Give it a try yourself:

  1. Create a Symfony 4.4 project with SonataAdmin, SonataUser (with all deps), Stof's Doctrine Extensions and PixSortable
  2. Create your own User entity by extending Sonata's BaseUser
  3. Add $position to your User entity
  4. Try to enable this template and methods accordingly to the recipe you sent me
wbloszyk commented 3 years ago

@kyeno I checked it. It is not ower issue. You should create it in pixassociates/sortable-behavior-bundle.

Problem is here (after remove it all is OK): https://github.com/pix-digital/pixSortableBehaviorBundle/blob/master/Services/PositionORMHandler.php#L49-L60

kyeno commented 3 years ago

Ha! You're right. Thank you!

VincentLanglet commented 3 years ago

@kyeno I didnt follow everything, but should some doc be improved ?

kyeno commented 3 years ago

@VincentLanglet Nope. In fact it's a bug in another bundle. Sonata is good. 👍