Skip to content

Commit 11d87d1

Browse files
Merge branch '5.3' into 5.4
* 5.3: expand uninitialized session tests [Lock] Release PostgreSqlStore connection lock on failure [DomCrawler] Fix HTML5 parser charset option cs fix [HttpKernel] Do not attempt to register enum arguments in controller service locator [Mime] Fix missing sprintf in DkimSigner [Translation] [LocoProvider] Use rawurlencode and separate tag setting [Security] fix unserializing session payloads from v4 [Cache] Don't lock when doing nested computations [Messenger] fix Redis support on 32b arch [HttpFoundation] Fix notice when HTTP_PHP_AUTH_USER passed without pass [Security] Add getting started example to README
2 parents e3dddc4 + cce790b commit 11d87d1

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-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
Sponsor
1042
-------

Role/Role.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Role;
13+
14+
/**
15+
* Allows migrating session payloads from v4.
16+
*
17+
* @internal
18+
*/
19+
class Role
20+
{
21+
private $role;
22+
23+
private function __construct()
24+
{
25+
}
26+
27+
public function __toString(): string
28+
{
29+
return $this->role;
30+
}
31+
}

Role/SwitchUserRole.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Role;
13+
14+
/**
15+
* Allows migrating session payloads from v4.
16+
*
17+
* @internal
18+
*/
19+
class SwitchUserRole extends Role
20+
{
21+
private $deprecationTriggered;
22+
private $source;
23+
}

Tests/Role/LegacyRoleTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Role;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16+
17+
class LegacyRoleTest extends TestCase
18+
{
19+
public function testPayloadFromV4CanBeUnserialized()
20+
{
21+
$serialized = 'C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":236:{a:3:{i:0;N;i:1;s:4:"main";i:2;a:5:{i:0;s:2:"sf";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Role'."\0".'role'."\0".'";s:9:"ROLE_USER";}}i:3;a:0:{}i:4;a:1:{i:0;s:9:"ROLE_USER";}}}}';
22+
23+
$token = unserialize($serialized);
24+
25+
$this->assertInstanceOf(UsernamePasswordToken::class, $token);
26+
$this->assertSame(['ROLE_USER'], $token->getRoleNames());
27+
}
28+
}

0 commit comments

Comments
 (0)