【问题标题】:Symfony2 - Security Role - From PHP 5.3 to PHP >5.4 - getRole() on a non-objectSymfony2 - 安全角色 - 从 PHP 5.3 到 PHP >5.4 - 非对象上的 getRole()
【发布时间】:2016-11-22 05:59:44
【问题描述】:

我已经看过这个问题了:https://github.com/symfony/symfony/issues/3691

我的问题是我找不到解决方案来让它发挥作用。

我使用 Symfony 2.8.3

这是我遇到的错误:

FatalErrorException in RoleHierarchy.php line 43: Error: Call to a member function getRole() on a non-object

我正确地序列化了所有内容,这是我的课程:

用户

<?php
/**
 */

namespace CNAMTS\PHPK\SecurityBundle\Security\User;

use Symfony\Component\Security\Core\User\UserInterface;
use CNAMTS\PHPK\SecurityBundle\Security\Role;

/**
 * Classe abstraite implémentant la classe Symfony UserInterface.
 */
abstract class User implements UserInterface
{
    public function __construct() {
        $this->roles = array();
    }

    /**
     * Concaténation du nom et du numéro d'agent : NOM-NUMERO.
     *
     * @var String
     */
    protected $ismum;
    /**
     * Nom de l'utilisateur.
     *
     * @var String
     */
    protected $nom;
    /**
     * Prénom de l'utilisateur.
     *
     * @var String
     */
    protected $prenom;
    /**
     * Id de l'utilisateur.
     *
     * @var String
     */
    protected $id;
    /**
     * Numéro d'agent.
     *
     * @var String
     */
    protected $chrono;
    /**
     * Code de l'organisme
     *
     * @var String
     */
    protected $codeOrganisme;
    /**
     * Numéro Siret de l'organisme.
     *
     * @var String
     */
    protected $siret;
    /**
     * Services AccessMaster de l'utilisateur.
     *
     * @var array(Symfony\Component\Security\Core\Role\Role)
     */
    protected $roles;
    /**
     * Système de l'utilisateur.
     *
     * @var String
     */
    protected $systeme;

    /**
     * Défini l'Ismum (Concaténation du nom et du numéro d'agent : NOM-NUMERO).
     *
     * @param String $ismum
     */
    public function setIsmum($ismum)
    {
        $this->ismum = $ismum;
    }
    /**
     * Retourne l'Ismum (Concaténation du nom et du numéro d'agent : NOM-NUMERO).
     *
     * @return String
     */
    public function getIsmum()
    {
        return $this->ismum;
    }
    /**
     * Défini le nom de l'utilisateur.
     *
     * @param String $nom
     */
    public function setNom($nom)
    {
        $this->nom = $nom;
    }
    /**
     * Retourne le nom de l'utilisateur.
     *
     * @return String
     */
    public function getNom()
    {
        return $this->nom;
    }
    /**
     * Défini le prénom de l'utilisateur.
     *
     * @param String $prenom
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;
    }
    /**
     * Retourne le nom de l'utilisateur.
     *
     * @return String
     */
    public function getPrenom()
    {
        return $this->prenom;
    }
    /**
     * Défini l'id de l'utilisateur.
     *
     * @param String $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }
    /**
     * Retourne l'id de l'utilisateur.
     *
     * @return String
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Défini le numéro d'agent.
     *
     * @param String $chrono
     */
    public function setChrono($chrono)
    {
        $this->chrono = $chrono;
    }
    /**
     * Retourne le numéro d'agent.
     *
     * @return String
     */
    public function getChrono()
    {
        return $this->chrono;
    }
    /**
     * Défini le code organisme
     * @param String $codeOrganisme
     */
    public function setCodeOrganisme($codeOrganisme)
    {
        $this->codeOrganisme = $codeOrganisme;
    }
    /**
     * Retourne le code organisme
     * @return String
     */
    public function getCodeOrganisme()
    {
        return $this->codeOrganisme;
    }
    /**
     * Défini le SIRET de l'utilisateur.
     *
     * @param String $siret
     */
    public function setSiret($siret)
    {
        $this->siret = $siret;
    }
    /**
     * Retourne le SIRET de l'utilisateur.
     *
     * @return String
     */
    public function getSiret()
    {
        return $this->siret;
    }

    /**
     * Ajoute un service AccessMaster à l'utilisateur.
     *
     * @param String $role
     */
    public function addRole(Role $role)
    {
        $this->roles[] = $role;
    }
    /**
     * Retourne les services AccessMaster de l'utilisateur.
     *
     * @return array(CNAMTS\PHPK\SecurityBundle\Security\Role)
     */
    public function getRoles()
    {
        return $this->roles;
    }

    /**
     * Défini le système de l'utilisateur.
     *
     * @param String $systeme
     */
    public function setSysteme($systeme)
    {
        $this->systeme = $systeme;
    }
    /**
     * Retourne le système de l'utilisateur.
     *
     * @return String
     */
    public function getSysteme()
    {
        return $this->systeme;
    }

