Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a5db15

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 ------- 10846fe [Security] Add getting started example to README
2 parents 7baf01f + 10846fe commit 6a5db15

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/Symfony/Component/Security/Core/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
---------

src/Symfony/Component/Security/Http/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
Security Component - HTTP Integration
22
=====================================
33

4-
Security provides an infrastructure for sophisticated authorization systems,
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.
4+
The Security HTTP component provides an HTTP integration of the Security Core
5+
component. It allows securing (parts of) your application using firewalls and
6+
provides authenticators to authenticate visitors.
7+
8+
Getting Started
9+
---------------
10+
11+
```
12+
$ composer require symfony/security-http
13+
```
814

915
Resources
1016
---------

0 commit comments

Comments
 (0)