Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security Symfony2-添加控制器安全性以限制用户对特定博客的访问_Security_Symfony_User Roles - Fatal编程技术网

Security Symfony2-添加控制器安全性以限制用户对特定博客的访问

Security Symfony2-添加控制器安全性以限制用户对特定博客的访问,security,symfony,user-roles,Security,Symfony,User Roles,我试图通过为每个用户使用USER_角色来实现安全性,以限制对特定博客的访问。每个博客页面都有一个可以向其发布文章的所有者 这是建立在我从KNP大学的精彩教程中学到的基础上的 我已经使用security.yml中的access\u control设置了这个选项,以根据每个用户的用户角色限制对其的访问。(user1可以访问/job1/new和/job1/create,以便在博客页面上创建/编辑/删除帖子——每个博客页面只有一个用户可以访问) 每个/job1、/job2等都是独立的博客页面。我在Tw

我试图通过为每个用户使用USER_角色来实现安全性,以限制对特定博客的访问。每个博客页面都有一个可以向其发布文章的所有者

这是建立在我从KNP大学的精彩教程中学到的基础上的

我已经使用security.yml中的access\u control设置了这个选项,以根据每个用户的用户角色限制对其的访问。(user1可以访问/job1/new和/job1/create,以便在博客页面上创建/编辑/删除帖子——每个博客页面只有一个用户可以访问)

每个/job1、/job2等都是独立的博客页面。我在Twig中使用if语句来确定哪个用户有权创建/编辑/删除帖子

{% if is_granted('ROLE_USER1') %}
    <li><a href="{{ path('job1_new') }}">Add New Post</a></li>
{% endif %}
{%if被授予('ROLE\u USER1')%}
  • {%endif%}
    问题是,随着我添加更多的博客页面,我需要在访问控制中创建更多的路径(例如,/job4、/job5等),这不是一个理想的解决方案,尽管它确实有效

    我已经在下面的链接中详细介绍了代码,因为根据与Ryan Weaver的对话“joe joe”,建议在控制器中使用安全性--

    我的问题是:

    1) 既然我已经与用户和类别建立了多人关系,我如何在控制器中设置安全性,以防止其他用户访问他们没有角色的创建/编辑/删除操作


    2) 如何在Twig中隐藏使用此方法创建/编辑/删除的选项,以及使用此方法在访问控制中添加什么?

    增加访问控制以使其需要显式包含每个新用户是不现实的。相反,可以使用ROLE_USER只允许经过身份验证的用户编辑/创建任何实体(例如博客)。一旦用户通过身份验证,控制器就可以提供对该用户博客实体的访问

    这需要用户和博客之间的一对多关系。在控制器中,它就变成了一个简单的事情,比如:

    ...
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
    
    /**
     * ...
     * @Security("has_role('ROLE_USER')")
     */
    
    class BlogifyController extends Controller
    {
    
       public function blogAction
        {
            $user = $this->getUser();
            $blogs = $user->getBlogs();
        // do your form thing, etc.
        ...
        }
    }
    

    我认为您应该使用安全包中的内置投票者。所以,您创建了一个博客实体,该实体与用户实体的关系为1:1,然后,在
    vote
    方法中构建您的逻辑,在controller中使用该投票者,就是这样。以下是示例:

    class SomeVoter implements VoterInterface
    {
        const CREATE = 'create';
        const EDIT = 'edit';
        const DELETE = 'delete';
    
        /**
         * @param string $attribute
         * @return bool
         */
        public function supportsAttribute($attribute)
        {
            return in_array($attribute, array(
                self::CREATE,
                self::EDIT,
                self::DELETE
            ));
        }
    
        /**
         * @param string $class
         * @return bool
         */
        public function supportsClass($class)
        {
            $supportedClass = 'Acme\DemoBundle\Entity\Blog';
    
            return $supportedClass === $class || is_subclass_of($class, $supportedClass);
        }
    
        /**
         * @param TokenInterface $token
         * @param object $blog
         * @param array $attributes
         * @return int
         */
        public function vote(TokenInterface $token, $blog, array $attributes)
        {
            ....
    
            $attribute = $attributes[0];
            $user = $token->getUser();
    
            switch($attribute) {
                case 'edit':
                    if ($user->getId() === $blog->getUser()->getId()) {
                        return VoterInterface::ACCESS_GRANTED;
                    }
                    break;
    
                ....
            }
    
            ...
        }
    }
    
    控制器操作:

    public function editAction($id)
    {
        $blog = ...;
    
        if (false === $this->get('security.context')->isGranted('edit', $blog)) {
            throw new AccessDeniedException('Unauthorised access!');
        }
    
        ...
    }
    
    public function editAction($id)
    {
        $blog = ...;
    
        if (false === $this->get('security.context')->isGranted('edit', $blog)) {
            throw new AccessDeniedException('Unauthorised access!');
        }
    
        ...
    }