madlines/security-resolver-bundle

1.0 2016-02-28 10:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:21 UTC


README

这是 Symfony2 或 Symfony3 与 Madlines 安全解析器 之间的桥梁。

安装

composer require madlines/security-resolver-bundle

然后更新你的 AppKernel.php

    public function registerBundles()
    {
        $bundles = [
            // ...
            new Madlines\SecurityResolverBundle\MadlinesSecurityResolverBundle(),
            // ...
        ];

        // ...

        return $bundles;
    }

配置

准备你的投票者如下

<?php

class PostEditVoter
{
    public function isGranted($user, $task)
    {
        // if (!($task instanceof PostEditTask)) {
        if ($task !== 'post_edit') {
            return null; // null means 'ignore'
            // returning integer 0 means the same
        }

        if ($user->hasRole('ROLE_ADMIN')) {
            return true; // agree
            // returning integer 1 means the same
        }

        return false; // disagree
        // returning integer -1 means the same
    }
}

然后以标签服务的形式连接每个投票者

    voter.post_edit:
        class: PostEditVoter
        public: false
        tags:
            - { name: madlines.security_resolver.voter }

你可以通过在标签中添加一个 method 属性来更改投票者的方法名。

使用方法

在注册为 madlines.security_resolver.access_resolver 的安全解析器服务上执行 isGranted 方法。例如

$isGranted = $this->get('madlines.security_resolver.access_resolver')->isGranted(
    $this->getUser(),
    'post_edit'
);

if (!$isGranted) {
    throw new Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException();
}