FOSUserBundle extend propel User

拜拜、爱过 提交于 2019-12-08 05:45:24

问题


I'm using FOSUserBundle and Propel in a Symfony 2.2 project. I'm trying to extend the User class and add a new method to it like so:

namespace Acme\UserBundle\Model;

use FOS\UserBundle\Propel\User as BaseUser;

class User extends BaseUser
{
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    public function hasPermission($topic) {
        // TODO check if $topic has permission
        return TRUE;
    }
}

The problem is that when calling $this->getUser() in a controller, the class of the object returned is FOS\UserBundle\Propel\User, so hasPermission() is undefined.

I tried throwing an exception in the constructor of the custom class and it seemed to be used when registering a new user. But I guess it is not saved as an Acme\UserBundle\Model\User.

I tried this with Doctrine in another project and it returned the correct class when calling $this->getUser(). Am i doing something wrong? How do i make it work with Propel?

config.yml:

fos_user:
    db_driver: propel
    firewall_name: main
    user_class: Acme\UserBundle\Model\User

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

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                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 }

回答1:


I installed GlorpenPropelBundle, added a couple of lines to config.yml.

config.yml

propel:
    classname: Glorpen\Propel\PropelBundle\Connection\EventPropelPDO
    build_properties:
        propel.behavior.event.class:    'vendor.glorpen.propel-bundle.Glorpen.Propel.PropelBundle.Behaviors.EventBehavior'
        propel.behavior.extend.class:   'vendor.glorpen.propel-bundle.Glorpen.Propel.PropelBundle.Behaviors.ExtendBehavior'
        propel.behavior.default:        "event, extend"

glorpen_propel:
    extended_models:
        FOS\UserBundle\Propel\User: Acme\UserBundle\Model\User

$this->getUser() now returns Acme\UserBundle\Model\User.



来源:https://stackoverflow.com/questions/16585957/fosuserbundle-extend-propel-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!