jdecool / security-role-checker-bundle

此包已废弃,不再维护。没有推荐替代包。

提供检查用户安全角色的Symfony服务

1.0.0 2016-07-14 09:19 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:18:20 UTC


README

Build Status Build status Scrutinizer Code Quality Latest Stable Version

此包提供用于检查用户安全角色的Symfony服务。

兼容性

此包已在Symfony 2.7+上测试,但应该与Symfony 2.3+兼容

警告

RoleChecker服务不模拟用户身份验证。这意味着如果身份验证过程修改用户权限,该服务可以检测角色更新。

文档

安装

使用composer安装扩展

{
    "require": {
        "jdecool/security-role-checker-bundle": "~1.0"
    }
}

在您的应用AppKernel中启用扩展

<?php

public function registerBundles()
{
    $bundles = [
        // ...
        new JDecool\Bundle\SecurityRoleCheckerBundle\JDecoolSecurityRoleCheckerBundle(),
    ];

    // ...

    return $bundles;
}

使用

您可以通过访问jdecool.security.role_checker服务来检查角色

class MyController
{
    public function myAction()
    {
        $roleChecker = $this->get('jdecool.security.role_checker');
        
        var_dump($role->hasRole('ROLE_USER')); // checking role for current user
        
        $userWithRole = $this->getDoctrine()->getRepository(/* ... */)->find(1);
        var_dump($role->hasRole('ROLE_USER', $userWithRole)); // true
        
        $userWithoutRole = $this->getDoctrine()->getRepository(/* ... */)->find(2);
        var_dump($role->hasRole('ROLE_USER', $userWithoutRole)); // false
        
        // ... 
    }
}

您还可以在Twig模板中检查角色(即使这不是最佳实践)

{{ has_role('ROLE_USER') ]} # Check role for current user
{{ has_role('ROLE_USER', other_user) ]}

许可证

此库在MIT许可证下发布