symfony2如何在扩展BaseUser的User类上实现AdvancedUserInterface? [英] symfony2 how to implement AdvancedUserInterface on User class that extends BaseUser?

查看:229
本文介绍了symfony2如何在扩展BaseUser的User类上实现AdvancedUserInterface?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的User类扩展BaseUser。

I have a custom User class that extends the BaseUser.

我被告知,为了使用用户锁功能我的用户类需要实现AdvancedUserInterface,但似乎我不能在User类上同时执行EXTENDS和IMPLEMENTS?

I have been informed that in order to make use of the user lock functions my user class needs to implement the AdvancedUserInterface, but it seems I can't do both EXTENDS and IMPLEMENTS on the User class?

<?php
// src/BizTV/UserBundle/Entity/User.php

namespace BizTV\UserBundle\Entity;

use BizTV\UserBundle\Validator\Constraints as BizTVAssert;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

use BizTV\BackendBundle\Entity\company as company;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser implements AdvancedUserInterface
{

使用这种方法我没有得到任何错误信息,

With this approach I get no error messages, but nor do I get the use of the functions for checking a user lock so it appears that nothing happens.

如果我像这样切换,

class User implements AdvancedUserInterface extends BaseUser 

我收到以下错误消息:

Parse error: syntax error, unexpected T_EXTENDS, expecting '{' in /var/www/cloudsign/src/BizTV/UserBundle/Entity/User.php on line 18


推荐答案

OK,我通过这样做解决了:

OK, I solved it by doing this:

添加到用户实体我自己的函数来获得锁状态t定义,它已经在我从

Add to the User entity my own function to get the Lock status (a variable that I didn't define, it was already in the user class which I am extending from

//Below should be part of base user class but doesn't work so I implement it manually.

/**
 * Get lock status
 *
 * @return boolean 
 */
public function getLocked()
{
    return $this->locked;
}    

在UserChecker中,我输入:

And in the UserChecker I put this:

public function checkPreAuth(UserInterface $user)
{

    //Test for companylock...
    if ( !$user->getCompany()->getActive() ) {
        throw new LockedException('The company of this user is locked.', $user);
    }    

    if ( $user->getLocked() ) {
        throw new LockedException('The admin of this company has locked this user.', $user);
    }

...

/**
 * {@inheritdoc}
 */
public function checkPostAuth(UserInterface $user)
{

    //Test for companylock...
    if ( !$user->getCompany()->getActive() ) {
        throw new LockedException('The company of this user is locked.', $user);
    }    

    if ( $user->getLocked() ) {
        throw new LockedException('The admin of this company has locked this user.', $user);
    }

这篇关于symfony2如何在扩展BaseUser的User类上实现AdvancedUserInterface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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