symfony2 how to implement AdvancedUserInterface on User class that extends BaseUser?

為{幸葍}努か 提交于 2019-12-11 06:29:41

问题


I have a custom User class that extends the BaseUser.

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.

If I switch them up like this,

class User implements AdvancedUserInterface extends BaseUser 

I get the following error message:

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

回答1:


OK, I solved it by doing this:

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

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



回答2:


Actually, You don't have to create anything. Just call user->isLocked() :) It was already implemented in the BaseUser class of FOSUserBundle ;)



来源:https://stackoverflow.com/questions/18672424/symfony2-how-to-implement-advanceduserinterface-on-user-class-that-extends-baseu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!