-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security][SecurityBundle] User authorization checker #48142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authentication\Token; | ||
|
||
/** | ||
* Interface used for marking tokens that do not represent the currently logged-in user. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
interface OfflineTokenInterface extends TokenInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the word offline is weird as first PR read
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like offline is still the most understandable word here, but open to suggestions. Token in my head is the representation of the user, and in this context they would be offline as far as the app is concerned. I could maybe see something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm personally fine with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about one of these?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SessionlessToken sounds best to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SessionLessToken and StatelessToken are both confusing, because the token of the current user stored in the TokenStorage might also be session-less (if your configure your firewall as stateless, it does not store the current token in the session). I would also keep |
||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authentication\Token; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* UserAuthorizationCheckerToken implements a token used for checking authorization. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
* | ||
* @internal | ||
*/ | ||
final class UserAuthorizationCheckerToken extends AbstractToken implements OfflineTokenInterface | ||
{ | ||
public function __construct(UserInterface $user) | ||
{ | ||
parent::__construct($user->getRoles()); | ||
|
||
$this->setUser($user); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\UserAuthorizationCheckerToken; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
final class UserAuthorizationChecker implements UserAuthorizationCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly AccessDecisionManagerInterface $accessDecisionManager, | ||
) { | ||
} | ||
|
||
public function userIsGranted(UserInterface $user, mixed $attribute, mixed $subject = null): bool | ||
{ | ||
return $this->accessDecisionManager->decide(new UserAuthorizationCheckerToken($user), [$attribute], $subject); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* Interface is used to check user authorization without a session. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
interface UserAuthorizationCheckerInterface | ||
{ | ||
/** | ||
* Checks if the attribute is granted against the user and optionally supplied subject. | ||
* | ||
* @param mixed $attribute A single attribute to vote on (can be of any type, string and instance of Expression are supported by the core) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cant use union type here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches |
||
*/ | ||
public function userIsGranted(UserInterface $user, mixed $attribute, mixed $subject = null): bool; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Tests\Authentication\Token; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\UserAuthorizationCheckerToken; | ||
use Symfony\Component\Security\Core\User\InMemoryUser; | ||
|
||
class UserAuthorizationCheckerTokenTest extends TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$token = new UserAuthorizationCheckerToken($user = new InMemoryUser('foo', 'bar', ['ROLE_FOO'])); | ||
$this->assertSame(['ROLE_FOO'], $token->getRoleNames()); | ||
$this->assertSame($user, $token->getUser()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isUserGranted no ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 discussed internally, either this or
isGrantedForUser()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #59214