Skip to content

Commit c180104

Browse files
[Security] add union types
1 parent 6c91640 commit c180104

35 files changed

+82
-158
lines changed

src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,8 @@ public function getUser()
9393
/**
9494
* {@inheritdoc}
9595
*/
96-
public function setUser($user)
96+
public function setUser(string|\Stringable|UserInterface $user)
9797
{
98-
if (!($user instanceof UserInterface || $user instanceof \Stringable || \is_string($user))) {
99-
throw new \InvalidArgumentException('$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string.');
100-
}
101-
10298
if (null === $this->user) {
10399
$changed = false;
104100
} elseif ($this->user instanceof UserInterface) {
@@ -233,12 +229,7 @@ public function getAttribute(string $name)
233229
return $this->attributes[$name];
234230
}
235231

236-
/**
237-
* Sets an attribute.
238-
*
239-
* @param mixed $value The attribute value
240-
*/
241-
public function setAttribute(string $name, $value)
232+
public function setAttribute(string $name, mixed $value)
242233
{
243234
$this->attributes[$name] = $value;
244235
}
@@ -267,9 +258,9 @@ final public function serialize(): string
267258
/**
268259
* @internal
269260
*/
270-
final public function unserialize($serialized)
261+
final public function unserialize(string $serialized)
271262
{
272-
$this->__unserialize(\is_array($serialized) ? $serialized : unserialize($serialized));
263+
$this->__unserialize(unserialize($serialized));
273264
}
274265

275266
private function hasUserChanged(UserInterface $user): bool

src/Symfony/Component/Security/Core/Authentication/Token/AnonymousToken.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ class AnonymousToken extends AbstractToken
2323
private $secret;
2424

2525
/**
26-
* @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
27-
* @param string|\Stringable|UserInterface $user
28-
* @param string[] $roles
26+
* @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
27+
* @param string[] $roles
2928
*/
30-
public function __construct(string $secret, $user, array $roles = [])
29+
public function __construct(string $secret, string|\Stringable|UserInterface $user, array $roles = [])
3130
{
3231
parent::__construct($roles);
3332

src/Symfony/Component/Security/Core/Authentication/Token/NullToken.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication\Token;
1313

14+
use Symfony\Component\Security\Core\User\UserInterface;
15+
1416
/**
1517
* @author Wouter de Jong <wouter@wouterj.nl>
1618
*/
@@ -36,7 +38,7 @@ public function getUser()
3638
return '';
3739
}
3840

39-
public function setUser($user)
41+
public function setUser(string|\Stringable|UserInterface $user)
4042
{
4143
throw new \BadMethodCallException('Cannot set user on a NullToken.');
4244
}
@@ -87,7 +89,7 @@ public function getAttribute(string $name)
8789
return null;
8890
}
8991

90-
public function setAttribute(string $name, $value)
92+
public function setAttribute(string $name, mixed $value)
9193
{
9294
throw new \BadMethodCallException('Cannot add attribute to NullToken.');
9395
}

src/Symfony/Component/Security/Core/Authentication/Token/PreAuthenticatedToken.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ class PreAuthenticatedToken extends AbstractToken
2424
private $firewallName;
2525

