Symfony2中如何在延伸BaseUser用户类实现AdvancedUserInterface?(

2019-10-18 14:30发布

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

我被告知,为了让使用的用户锁定功能,我的用户类需要实现的AdvancedUserInterface,但似乎我不能这样做既EXTENDS,实现对用户类?

<?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
{

通过这种方法我没有得到任何错误消息,但我也不让使用的用于检查用户锁定的功能,因此看来,没有任何反应。

如果我切换起来像这样,

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

Answer 1:

好吧,我解决它通过这样做:

添加到用户实体我自己的函数来获得锁定状态(即我没有定义一个变量,它已经在我从扩展用户类

//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我把这个:

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


Answer 2:

其实,你没有创造任何东西。 只需拨打用户> isLocked():)这是在BaseUser类FOSUserBundle已经实现;)



文章来源: symfony2 how to implement AdvancedUserInterface on User class that extends BaseUser?