Symfony 4.4 - 如何将 UserInterface 作为参数传递给encodePassword 函数

问题描述 投票:0回答:1

我想知道如何将 UserInterface 作为参数传递给encodePassword 函数。实际上,当运行以下命令行

php bin/console doctrine:fixtures:load
时,我收到此消息错误:

传递给 Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword() 的参数 1 必须是 Symfony\Component\Security\Core\User\UserInterface 的实例,给定的 App\Entity\User 的实例,

这些是我的相关文件:

用户.php

    <?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @UniqueEntity(fields="email", message="Un utilisateur existe déjà avec cet email.")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $nom;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $prenom;

    /**
     * @ORM\Column(type="text")
     */
    private $roles;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $password;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    public function getPrenom(): ?string
    {
        return $this->prenom;
    }

    public function setPrenom(string $prenom): self
    {
        $this->prenom = $prenom;

        return $this;
    }

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

    public function setRoles(string $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

//    public function getRoles(){
//        return [
//            'ROLE_ADMIN'
//        ];
//    }

    public function getSalt() {}
    public function eraseCredentials() {}

//    Some added code
    public function serialize(){
        return $this->serialize([
            $this->id,
            $this->email,
            $this->password
        ]);
    }

    public function unserialize($string){
        list(
            $this->id,
            $this->email,
            $this->password
        ) = unserialize($string, ['allowed_classes' => false]);
    }
}

UserFixture.php

    <?php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserFixture extends Fixture
{
    private $encoder;
    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    public function load(ObjectManager $manager): void
    {
        $user = new User();


        $user->getNom('test');
        $user->setPrenom('test');
        $user->setRoles('["ROLE_ADMIN"]');
        $user->setEmail('[email protected]');
        $user->setPassword(
            $this->encoder->encodePassword($user,'test')
        );

        $manager->persist($user);
        $manager->flush();
    }
}

用户界面.php

    <?php
    
    /*
     * This file is part of the Symfony package.
     *
     * (c) Fabien Potencier <[email protected]>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    namespace Symfony\Component\Security\Core\User;
    
    use Symfony\Component\Security\Core\Role\Role;
    
    /**
     * Represents the interface that all user classes must implement.
     *
     * This interface is useful because the authentication layer can deal with
     * the object through its lifecycle, using the object to get the encoded
     * password (for checking against a submitted password), assigning roles
     * and so on.
     *
     * Regardless of how your users are loaded or where they come from (a database,
     * configuration, web service, etc.), you will have a class that implements
     * this interface. Objects that implement this interface are created and
     * loaded by different objects that implement UserProviderInterface.
     *
     * @see UserProviderInterface
     *
     * @author Fabien Potencier <[email protected]>
     */
    interface UserInterface
    {
        /**
         * Returns the roles granted to the user.
         *
         *     public function getRoles()
         *     {
         *         return ['ROLE_USER'];
         *     }
         *
         * Alternatively, the roles might be stored in a ``roles`` property,
         * and populated in any number of different ways when the user object
         * is created.
         *
         * @return array<Role|string> The user roles
         */
        public function getRoles();
    
        /**
         * Returns the password used to authenticate the user.
         *
         * This should be the encoded password. On authentication, a plain-text
         * password will be salted, encoded, and then compared to this value.
         *
         * @return string|null The encoded password if any
         */
        public function getPassword();
    
        /**
         * Returns the salt that was originally used to encode the password.
         *
         * This can return null if the password was not encoded using a salt.
         *
         * @return string|null The salt
         */
        public function getSalt();
    
        /**
         * Returns the username used to authenticate the user.
         *
         * @return string The username
         */
        public function getUsername();
    
        /**
         * Removes sensitive data from the user.
         *
         * This is important if, at any given point, sensitive information like
         * the plain-text password is stored on this object.
         */
        public function eraseCredentials();
    }

UserPasswordEncoderInterface.php

    <?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Encoder;

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

/**
 * UserPasswordEncoderInterface is the interface for the password encoder service.
 *
 * @author Ariel Ferrandini <[email protected]>
 *
 * @method bool needsRehash(UserInterface $user)
 */
interface UserPasswordEncoderInterface
{
    /**
     * Encodes the plain password.
     *
     * @param string $plainPassword The password to encode
     *
     * @return string The encoded password
     */
    public function encodePassword(UserInterface $user, $plainPassword);

    /**
     * @param string $raw A raw password
     *
     * @return bool true if the password is valid, false otherwise
     */
    public function isPasswordValid(UserInterface $user, $raw);
}

那么,我的代码有什么问题以及如何修复它。有什么想法吗?

php authentication parameters symfony4 encoder
1个回答
-1
投票

在您的 user.php 文件中将其更新为:

class User implements UserInterface
{...
© www.soinside.com 2019 - 2024. All rights reserved.