在boolean上调用成员函数getRole()

时间:2017-02-11 17:12:23

标签: php symfony

我在登录时遇到此错误:

FatalThrowableError in RoleHierarchy.php line 43: Call to a member function getRole() on boolean

    in RoleHierarchy.php line 43
    at RoleHierarchy->getReachableRoles(array(false)) in RoleHierarchyVoter.php line 39
    at RoleHierarchyVoter->extractRoles(object(UsernamePasswordToken)) in RoleVoter.php line 42
    at RoleVoter->vote(object(UsernamePasswordToken), object(Request), array('IS_AUTHENTICATED_ANONYMOUSLY')) in classes.php line 4857
    at AccessDecisionManager->decideAffirmative(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in classes.php line 4851
    at AccessDecisionManager->decide(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in DebugAccessDecisionManager.php line 48
    at DebugAccessDecisionManager->decide(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in AccessListener.php line 69
    at AccessListener->handle(object(GetResponseEvent)) in classes.php line 5044
    at Firewall->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
    at call_user_func(array(object(Firewall), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher)) in WrappedListener.php line 106
    at WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher))
    at call_user_func(object(WrappedListener), object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher)) in classes.php line 2923
    at EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent)) in classes.php line 2838
    at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in TraceableEventDispatcher.php line 136
    at TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in classes.php line 4273
    at HttpKernel->handleRaw(object(Request), 1) in classes.php line 4243
    at HttpKernel->handle(object(Request), 1, true) in Kernel.php line 168
    at Kernel->handle(object(Request)) in app_dev.php line 28
    at require('/home/owner/Desktop/Workspace/Documentary/web/app_dev.php') in router_dev.php line 40

security.yml

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:
    encoders:
        DW\UserBundle\Entity\User:
            algorithm: bcrypt
            cost: 12

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    # http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        main:
            entity:
                class: DW\UserBundle\Entity\User
                property: username
        in_memory:
            memory: ~

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern:    ^/
            anonymous: ~
            form_login:
                login_path:  login
                check_path:  login_check
                default_target_path: /
                always_use_default_target_path: true
            logout:
                path:   logout
                target: /

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/connect/facebook, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_ADMIN }
        - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

用户

<?php

namespace DW\UserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use DW\ActivityBundle\Entity\Activity;
use DW\CommentBundle\Entity\Comment;
use DW\CommentBundle\Entity\CommentVote;
use DW\WatchlistBundle\Entity\Watchlist;
use Gedmo\Timestampable\Traits\Timestampable;
use Symfony\Component\Security\Core\User\UserInterface;

class User implements UserInterface
{
    use Timestampable;

    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $username;

    /**
     * @var string
     */
    private $password;

    /**
     * @var string
     */
    private $salt;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $avatar;

    /**
     * @var string
     */
    private $resetKey;

    /**
     * @var \DateTime
     */
    private $resetRequestAt;

    /**
     * @var \DateTime
     */
    private $lastResetAt;

    /**
     * @var \DateTime
     */
    private $activatedAt;

    /**
     * @var string
     */
    private $activationKey;

    /**
     * @var \DateTime
     */
    private $lastActiveAt;

    /**
     * @var int
     */
    private $status;

    /**
     * @var Facebook
     */
    private $facebook;

    /**
     * @var ArrayCollection|Comment[]
     */
    private $comments;

    /**
     * @var ArrayCollection|Role[]
     */
    private $roles;

    /**
     * @var ArrayCollection|Watchlist[]
     */
    private $watchlisted;

    /**
     * @var ArrayCollection|Activity[]
     */
    private $activity;

    /**
     * @var ArrayCollection|CommentVote[]
     */
    private $commentVoteGiven;

    /**
     * @var ArrayCollection|CommentVote[]
     */
    private $commentVoteReceived;

    public function __construct()
    {
        $this->comments = new ArrayCollection();
        $this->roles = new ArrayCollection();
        $this->watchlisted = new ArrayCollection();
        $this->activity = new ArrayCollection();
        $this->commentVoteGiven = new ArrayCollection();
        $this->commentVoteReceived = new ArrayCollection();
    }

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

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

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

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

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

    /**
     * @return string
     */
    public function getSalt()
    {
        return null;
    }

