错误邮递员符号

时间:2018-07-16 13:31:42

标签: php sql symfony postman

当我尝试使用以下命令在/ users上发布时,出现此错误: {     “ username”:“ Ausername123”,     “ password”:“ Apassword123”,     “ retyped_pa​​ssword”:“ Apassword123” }

“类型错误:传递给Symfony \ Component \ Security \ Core \ Encoder \ UserPasswordEncoder :: encodePassword()的参数1必须是Symfony \ Component \ Security \ Core \ User \ UserInterface的实例,AppBundle \ Entity \的实例给定的用户,在第160行的/home/rluis/Stage_Tek2/Boojon/src/AppBundle/Controller/UsersController.php中被调用

我的UserController:

<?php
/**
* Created by PhpStorm.
* User: rluis
* Date: 7/11/18
* Time: 2:08 PM
*/

namespace AppBundle\Controller;

use AppBundle\Entity\EntityMerger;
use AppBundle\Entity\User;
use AppBundle\Exception\ResourceValidationException;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use FOS\RestBundle\Controller\Annotations as Rest;

/**
* @Security("is_anonymous() or is_authenticated()")
*/
class UsersController extends AbstractController
{
/**
 * @var UserPasswordEncoderInterface
 */
private $passwordEncoder;

/**
 * @var JWTEncoderInterface
 */
private $jwtEncoder;

/**
 * @var EntityMerger
 */
private $entityMerger;

/**
 * UsersController constructor.
 * @param UserPasswordEncoderInterface $passwordEncoder
 * @param JWTEncoderInterface $jwtEncoder
 * @param EntityMerger $entityMerger
 */
public function __construct(UserPasswordEncoderInterface $passwordEncoder, JWTEncoderInterface $jwtEncoder, EntityMerger $entityMerger)
{
    $this->passwordEncoder = $passwordEncoder;
    $this->jwtEncoder = $jwtEncoder;
    $this->entityMerger = $entityMerger;
}

/**
 * @Rest\View(serializerGroups={"user_detail"})
 * @Security("is_granted('show', theUser)", message="Access denied")
 */
public function getUserAction(?User $theUser)
{
    if (null === $theUser) {
        throw new NotFoundHttpException();
    }

    return $theUser;
}

/**
 *
 * @Rest\Post(
 *     path = "/users",
 *     name = "users_add"
 * )
 * @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
 * @ParamConverter(
 *     "user",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}}
 * )
 */
public function postUserAction(User $user, ConstraintViolationListInterface $violations)
{
    if (count($violations) > 0) {
        $message = 'The user is not valid: ';
        foreach ($violations as $violation) {
            $message .= sprintf(
                "Field %s: %s ",
                $violation->getPropertyPath(),
                $violation->getMessage()
            );
        }

        throw new ResourceValidationException($message);
    }

    $this->encodePassword($user);
    $user->setRoles([User::ROLE_USER]);

    $this->persistUser($user);

    return $user;
}

/**
 * @Rest\View(serializerGroups={"user_detail"})
 * @Rest\Patch(
 *     path = "/users/{theUser}",
 *     name= "patch_user"
 * )
 * @ParamConverter(
 *     "modifiedUser",
 *     converter="fos_rest.request_body",
 *     options={
 *      "validator"={"groups"={"Patch"}},
 *      "deserializationContext"={"groups"={"Deserialize"}}
 *     }
 * )
 * @Security("is_granted('edit', theUser)", message="Access Denied")
 */
public function patchUserAction(?User $theUser, User $modifiedUser, ConstraintViolationListInterface $violations)
{
    if (null === $theUser) {
        throw new NotFoundHttpException();
    }

    if (empty($modifiedUser->getPassword())) {
        $modifiedUser->setPassword(null);
    }

    if (count($violations) > 0) {
        $message = 'The user is not valid: ';
        foreach ($violations as $violation) {
            $message .= sprintf(
                "Field %s: %s",
                $violation->getPropertyPath(),
                $violation->getMessage()
            );
        }
        throw new ResourceValidationException($message);
    }

    $this->entityMerger->merge($theUser, $modifiedUser);

    $this->encodePassword($theUser);
    $this->persistUser($theUser);

    return $theUser;
}

/**
 * @param User $user
 */
protected function encodePassword(User $user)
{
    $user->setPassword(
        $this->passwordEncoder->encodePassword(
            $user,
            $user->getPassword()
        )
    );
}

/**
 * @param User $user
 */
protected function persistUser(User $user)
{
    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();
}
}

