Symfony2 - Security/Custom User Class

Asked

Viewed 59 times

1

I’m learning how to work a little with Symfony2 and a certain doubt has arisen. Following the Symfony document itself, it teaches you to create a simple login form, where it implements the interface UserInterface, with the fields $username and $password and some others.

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

1 answer

0


There is no problem in having the name of the desired properties, as long as you return correctly what the interface implemented requires.

An interface in PHP "force" that the class has the methods that are set in it.

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

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

Recommended readings:

Example taken from the Symfony:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.