    /**
     * @param string $salt
     */
    public function setSalt(string $salt)
    {
        $this->salt = $salt;
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param string $email
     */
    public function setEmail(string $email)
    {
        $this->email = $email;
    }

    /**
     * @return string
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * @param string $avatar
     */
    public function setAvatar(string $avatar)
    {
        $this->avatar = $avatar;
    }

    /**
     * @return string
     */
    public function getResetKey()
    {
        return $this->resetKey;
    }

    /**
     * @param string $resetKey
     */
    public function setResetKey(string $resetKey)
    {
        $this->resetKey = $resetKey;
    }

    /**
     * @return \DateTime
     */
    public function getResetRequestAt()
    {
        return $this->resetRequestAt;
    }

    /**
     * @param \DateTime $resetRequestAt
     */
    public function setResetRequestAt(\DateTime $resetRequestAt)
    {
        $this->resetRequestAt = $resetRequestAt;
    }

    /**
     * @return \DateTime
     */
    public function getLastResetAt()
    {
        return $this->lastResetAt;
    }

    /**
     * @param \DateTime $lastResetAt
     */
    public function setLastResetAt(\DateTime $lastResetAt)
    {
        $this->lastResetAt = $lastResetAt;
    }

    /**
     * @return \DateTime
     */
    public function getActivatedAt()
    {
        return $this->activatedAt;
    }

    /**
     * @param \DateTime $activatedAt
     */
    public function setActivatedAt(\DateTime $activatedAt)
    {
        $this->activatedAt = $activatedAt;
    }

    /**
     * @return string
     */
    public function getActivationKey()
    {
        return $this->activationKey;
    }

    /**
     * @param string $activationKey
     */
    public function setActivationKey(string $activationKey)
    {
        $this->activationKey = $activationKey;
    }

    /**
     * @return \DateTime
     */
    public function getLastActiveAt()
    {
        return $this->lastActiveAt;
    }

    /**
     * @param \DateTime $lastActiveAt
     */
    public function setLastActiveAt(\DateTime $lastActiveAt)
    {
        $this->lastActiveAt = $lastActiveAt;
    }

    /**
     * @return int
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param int $status
     */
    public function setStatus(int $status)
    {
        $this->status = $status;
    }

    /**
     * @return Facebook
     */
    public function getFacebook()
    {
        return $this->facebook;
    }

    /**
     * @param Facebook $facebook
     */
    public function setFacebook(Facebook $facebook)
    {
        $this->facebook = $facebook;
    }

    /**
     * @return ArrayCollection|Comment[]
     */
    public function getComments()
    {
        return $this->comments;
    }

    /**
     * @param ArrayCollection|Comment[] $comments
     */
    public function setComments($comments)
    {
        $this->comments = $comments;
    }

    /**
     * @param Comment $comment
     * @return bool
     */
    public function hasComment(Comment $comment)
    {
        return $this->comments->contains($comment);
    }

    /**
     * @param Comment $comment
     */
    public function addComment(Comment $comment)
    {
        if (!$this->comments->contains($comment)) {
            $this->comments->add($comment);
        }
    }

    /**
     * @return ArrayCollection|Role[]
     */
    public function getRoles()
    {
        return $this->roles->toArray();
    }

    /**
     * @param ArrayCollection|Role[] $roles
     */
    public function setRoles($roles)
    {
        $this->roles = $roles;
    }

    /**
     * @param Role $role
     * @return bool
     */
    public function hasRole(Role $role)
    {
        return $this->roles->contains($role);
    }

    /**
     * @param Role $role
     */
    public function addRole(Role $role)
    {
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
        }
    }

    /**
     * @return ArrayCollection|Watchlist[]
     */
    public function getWatchlisted()
    {
        return $this->watchlisted;
    }

    /**
     * @param array $watchlisted
     */
    public function setWatchlisted($watchlisted)
    {
        $this->watchlisted = $watchlisted;
    }

    /**
     * @param Watchlist $watchlist
     * @return bool
     */
    public function hasWatchlist(Watchlist $watchlist)
    {
        return $this->watchlisted->contains($watchlist);
    }

    /**
     * @param Watchlist $watchlist
     */
    public function addWatchlist(Watchlist $watchlist)
    {
        if (!$this->hasWatchlist($watchlist)) {
            $this->watchlisted->add($watchlist);
        }
    }

    /**
     * @return ArrayCollection|Activity[]
     */
    public function getActivity()
    {
        return $this->activity;
    }

    /**
     * @param array $activity
     */
    public function setActivity($activity)
    {
        $this->activity = $activity;
    }

    /**
     * @param Activity $activity
     * @return bool
     */
    public function hasActivity(Activity $activity)
    {
        return $this->activity->contains($activity);
    }

    /**
     * @param Activity $activity
     */
    public function addActivity(Activity $activity)
    {
        if (!$this->hasActivity($activity)) {
            $this->activity->add($activity);
        }
    }

    /**
     * @return ArrayCollection|CommentVote[]
     */
    public function getCommentsVotesGiven()
    {
        return $this->commentVoteGiven;
    }

    /**
     * @param $commentsVotesGiven
     */
    public function setCommentsVotesGiven($commentsVotesGiven)
    {
        $this->commentsVotesGiven = $commentsVotesGiven;
    }

    /**
     * @param CommentVote $commentVote
     * @return bool
     */
    public function hasCommentVoteGiven(CommentVote $commentVote)
    {
        return $this->commentVoteGiven->contains($commentVote);
    }

    /**
     * @param CommentVote $commentVote
     */
    public function addCommentVoteGiven(CommentVote $commentVote)
    {
        if (!$this->hasCommentVoteGiven($commentVote)) {
            $this->commentVoteGiven->add($commentVote);
        }
    }

    /**
     * @return ArrayCollection|CommentVote[]
     */
    public function getCommentVoteReceived()
    {
        return $this->commentVoteReceived;
    }

    /**
     * @param $commentVoteReceived
     */
    public function setCommentVoteReceived($commentVoteReceived)
    {
        $this->commentsVoteReceived = $commentVoteReceived;
    }

    /**
     * @param CommentVote $commentVote
     * @return bool
     */
    public function hasCommentVoteReceived(CommentVote $commentVote)
    {
        return $this->commentVoteReceived->contains($commentVote);
    }

    public function addCommentVoteReceived(CommentVote $commentVote)
    {
        if (!$this->hasCommentVoteReceived($commentVote)) {
            $this->addCommentVoteReceived($commentVote);
        }
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
        //@TODO
    }
}

作用

<?php

namespace DW\UserBundle\Entity;

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

    class Role implements RoleInterface
    {
        /**
         * @var string
         */
        private $id;

        /**
         * @var string
         */
        private $name;

        /**
         * @var string
         */
        private $role;

        /**
         * @var ArrayCollection|User[]
         */
        private $users;

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

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

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

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

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

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

        /**
         * @return ArrayCollection|User[]
         */
        public function getUsers()
        {
            return $this->users;
        }

        /**
         * @param ArrayCollection|User[] $users
         */
        public function setUsers($users)
        {
            $this->users = $users;
        }
    }

1 个答案:

答案 0 :(得分:3)

更改

volatile

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