symfony: Argument 4 passed to Symfony\Component\Security\Core\Event\VoteEvent::__construct() must be of the type integer, null given
Symfony version(s) affected: x.y.z 4.2.3 Description
I am following the tutorial and at the point of Voters my code is breaking. I get the following error:
Argument 4 passed to Symfony\Component\Security\Core\Event\VoteEvent::__construct() must be of the type integer, null given, called in /Applications/XAMPP/xamppfiles/htdocs/symfony-01/vendor/symfony/security-core/Authorization/Voter/TraceableVoter.php on line 40
The code is a bit messy now, because I tried to follow documentation and the tutorial (at least one should work, huh?). Changing “role” field type didn’t work. I don’t have idea for that.
How to reproduce
Voter.php
class MicroPostVoter extends Voter
{
const EDIT = 'edit';
const DELETE = 'delete';
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::DELETE, self::EDIT])) {
return false;
}
// only vote on Post objects inside this voter
if (!$subject instanceof MicroPost) {
return false;
}
return true;
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
// return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
// && $subject instanceof \App\Entity\BlogPost;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof User) {
return false;
}
/** @var MicroPost $microPost */
$microPost = $subject;
//... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::DELETE:
// logic to determine if the user can EDIT
// return true or false
return $this->canDelete($microPost, $user);
break;
case self::EDIT:
// logic to determine if the user can VIEW
// return true or false
return $this->canEdit($microPost, $user);
break;
}
return false;
}
private function canEdit(MicroPost $microPost, User $user)
{
return $user === $microPost->getUser();
}
private function canDelete(MicroPost $microPost, User $user)
{
return $user === $microPost->getUser();
}`
Entity/User.php
` class User implements UserInterface, \Serializable { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type=“integer”) */ private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\NotBlank()
* @Assert\Length(min=5, max=50)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @Assert\NotBlank()
* @Assert\Length(min=8)
*/
private $plainPassword;
/**
* @return mixed
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param mixed $plainPassword
*/
public function setPlainPassword($plainPassword): void
{
$this->plainPassword = $plainPassword;
}
/**
* @ORM\Column(type="string", length=190, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(min="4")
*/
private $fullName;
/**
* @ORM\OneToMany(targetEntity="App\Entity\MicroPost", mappedBy="user")
*/
private $microPosts;
public function __construct()
{
$this->microPosts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string)$this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string)$this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFullName(): ?string
{
return $this->fullName;
}
public function setFullName(string $fullName): self
{
$this->fullName = $fullName;
return $this;
}
/**
* String representation of object
*
* @link https://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1.0
*/
public function serialize()
{
return serialize([
$this->id,
$this->username,
$this->password
]);
}
/**
* Constructs the object
*
* @link https://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
* @since 5.1.0
*/
public function unserialize($serialized)
{
list($this->id,
$this->username,
$this->password) = unserialize($serialized);
}
/**
* @return Collection|MicroPost[]
*/
public function getMicroPosts(): Collection
{
return $this->microPosts;
}
public function addMicroPost(MicroPost $microPost): self
{
if (!$this->microPosts->contains($microPost)) {
$this->microPosts[] = $microPost;
$microPost->setUser($this);
}
return $this;
}
public function removeMicroPost(MicroPost $microPost): self
{
if ($this->microPosts->contains($microPost)) {
$this->microPosts->removeElement($microPost);
// set the owning side to null (unless already changed)
if ($microPost->getUser() === $this) {
$microPost->setUser(null);
}
}
return $this;
}
} ` Possible Solution
Additional context
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (5 by maintainers)
@dipaksid Inside the ExampleVoter (a class created early in the course) you need to return something, like:
In future, you can just ask on the Q&A section of the tutorial. Note, I’m an author of this course.