-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update authbucket records on username change
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace AppBundle\Event; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class UsernameChangeEvent extends Event | ||
{ | ||
const USERNAME_CHANGE = 'app.username_change'; | ||
|
||
protected $user; | ||
protected $oldUsername; | ||
|
||
public function __construct($user, $oldUsername) | ||
{ | ||
$this->user = $user; | ||
$this->oldUsername = $oldUsername; | ||
} | ||
|
||
public function getUser() | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function getOldUsername() | ||
{ | ||
return $this->oldUsername; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace AppBundle\EventListener; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use AppBundle\Event\UsernameChangeEvent; | ||
|
||
class UsernameChangeListener | ||
{ | ||
protected $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function onComplete(UsernameChangeEvent $event) | ||
{ | ||
$logger = $this->container->get('logger'); | ||
|
||
try | ||
{ | ||
$user = $event->getUser(); | ||
$oldUsername = $event->getOldUsername(); | ||
$em = $this->container->get('doctrine.orm.entity_manager'); | ||
|
||
$accessTokens = $em->getRepository('AppBundle:AccessToken')->findBy(['username' => $oldUsername]); | ||
|
||
if($accessTokens != null) | ||
{ | ||
foreach($accessTokens as $accessToken) | ||
{ | ||
$accessToken->setUsername($user->getUsername()); | ||
|
||
$em->persist($accessToken); | ||
} | ||
} | ||
|
||
$refreshTokens = $em->getRepository('AppBundle:RefreshToken')->findBy(['username' => $oldUsername]); | ||
|
||
if($refreshTokens != null) | ||
{ | ||
foreach($refreshTokens as $refreshToken) | ||
{ | ||
$refreshToken->setUsername($user->getUsername()); | ||
|
||
$em->persist($refreshToken); | ||
} | ||
} | ||
|
||
$authorizations = $em->getRepository('AppBundle:Authorize')->findBy(['username' => $oldUsername]); | ||
|
||
if($authorizations != null) | ||
{ | ||
foreach($authorizations as $authorization) | ||
{ | ||
$authorization->setUsername($user->getUsername()); | ||
|
||
$em->persist($authorization); | ||
} | ||
} | ||
|
||
$authCodes = $em->getRepository('AppBundle:Code')->findBy(['username' => $oldUsername]); | ||
|
||
if($authCodes != null) | ||
{ | ||
foreach($authCodes as $authCode) | ||
{ | ||
$authCode->setUsername($user->getUsername()); | ||
|
||
$em->persist($authCode); | ||
} | ||
} | ||
|
||
$em->flush(); | ||
|
||
}catch(\Exception $e) | ||
{ | ||
$logger->error($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters