Skip to content

Commit

Permalink
Update authbucket records on username change
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrulez committed Mar 23, 2016
1 parent 31a5865 commit 7cdd214
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use AppBundle\Common\Result;
use AppBundle\Common\ErrorCode;
use AppBundle\Entity\User;
use AppBundle\Event\UsernameChangeEvent;

/**
* @Route("/api/resource/users")
Expand Down Expand Up @@ -97,6 +98,7 @@ public function setUsernameAction(Request $request)
try
{

$oldUsername = $user->getUsername();
$user->setUsername($username)
->setUpdatedAt(new \DateTime('now'));

Expand All @@ -106,6 +108,16 @@ public function setUsernameAction(Request $request)

$result->setData($user, ['password', 'salt']);

try
{
$event = new UsernameChangeEvent($user, $oldUsername);
$eventDispatcher = $this->get('event_dispatcher');
$eventDispatcher->dispatch(UsernameChangeEvent::USERNAME_CHANGE, $event);
}catch(\Exception $e)
{
$this->get('logger')->error($e->getMessage());
}

return new ApiResponse($result);
}catch(\Exception $e)
{
Expand Down
29 changes: 29 additions & 0 deletions src/AppBundle/Event/UsernameChangeEvent.php
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;
}
}
82 changes: 82 additions & 0 deletions src/AppBundle/EventListener/UsernameChangeListener.php
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());
}
}
}
7 changes: 7 additions & 0 deletions src/AppBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ services:
arguments:
- "@service_container"

app.username_change_listener:
class: AppBundle\EventListener\UsernameChangeListener
tags:
- { name: kernel.event_listener, event: app.username_change, method: onComplete, priority: -1 }
arguments:
- "@service_container"

date_time_normalizer:
class: AppBundle\Normalizer\DateTimeNormalizer
tags:
Expand Down

0 comments on commit 7cdd214

Please sign in to comment.