FOSUserBundle扩展推进用户[英] FOSUserBundle extend propel User

本文是小编为大家收集整理的关于FOSUserBundle扩展推进用户的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在Symfony 2.2项目中使用FOSUserBundle和Propel.我正在尝试扩展User类,并像这样添加新方法:

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

问题是,当在控制器中调用$this->getUser()时,返回的对象的类为FOS\UserBundle\Propel\User,因此hasPermission()是未定义的.

我尝试在自定义类的构造函数上抛出一个例外,并且在注册新用户时似乎使用它.但是我想它不能保存为Acme\UserBundle\Model\User.

我在另一个项目中使用Doctrine尝试了此操作,然后在调用$this->getUser()时返回正确的类.难道我做错了什么?我如何使其与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 }

推荐答案

我安装了 glorpenpropelbundle ,在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()现在返回Acme\UserBundle\Model\User.

本文地址:https://www.itbaoku.cn/post/2090802.html

问题描述

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 }

推荐答案

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.