Skip to content

Commit 5fa77a1

Browse files
committed
minor #44778 [Security] Add getting started example to README (wouterj)
This PR was merged into the 5.3 branch. Discussion ---------- [Security] Add getting started example to README | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - We're about to remove the Security component docs from symfony.com. I tried creating a minimal example for Security HTTP - but that one is quite coupled with the HttpKernel component. So let's not document things there. If you're using it standalone, you're on your own (and already such an advanced user that you probably will be able to find out things on your own). Targeting 5.3 to not have to document the deprecated APIs. This is also in sync with the doc removal (we'll keep documenting the legacy stuff in <5.3) Commits ------- 10846fed96 [Security] Add getting started example to README
2 parents 7e9ce0c + fb0a242 commit 5fa77a1

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,40 @@ Security Component - Core
33

44
Security provides an infrastructure for sophisticated authorization systems,
55
which makes it possible to easily separate the actual authorization logic from
6-
so called user providers that hold the users credentials. It is inspired by
7-
the Java Spring framework.
6+
so called user providers that hold the users credentials.
7+
8+
Getting Started
9+
---------------
10+
11+
```
12+
$ composer require symfony/security-core
13+
```
14+
15+
```php
16+
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
17+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
18+
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
19+
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
20+
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
21+
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
22+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
23+
use Symfony\Component\Security\Core\Role\RoleHierarchy;
24+
25+
$accessDecisionManager = new AccessDecisionManager([
26+
new AuthenticatedVoter(new AuthenticationTrustResolver()),
27+
new RoleVoter(),
28+
new RoleHierarchyVoter(new RoleHierarchy([
29+
'ROLE_ADMIN' => ['ROLE_USER'],
30+
]))
31+
]);
32+
33+
$user = new \App\Entity\User(...);
34+
$token = new UsernamePasswordToken($user, 'main', $user->getRoles());
35+
36+
if (!$accessDecisionManager->decide($token, ['ROLE_ADMIN'])) {
37+
throw new AccessDeniedException();
38+
}
39+
```
840

941
Resources
1042
---------

0 commit comments

Comments
 (0)