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

查看:90
本文介绍了FOSUserBundle扩展推动用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony 2.2项目中使用FOSUserBundlePropel.我正在尝试扩展User类,并向其添加一个新方法,如下所示:

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

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

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.

我尝试在自定义类的构造函数中引发异常,并且该异常似乎在注册新用户时使用.但是我想它没有保存为Acme\UserBundle\Model\User.

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.

我在另一个项目中用Doctrine进行了尝试,并且在调用$this->getUser()时返回了正确的类.难道我做错了什么?如何与Propel配合使用?

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 }

推荐答案

我安装了 GlorpenPropelBundle ,几行到config.yml.

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()现在返回Acme\UserBundle\Model\User.

这篇关于FOSUserBundle扩展推动用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