gpt4 book ai didi

php - getRole() 表单 RoleInterface 在 symfony 上返回空

转载 作者:搜寻专家 更新时间:2023-10-31 21:51:43 25 4
gpt4 key购买 nike

我在 symfony 上实现 JWT token 时遇到了很大的问题。我已经使用了 JWT token ,但我也需要将用户角色添加到 token 信息中。我正在使用监听器 (JWTCreatedListener) 执行此操作:

public function onJWTCreated(JWTCreatedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();

$payload = $event->getData();
$payload['ip'] = $request->getClientIp();
$payload['roles'] = $event->getUser()->getRoles();

$event->setData($payload);
}

我以这种方式实现了 Role.php (AppBundle/Entity/Role.php):

<?php

namespace AppBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="acme_role")
* @ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

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

/**
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;

/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;

public function __construct()
{
$this->users = new ArrayCollection();
}

/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}

// ... getters and setters for each property

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

/**
* Set name
*
* @param string $name
*
* @return Role
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set role
*
* @param string $role
*
* @return Role
*/
public function setRole($role)
{
$this->role = $role;

return $this;
}

/**
* Add user
*
* @param \AppBundle\Entity\User $user
*
* @return Role
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->users[] = $user;

return $this;
}

/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->users->removeElement($user);
}

/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}

还有我的用户类:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Table(name="users")
* @ORM\Entity
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;

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

/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;

/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;

public function __construct($username)
{
$this->isActive = true;
$this->username = $username;
$this->roles = new ArrayCollection();
}

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

public function getSalt()
{
return null;
}

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

public function setPassword($password)
{
$this->password = $password;
}

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

public function eraseCredentials()
{
}

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

/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;

return $this;
}

/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;

return $this;
}

/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}

/**
* Add role
*
* @param \AppBundle\Entity\Role $role
*
* @return User
*/
public function addRole(\AppBundle\Entity\Role $role)
{
$this->roles[] = $role;

return $this;
}

/**
* Remove role
*
* @param \AppBundle\Entity\Role $role
*/
public function removeRole(\AppBundle\Entity\Role $role)
{
$this->roles->removeElement($role);
}

public function isAccountNonExpired()
{
return true;
}

public function isAccountNonLocked()
{
return true;
}

public function isCredentialsNonExpired()
{
return true;
}

public function isEnabled()
{
return $this->isActive;
}

// serialize and unserialize must be updated - see below
public function serialize()
{
return serialize(array(
// ...
$this->isActive
));
}
public function unserialize($serialized)
{
list (
// ...
$this->isActive
) = unserialize($serialized);
}
}

问题是这个方法 getRole() 总是返回空。

这是我的数据库数据:

[users]
id username password is_active
1 abriceno $2y$13$NW6uNOKJGUQTSXirej4HKOwIa6mWzYqFxzz1ppWQjyp... 1

[acme_role]
id name role
1 admin ROLE_ADMIN

[user_role]
user_id user_role
1 1

此外,我尝试使用 doctrine 从 Controller 测试中调用数据:

public function indexAction(Request $request)
{

$repository = $this->getDoctrine()->getRepository('AppBundle:User');
$user = $repository->findOneByusername('abriceno');


$username = $user->getUsername();
$roles = $user->getRoles();

$arr = array(
'username' => $user->getUsername(),
'password' => $user->getPassword(),
'roles' => $user->getRoles()
);
return new JsonResponse($arr);

返回:

{"username":"abriceno","password":"$2y$13$NW6uNOKJGUQTSXirej4HKOwIa6mWzYqFxzz1ppWQjypQJLIgUGJ.m","roles":[{}]}

我很绝望...感谢您能为我提供的所有帮助。

更新 1

如果我执行 print_r($role) 这将打印一个 huuuuuge 值列表:

array(1) { [0]=> object(AppBundle\Entity\Role)#624 (4) { ["id":"AppBundle\Entity\Role":private]=> int(1) ["name":"AppBundle\Entity\Role":private]=> string(5) "admin" ["role":"AppBundle\Entity\Role":private]=> string(10) "ROLE_ADMIN" ["users":"AppBundle\Entity\Role":private]=> object(Doctrine\ORM\PersistentCollection)#626 (9) { ["snapshot":"Doctrine\ORM\PersistentCollection":private]=> array(0) { } ["owner":"Doctrine\ORM\PersistentCollection":private]=> *RECURSION* 

...并继续...非常奇怪!

最佳答案

似乎连接表不直接由 doctrine 处理,因此您需要指定 doctrine 如何使用 JoinTable 注释映射引用的字段(参见 default mapping 部分。)

在您的情况下,尝试修改 User 实体中的 roles 关系 定义,如下所示:

/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_role", referencedColumnName="id")})
*
*/
private $roles;

希望对你有帮助

关于php - getRole() 表单 RoleInterface 在 symfony 上返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40719748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com