    /**
     * @ignore
     */
    public function eraseCredentials()
    {
    }

    /**
     * Permet de tester si deux instances de UserInterface sont égales.
     *
     * @param UserInterface $user
     *
     * @return boolean
     */
    public function equals(UserInterface $user)
    {
        if (!$user instanceof User) {
            return false;
        }

        if ($this->username !== $user->getUsername()) {
            return false;
        }
        if ($this->organisme !== $user->getOrganisme()) {
            return false;
        }
        if ($this->jeton !== $user->getJeton()) {
            return false;
        }

        return true;
    }

    /**
     * Retourne true si l'utilisateur possède le service passé en paramètre
     * Prend en paramètre une chaîne ou un tableau de chaînes.
     *
     * @param mixed $role
     *
     * @return boolean
     */
    public function isGranted($role)
    {
        if (is_array($role)) {
            foreach ($role as $r) {
                foreach ($this->getRoles() as $own) {
                    if ($own->getRole() === $r) {
                        return true;
                    }
                }
            }
        } elseif (is_string($role)) {
            foreach ($this->getRoles() as $own) {
                if ($own->getRole() === $role) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * @ignore
     */
    public function getUsername()
    {
        return $this->getIsmum();
    }
    /**
     * @ignore
     */
    public function getPassword()
    {
        return;
    }
    /**
     * @ignore
     */
    public function getSalt()
    {
        return;
    }
}

这个抽象用户的实现

<?php
namespace CNAMTS\PHPK\SecurityBundle\Security\User;

/**
 * Classe décrivant un User lorsque le mode AccessMaster pour la sécurité est activé.
 */
class AccessMasterUser extends User implements \Serializable
{
    public function serialize()
    {
        return \json_encode(array(
            $this->civilite,
            $this->chrono,
            $this->codeOrganisme,
            $this->id,
            $this->ismum,
            $this->nom,
            $this->prenom,
            serialize($this->roles),
            $this->siret,
            $this->systeme
        ));
    }
    public function unserialize($serialized)
    {
        list(
            $this->civilite,
            $this->chrono,
            $this->codeOrganisme,
            $this->id,
            $this->ismum,
            $this->nom,
            $this->prenom,
            $roles,
            $this->siret,
            $this->systeme
        ) = \json_decode($serialized);

        $this->roles = unserialize($roles);
    }

    /**
     * Civilité de l'utilisateur.
     *
     * @var String
     */
    private $civilite;

    /**
     * Défini la civilité de l'utilisateur.
     *
     * @param String $civilite
     */
    public function setCivilite($civilite)
    {
        $this->civilite = $civilite;
    }
    /**
     * Retourne la civilité de l'utilisateur.
     *
     * @return String
     */
    public function getCivilite()
    {
        return $this->civilite;
    }
}

角色

<?php
namespace CNAMTS\PHPK\SecurityBundle\Security;

use Symfony\Component\Security\Core\Role\RoleInterface;

class Role implements RoleInterface, \Serializable
{
    private $role;
    private $attributes;

    public function serialize() {
        return \json_encode(array(
            $this->role,
            $this->attributes
        ));
    }

    public function unserialize($serialized) {
        list(
            $this->role,
            $this->attributes
        ) = \json_decode($serialized);
    }

    /**
     * Constructor.
     *
     * @param string $role The role name
     */
    public function __construct($role, $attributes = array())
    {
        $this->role = (string) $role;
        $this->attributes = $attributes;
    }

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

    /**
     * {@inheritdoc}
     */
    public function getAttributes()
    {
        return $this->attributes;
    }
}

刚登录,所有角色都ok,users.roles和roles完全一样:

但是当我转到另一个页面时,我得到了这个:

我的代码有什么问题? 为什么这适用于 PHP 5.3.3,但不适用于 PHP 5.4.40 或 PHP 5.5.28?

提前谢谢,我完全被屏蔽了。

【问题讨论】:

  • 升级 php 后是否清除了会话数据?
  • 它不在同一台服务器上,它适用于我的 PHP 5.3.3 服务器,但不适用于其他两个服务器(5.4.40 和 5.5.28)。所以我假设该会话已清除。 -- 好的,我刚刚清除了所有内容,问题仍然存在:-/

标签: php symfony serialization


【解决方案1】:

我认为您的问题的答案在那个链接的错误中,正如 GitHub 用户 Sharom 指出的 herehere

因此,问题似乎在于您在不必要时将User::$roles 包含到User 类本身的序列化/反序列化中。

附:我还认为您使用 json_*() 作为 Serializable 接口背后的机制很奇怪,而且可能不合适。

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2012-06-28
    • 2017-02-10
    • 2015-04-20
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多