1

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

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

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

2 回答 2

0

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

将我自己的函数添加到用户实体以获取锁定状态(我没有定义的变量,它已经在我要扩展的用户类中

//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);
    }
于 2013-09-12T12:55:55.057 回答
0

实际上,您不必创造任何东西。只需调用 user->isLocked() :) 它已经在 FOSUserBundle 的 BaseUser 类中实现;)

于 2015-09-09T17:52:38.263 回答