Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement security voter #58

Open
PGBastien opened this issue Aug 10, 2017 · 1 comment
Open

Implement security voter #58

PGBastien opened this issue Aug 10, 2017 · 1 comment
Assignees
Labels

Comments

@PGBastien
Copy link

The documentation talking about security https://github.com/Youshido/GraphQLBundle#using-security-voter but i get an Access denied 403 every time.

@portey portey added the bug label Sep 27, 2017
@portey portey self-assigned this Sep 27, 2017
@LiFeAiR
Copy link
Contributor

LiFeAiR commented Feb 2, 2018

use Implement like this

class GraphQLVoter extends Voter
{
    /** @var RolesCheckerService */
    protected $rolesChecker;

    public function setRolesChecker(RolesCheckerService $rolesChecker)
    {
        $this->rolesChecker = $rolesChecker;
    }

    /**
     * {@inheritdoc}
     */
    protected function supports($attribute, $subject)
    {
        return in_array(
            $attribute,
            [
                SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE,
                SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE,
            ],
            true
        );
    }

    /**
     * {@inheritdoc}
     *
     * @param Mutation $subject
     */
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        $user = $token->getUser();

        if (! $user instanceof UserInfo) {
            return false;
        }

        if (SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE === $attribute) {
            return true;
        }

        if (SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE === $attribute) {
            return $this->rolesChecker->hasAccessTo($subject->getName());
        }

        return true;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants