@@ -3,8 +3,40 @@ Security Component - Core
3
3
4
4
Security provides an infrastructure for sophisticated authorization systems,
5
5
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
+ ```
8
40
9
41
Resources
10
42
---------
0 commit comments