2626
/**
27-
* @param string|\Stringable|UserInterface $user
28-
* @param mixed $credentials
29-
* @param string[] $roles
27+
* @param string[] $roles
3028
*/
31-
public function __construct($user, $credentials, string $firewallName, array $roles = [])
29+
public function __construct(string|\Stringable|UserInterface $user, mixed $credentials, string $firewallName, array $roles = [])
3230
{
3331
parent::__construct($roles);
3432

src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication\Token;
1313

14+
use Symfony\Component\Security\Core\User\UserInterface;
15+
1416
/**
1517
* Token representing a user who temporarily impersonates another one.
1618
*
@@ -22,13 +24,13 @@ class SwitchUserToken extends UsernamePasswordToken
2224
private $originatedFromUri;
2325

2426
/**
25-
* @param string|object $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method
26-
* @param mixed $credentials This usually is the password of the user
27-
* @param string|null $originatedFromUri The URI where was the user at the switch
27+
* @param $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method
28+
* @param $credentials This usually is the password of the user
29+
* @param $originatedFromUri The URI where was the user at the switch
2830
*
2931
* @throws \InvalidArgumentException
3032
*/
31-
public function __construct($user, $credentials, string $firewallName, array $roles, TokenInterface $originalToken, string $originatedFromUri = null)
33+
public function __construct(string|\Stringable|UserInterface $user, mixed $credentials, string $firewallName, array $roles, TokenInterface $originalToken, string $originatedFromUri = null)
3234
{
3335
parent::__construct($user, $credentials, $firewallName, $roles);
3436

src/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ public function getUser();
5959
* The user can be a UserInterface instance, or an object implementing
6060
* a __toString method or the username as a regular string.
6161
*
62-
* @param string|\Stringable|UserInterface $user
63-
*
6462
* @throws \InvalidArgumentException
6563
*/
66-
public function setUser($user);
64+
public function setUser(string|\Stringable|UserInterface $user);
6765

6866
/**
6967
* Returns whether the user is authenticated or not.
@@ -114,10 +112,8 @@ public function getAttribute(string $name);
114112

115113
/**
116114
* Sets an attribute.
117-
*
118-
* @param mixed $value The attribute value
119115
*/
120-
public function setAttribute(string $name, $value);
116+
public function setAttribute(string $name, mixed $value);
121117

122118
/**
123119
* Returns all the necessary state of the object for serialization purposes.

src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ class UsernamePasswordToken extends AbstractToken
2323
private $credentials;
2424
private $firewallName;
2525

26-
/**
27-
* @param string|\Stringable|UserInterface $user The username (like a nickname, email address, etc.) or a UserInterface instance
28-
* @param mixed $credentials
29-
* @param string[] $roles
30-
*
31-
* @throws \InvalidArgumentException
32-
*/
33-
public function __construct($user, $credentials, string $firewallName, array $roles = [])
26+
public function __construct(string|\Stringable|UserInterface $user, mixed $credentials, string $firewallName, array $roles = [])
3427
{
3528
parent::__construct($roles);
3629

src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public function __construct(iterable $voters = [], string $strategy = self::STRA
5959
*
6060
* {@inheritdoc}
6161
*/
62-
public function decide(TokenInterface $token, array $attributes, $object = null/*, bool $allowMultipleAttributes = false*/)
62+
public function decide(TokenInterface $token, array $attributes, mixed $object = null, bool $allowMultipleAttributes = false)
6363
{
64-
$allowMultipleAttributes = 3 < \func_num_args() && func_get_arg(3);
65-
6664
// Special case for AccessListener, do not remove the right side of the condition before 6.0
6765
if (\count($attributes) > 1 && !$allowMultipleAttributes) {
6866
throw new InvalidArgumentException(sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__));
@@ -77,7 +75,7 @@ public function decide(TokenInterface $token, array $attributes, $object = null/
7775
* If all voters abstained from voting, the decision will be based on the
7876
* allowIfAllAbstainDecisions property value (defaults to false).
7977
*/
80-
private function decideAffirmative(TokenInterface $token, array $attributes, $object = null): bool
78+
private function decideAffirmative(TokenInterface $token, array $attributes, mixed $object = null): bool
8179
{
8280
$deny = 0;
8381
foreach ($this->voters as $voter) {
@@ -115,7 +113,7 @@ private function decideAffirmative(TokenInterface $token, array $attributes, $ob
115113
* If all voters abstained from voting, the decision will be based on the
116114
* allowIfAllAbstainDecisions property value (defaults to false).
117115
*/
118-
private function decideConsensus(TokenInterface $token, array $attributes, $object = null): bool
116+
private function decideConsensus(TokenInterface $token, array $attributes, mixed $object = null): bool
119117
{
120118
$grant = 0;
121119
$deny = 0;
@@ -152,7 +150,7 @@ private function decideConsensus(TokenInterface $token, array $attributes, $obje
152150
* If all voters abstained from voting, the decision will be based on the
153151
* allowIfAllAbstainDecisions property value (defaults to false).
154152
*/
155-
private function decideUnanimous(TokenInterface $token, array $attributes, $object = null): bool
153+
private function decideUnanimous(TokenInterface $token, array $attributes, mixed $object = null): bool
156154
{
157155
$grant = 0;
158156
foreach ($this->voters as $voter) {
@@ -186,7 +184,7 @@ private function decideUnanimous(TokenInterface $token, array $attributes, $obje
186184
* If all voters abstained from voting, the decision will be based on the
187185
* allowIfAllAbstainDecisions property value (defaults to false).
188186
*/
189-
private function decidePriority(TokenInterface $token, array $attributes, $object = null)
187+
private function decidePriority(TokenInterface $token, array $attributes, mixed $object = null)
190188
{
191189
foreach ($this->voters as $voter) {
192190
$result = $voter->vote($token, $object, $attributes);

src/Symfony/Component/Security/Core/Authorization/AccessDecisionManagerInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ interface AccessDecisionManagerInterface
2323
/**
2424
* Decides whether the access is possible or not.
2525
*
26-
* @param array $attributes An array of attributes associated with the method being invoked
27-
* @param object $object The object to secure
26+
* @param $attributes An array of attributes associated with the method being invoked
27+
* @param $object The object to secure
2828
*
2929
* @return bool true if the access is granted, false otherwise
3030
*/
31-
public function decide(TokenInterface $token, array $attributes, $object = null);
31+
public function decide(TokenInterface $token, array $attributes, mixed $object = null);
3232
}

src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(TokenStorageInterface $tokenStorage, AuthenticationM
4646
*
4747
* @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token and $exceptionOnNoToken is set to true
4848
*/
49-
final public function isGranted($attribute, $subject = null): bool
49+
final public function isGranted(mixed $attribute, mixed $subject = null): bool
5050
{
5151
if (null === ($token = $this->tokenStorage->getToken())) {
5252
if ($this->exceptionOnNoToken) {

0 commit comments

Comments
 (0)