我的用户实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
* User
*
* @ORM\Table(name="user", uniqueConstraints=
{@ORM\UniqueConstraint(name="user_id_uindex", columns={"id"}), 
@ORM\UniqueConstraint(name="user_username_uindex", columns=
{"username"})}, indexes={@ORM\Index(name="user_profile_id_fk", 
columns={"id_profile"}), @ORM\Index(name="user_localisation_id_fk", 
columns={"id_location"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity("username", groups={"Default", "Patch"})
*/

class User
{
const ROLE_USER = 'ROLE_USER';
const ROLE_ADMIN = 'ROLE_ADMIN';

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @Serializer\Groups({"Default", "Deserialize", "user_detail"})
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=32, nullable=false)
 * @Serializer\Groups({"Default", "Deserialize", "user_detail"})
 */
private $username;

/**
 * @var string
 * @Serializer\Type("string")
 * @Assert\NotBlank(groups={"Default"})
 * @Assert\Expression(
 *     "this.getPassword() === this.getRetypedPassword()",
 *     message="Password does not match",
 *     groups={"Default", "Patch"}
 * )
 * @Assert\NotBlank(groups={"Default"})
 * @Serializer\Groups({"Deserialize"})
 */
private $retypedPassword;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=32, nullable=false)
 * @Serializer\Groups({"Deserialize", "user_detail"})
 * @Assert\Regex(
 *     pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
 *     message="Password must be seven characters long and contain at least one digit code, upper case, and lower case letter!",
 *     groups={"Default", "Patch"}
 * )
 */
private $password;

/**
 * @var boolean
 *
 * @ORM\Column(name="enabled", type="boolean", nullable=false)
 */
private $enabled = '1';

/**
 * @var \AppBundle\Entity\Localisation
 *
 * @ORM\ManyToOne(targetEntity="Localisation")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_location", referencedColumnName="id")
 * })
 */
private $idLocation;

/**
 * @var \AppBundle\Entity\Profile
 *
 * @ORM\ManyToOne(targetEntity="Profile")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_profile", referencedColumnName="id")
 * })
 */
private $idProfile;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="User", inversedBy="idUserOne")
 * @ORM\JoinTable(name="friend",
 *   joinColumns={
 *     @ORM\JoinColumn(name="id_user_one", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="id_user_two", referencedColumnName="id")
 *   }
 * )
 */
private $idUserTwo;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserInvited")
 * @ORM\JoinTable(name="list_invited",
 *   joinColumns={
 *     @ORM\JoinColumn(name="id_user_invited", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="id_place_invited", referencedColumnName="id")
 *   }
 * )
 */
private $idPlaceInvited;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Place", mappedBy="idUserPresent")
 */
private $idPlacePresent;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserPlace")
 * @ORM\JoinTable(name="user_place",
 *   joinColumns={
 *     @ORM\JoinColumn(name="id_user_place", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="id_place_place", referencedColumnName="id")
 *   }
 * )
 */
private $idPlacePlace;

/**
 * Constructor
 */
public function __construct()
{
    $this->idUserTwo = new \Doctrine\Common\Collections\ArrayCollection();
    $this->idPlaceInvited = new \Doctrine\Common\Collections\ArrayCollection();
    $this->idPlacePresent = new \Doctrine\Common\Collections\ArrayCollection();
    $this->idPlacePlace = new \Doctrine\Common\Collections\ArrayCollection();
}

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

/**
 * @param int $id
 */
public function setId(int $id): void
{
    $this->id = $id;
}

/**
 * @return string
 */
public function getUsername(): string
{
    return $this->username;
}

/**
 * @param string $username
 */
public function setUsername(string $username): void
{
    $this->username = $username;
}

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

/**
 * @param string $password
 */
public function setPassword(string $password): void
{
    $this->password = $password;
}

/**
 * @return bool
 */
public function isEnabled(): bool
{
    return $this->enabled;
}

/**
 * @param bool $enabled
 */
public function setEnabled(bool $enabled): void
{
    $this->enabled = $enabled;
}

/**
 * @return mixed
 */
public function getIdLocation()
{
    return $this->idLocation;
}

/**
 * @param mixed $idLocation
 */
public function setIdLocation($idLocation): void
{
    $this->idLocation = $idLocation;
}

/**
 * @return mixed
 */
public function getIdProfile()
{
    return $this->idProfile;
}

/**
 * @param mixed $idProfile
 */
public function setIdProfile($idProfile): void
{
    $this->idProfile = $idProfile;
}

/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getIdUserTwo(): \Doctrine\Common\Collections\Collection
{
    return $this->idUserTwo;
}

/**
 * @param \Doctrine\Common\Collections\Collection $idUserTwo
 */
public function setIdUserTwo(\Doctrine\Common\Collections\Collection $idUserTwo): void
{
    $this->idUserTwo = $idUserTwo;
}

/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getIdPlaceInvited(): \Doctrine\Common\Collections\Collection
{
    return $this->idPlaceInvited;
}

/**
 * @param \Doctrine\Common\Collections\Collection $idPlaceInvited
 */
public function setIdPlaceInvited(\Doctrine\Common\Collections\Collection $idPlaceInvited): void
{
    $this->idPlaceInvited = $idPlaceInvited;
}

/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getIdPlacePresent(): \Doctrine\Common\Collections\Collection
{
    return $this->idPlacePresent;
}

/**
 * @param \Doctrine\Common\Collections\Collection $idPlacePresent
 */
public function setIdPlacePresent(\Doctrine\Common\Collections\Collection $idPlacePresent): void
{
    $this->idPlacePresent = $idPlacePresent;
}

/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getIdPlacePlace(): \Doctrine\Common\Collections\Collection
{
    return $this->idPlacePlace;
}

/**
 * @param \Doctrine\Common\Collections\Collection $idPlacePlace
 */
public function setIdPlacePlace(\Doctrine\Common\Collections\Collection $idPlacePlace): void
{
    $this->idPlacePlace = $idPlacePlace;
}

/**
 * @return string
 */
public function getRetypedPassword(): string
{
    return $this->retypedPassword;
}

/**
 * @param string $retypedPassword
 */
public function setRetypedPassword(string $retypedPassword): void
{
    $this->retypedPassword = $retypedPassword;
}

}

1 个答案:

答案 0 :(得分:0)

您的User实体必须实现Symfony\Component\Security\Core\User\UserInterface以及该接口中定义的所有必要方法:

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

class User implements UserInterface {
    /* implement missing methods */
}