Symfony2 - Security / Custom user class

1

I'm learning to tinker with Symfony2 and some doubt has surfaced. Following the Symfony document itself, it teaches you to create a simple login form, where it implements the UserInterface interface, with the $username and $password fields and a few others.

If I wanted to create this different user class, with names $usuario and $senha , what do I need to know?

    
asked by anonymous 03.02.2016 / 17:12

1 answer

0

There's no problem getting the name of the desired properties, as long as you correctly return what the interface implemented requires.

An interface in PHP "forces" the class to have the methods that are determined on it.

So, what you use the Symfony\Component\Security\Core\User\UserInterface interface, you have to return $senha to method getPassword and usuario to getUsername method.

There are also other methods that need to be implemented from this interface

Recommended readings:

Example taken from Symfony page:

namespace AppBundle\Security\User;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;

class WebserviceUser implements UserInterface, EquatableInterface
{
    private $usuario;
    private $senha;
    private $salt;
    private $roles;

    public function __construct($usuario, $senha, $salt, array $roles)
    {
        $this->usuario = $usuario;
        $this->senha = $senha;
        $this->salt = $salt;
        $this->roles = $roles;
    }

    public function getRoles()
    {
        return $this->roles;
    }

    public function getPassword()
    {
        return $this->senha;
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function getUsername()
    {
        return $this->usuario;
    }

    public function eraseCredentials()
    {
    }

    public function isEqualTo(UserInterface $user)
    {
        if (!$user instanceof WebserviceUser) {
            return false;
        }

        if ($this->senha !== $user->getPassword()) {
            return false;
        }

        if ($this->salt !== $user->getSalt()) {
            return false;
        }

        if ($this->usuario !== $user->getUsername()) {
            return false;
        }

        return true;
    }
}
    
03.02.2016 / 19:56