knplabs / rad-security
此软件包已被放弃且不再维护。没有建议的替代软件包。
提供RAD安全组件
v4.0.0
2020-11-09 10:26 UTC
Requires
- symfony/http-kernel: ~5.1
- symfony/security-core: ~5.1
Requires (Dev)
- php: >=7.2.5
- bossa/phpspec2-expect: ~3.0
- friendsofphp/php-cs-fixer: ^2.16
- pedrotroller/php-cs-custom-fixer: ~2.23
- phpspec/phpspec: ~6.0
- symfony/dependency-injection: ~5.1
- symfony/http-foundation: ~5.1
Suggests
- knplabs/rad-resource-resolver: Allows to check authorization against resources resolved from the routing.
README
很遗憾我们决定不再维护此项目(查看原因)。如果您想将其他软件包标记为替代品,请发送电子邮件至 hello@knplabs.com。
快速应用开发:安全
提供RAD安全组件
官方维护者
安装
composer require knplabs/rad-security ~4.0
// config/bundles.php <?php return [ Knp\Rad\Security\Bundle\SecurityBundle::class => ['all' => true], ];
使用
IS_OWNER 选举人
您现在可以访问一个检查认证用户是否是对象所有者的选举人。
安全令牌中包含的用户必须实现 Knp\Rad\Security\OwnerInterface
。
您即将测试所有权的对象必须实现 Knp\Rad\Security\OwnableInterface
。
示例
<?php namespace App\Model; use Knp\Rad\Security\OwnerInterface; class User implements OwnerInterface { }
<?php namespace App\Model; use Knp\Rad\Security\OwnableInterface; use App\Model\User; class Book implements OwnableInterface { /** @var App\Model\User */ protected $writtenBy; public function __construct(User $writtenBy) { $this->writtenBy = $writtenBy; } public function getOwner() { return $this->writtenBy; } }
$zola = new \App\Model\User(); // He is the current authenticated user $hugo = new \App\Model\User(); $germinal = new \App\Model\Book($zola); $miserables = new \App\Model\Book($hugo); $authorizationChecker = $container->get(/* ... */); $authorizationChecker->isGranted(array('IS_OWNER'), $germinal); // true $authorizationChecker->isGranted(array('IS_OWNER'), $miserables); // false
从路由中获取安全
您可以通过提供带有 roles
参数的角色或角色数组来直接从路由中指定安全约束。如果您指定一个数组,它将原样传递给授权检查器,这意味着 授权策略取决于您对安全组件的配置。
示例
acme_demo: path: /demo defaults: _controller: FrameworkBundle:Template:template template: Acme:demo:index.html.twig _security: - roles: IS_AUTHENTICATED_FULLY
当与 rad-resource-resolver 组件和 SensioLabs 的 ParamConverter 一起使用时,优点更为明显。您可以在请求的 attributes
中提供先前解析并可用于请求的 subject
。如果您有多个对象需要检查安全约束,您可以指定多个规则。
示例
acme_group_update: path: /team/{tid}/group/{gid}/update defaults: _controller: AcmeBundle:Group:update template: Acme:Group:update.html.twig _resources: team: # ... group: # ... _security: - roles: [IS_MEMBER, ANOTHER_ROLE] subject: team - roles: IS_OWNER subject: group