-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[SecurityBundle] error helper added symfony/symfony#11147 #11324
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,87 @@ | ||
<?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\Http\Authentication; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\SecurityContextInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Extracts Security Errors from Request | ||
* | ||
* @author Boris Vujicic <boris.vujicic@gmail.com> | ||
*/ | ||
class AuthenticationUtils | ||
{ | ||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @param RequestStack $requestStack | ||
*/ | ||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
/** | ||
* @param bool $clearSession | ||
* @return null|AuthenticationException | ||
*/ | ||
public function getLastAuthenticationError($clearSession = true) | ||
{ | ||
$request = $this->getRequest(); | ||
$session = $request->getSession(); | ||
$authenticationException = null; | ||
|
||
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { | ||
$authenticationException = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR); | ||
} elseif ($session !== null && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { | ||
$authenticationException = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR); | ||
|
||
if ($clearSession) { | ||
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR); | ||
} | ||
|
||
} | ||
|
||
return $authenticationException; | ||
} | ||
|
||
/** | ||
* @return string | ||
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. here too 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 thought method name is self explanatory. 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. For me the method name is self explanatory. Let's avoid comments like 'Gets last username' for a method 'getLastUsername'. |
||
*/ | ||
public function getLastUsername() | ||
{ | ||
$session = $this->getRequest()->getSession(); | ||
|
||
return null === $session ? '' : $session->get(SecurityContextInterface::LAST_USERNAME); | ||
} | ||
|
||
/** | ||
* @return Request | ||
* @throws \LogicException | ||
*/ | ||
private function getRequest() | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
if (null === $request) { | ||
throw new \LogicException('Request should exist so it can be processed for error.'); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
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.
I think it's more developer friendly to provide some more documentation comments here.
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.
Any suggestion on what it should be?
"Retrieves last Authentication Error, if $clearSession is true it will also remove the error form session."?
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.
"Retrieves last Authentication Error, if $clearSession is true it will also remove the error form session." - this can be read from the code and I don't think we need duplicating it in a comment.
@xabbuh could you explain what kind of a documentation you mean here?
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.
I would write something like this:
I know that the method name is quite self-explanatory, but when I look at the generated API documentation I find it useful if there is some more explanation.
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.
I'm against documenting WHAT the code is doing. It's redundant, since you can read what the code is doing from the code itself... Also, this kind of comments often lie, since people tend to forget to update them.
We should rather explain WHY we're doing something, or document potential pitfalls. Anything that's not self-evident from reading the code.
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.
Sure, you can have a look at the code. I just think that this isn't a nice experience from the user's (the developer that uses Symfony) point of view. I feel that the best experience is when one just reads the API documentation and you do know how to use a particular class and which implications its usage has.
Given this method, it's useful to know what the
clearSession
argument is used for, why I should set it tofalse
and what it'll imply if I do that (so maybe my suggestion doesn't fit right how it should be done).Though I'm not sure if this pull request is the right place to discuss this topic since it's somehow is related to the API documentation as a whole, isn't it?
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.
You wouldn't be able to use Symfony's API docs this way, as it hardly contains comments of that kind. You're right it starts going out of the scope for this PR ;)