symplify / symfony-security
此包已废弃且不再维护。未建议替代包。
从Symfony\Security提取的投票者和防火墙功能,供独立使用。
v1.2.21
2016-12-23 20:55 UTC
Requires
- php: ^7.0
- nette/di: ~2.4
- nette/http: ~2.4
- nette/security: ~2.4
- symfony/security-core: ~3.1
- symfony/security-http: ~3.1
- symplify/symfony-event-dispatcher: ~1.2
Requires (Dev)
- nette/application: ~2.4
- nette/bootstrap: ~2.4
- nette/robot-loader: ~2.4
- nette/utils: ~2.4
- phpunit/phpunit: ~5.7
- symplify/coding-standard: ~1.2
- tracy/tracy: ~2.4
This package is not auto-updated.
Last update: 2018-07-17 17:55:08 UTC
README
安装
composer require symplify/symfony-security
注册扩展
# app/config/config.neon extensions: - Symplify\SymfonySecurity\Adapter\Nette\DI\SymfonySecurityExtension - Symplify\SymfonyEventDispatcher\DI\SymfonyEventDispatcherExtension
使用
投票者
首先,阅读Symfony食谱
然后创建一个新的投票者,实现Symfony\Component\Security\Core\Authorization\Voter\VoterInterface
并注册为服务在config.neon
services: - App\SomeModule\Security\Voter\MyVoter
然后在我们需要验证访问的地方,我们将使用AuthorizationChecker
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; class Presenter { /** * @var AuthorizationCheckerInterface */ private $authorizationChecker; public function __construct(AuthorizationCheckerInterface $authorizationChecker) { $this->authorizationChecker = $authorizationChecker; } /** * @param PresenterComponentReflection $element */ public function checkRequirements($element) { if ($this->authorizationChecker->isGranted('access', $element) === FALSE) { throw new ForbiddenRequestException; } } }
防火墙
原始的Symfony防火墙相当简化,默认支持模块化。
我们只需要创建一个匹配器和一个监听器。
请求匹配器
此服务将匹配管理模块中的所有站点 - 以/admin
开头的url
use Symfony\Component\HttpFoundation\Request; use Symplify\SymfonySecurity\Contract\HttpFoundation\RequestMatcherInterface; class AdminRequestMatcher implements RequestMatcherInterface { /** * {@inheritdoc} */ public function getFirewallName() { return 'adminSecurity'; } /** * {@inheritdoc} */ public function matches(Request $request) { $url = $request->getPathInfo(); return strpos($url, '/admin') === 0; } }
防火墙监听器
它将确保用户已登录且具有'admin'角色,否则重定向。
use Nette\Application\AbortException; use Nette\Application\Application; use Nette\Application\Request; use Nette\Security\User; use Symplify\SymfonySecurity\Contract\Http\FirewallListenerInterface; class LoggedAdminFirewallListener implements FirewallListenerInterface { /** * @var User */ private $user; public function __construct(User $user) { $this->user = $user; } /** * {@inheritdoc} */ public function getFirewallName() { return 'adminSecurity'; } /** * {@inheritdoc} */ public function handle(Application $application, Request $applicationRequest) { if ( ! $this->user->isLoggedIn()) { throw new AbortException; } if ( ! $this->user->isInRole('admin')) { throw new AbortException; } } }
然后我们注册这两个服务。
services: - AdminRequestMatcher - LoggedAdminFirewallListener
就是这样!
测试
composer check-cs # see "scripts" section of composer.json for more details
vendor/bin/phpunit
贡献
规则很简单
- 新功能需要测试
- 所有测试必须通过
- 每个PR一个功能
我们很高兴合并你的功能!