Skip to content

Commit fb0a242

Browse files
committed
[Security] Add getting started example to README
1 parent 2ce917f commit fb0a242

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)