问题
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