-
Notifications
You must be signed in to change notification settings - Fork 21
Implement ability for users to register custom callbacks via service definition tags. #36
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
base: master
Are you sure you want to change the base?
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,80 @@ | ||
<?php | ||
|
||
namespace Bugsnag\BugsnagBundle\Callbacks; | ||
|
||
use Bugsnag\Report; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class UserSettingCallback | ||
{ | ||
/** | ||
* The token resolver. | ||
* | ||
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null | ||
*/ | ||
protected $tokens; | ||
|
||
/** | ||
* The auth checker. | ||
* | ||
* @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null | ||
*/ | ||
protected $checker; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $setUser; | ||
|
||
/** | ||
* @param null|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokens | ||
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. Please put |
||
* @param null|\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker | ||
* @param bool $setUser | ||
* | ||
* @return void | ||
*/ | ||
public function __construct( | ||
TokenStorageInterface $tokens = null, | ||
AuthorizationCheckerInterface $checker = null, | ||
$setUser = true | ||
) { | ||
$this->tokens = $tokens; | ||
$this->checker = $checker; | ||
$this->setUser = $setUser; | ||
} | ||
|
||
/** | ||
* Define a callback to set the currently authenticated user as the user | ||
* on any Bugsnag reports that are sent. | ||
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. Please change to: /**
* Define a callback to set the currently authenticated user.
*
* @param \Bugsnag\Report $report
*
* @return void
*/ This is because "short descriptions" cannot span more than 1 line, and we also apply the extra cs constraint that they cannot be longer than 80 characters. |
||
* | ||
* @param \Bugsnag\Report $report | ||
* | ||
* @return void | ||
*/ | ||
public function registerCallback(Report $report) | ||
{ | ||
// If told to not set the user, or the security services were not passed in | ||
// (not registered in the container), then exit early | ||
if (!$this->setUser || is_null($this->tokens) || is_null($this->checker)) { | ||
return; | ||
} | ||
|
||
$token = $this->tokens->getToken(); | ||
|
||
if (!$token || !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { | ||
return; | ||
} | ||
|
||
$user = $token->getUser(); | ||
|
||
if ($user instanceof UserInterface) { | ||
$bugsnagUser = ['id' => $user->getUsername()]; | ||
} else { | ||
$bugsnagUser = ['id' => (string) $user]; | ||
} | ||
|
||
$report->setUser($bugsnagUser); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Bugsnag\BugsnagBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class CallbackRegisteringPass implements CompilerPassInterface | ||
{ | ||
const BUGSNAG_SERVICE_NAME = 'bugsnag'; | ||
const TAG_NAME = 'bugsnag.callback'; | ||
|
||
/** | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
* | ||
* @return void | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->has(self::BUGSNAG_SERVICE_NAME)) { | ||
return; | ||
} | ||
|
||
// Get the Bugsnag service | ||
$bugsnag = $container->findDefinition(self::BUGSNAG_SERVICE_NAME); | ||
|
||
// Get all services tagged as a callback | ||
$callbackServices = $container->findTaggedServiceIds(self::TAG_NAME); | ||
|
||
// Register each callback on the bugsnag service | ||
foreach ($callbackServices as $id => $tags) { | ||
foreach ($tags as $attributes) { | ||
// Get the method name to call on the service from the tag definition, | ||
// defaulting to registerCallback | ||
$method = isset($attributes['method']) ? $attributes['method'] : 'registerCallback'; | ||
$bugsnag->addMethodCall('registerCallback', [[new Reference($id), $method]]); | ||
} | ||
} | ||
} | ||
} |
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.
Please add a description to this doc, similar to the others in the project.