Last active
August 29, 2015 13:56
-
-
Save bezhermoso/9305705 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace App\ShopBundle\Security\Authentication\Provider; | |
use App\ShopBundle\Entity\Order; | |
use App\ShopBundle\Model\OrderManager; | |
use App\ShopBundle\Security\SessionToken; | |
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
class SessionProvider implements AuthenticationProviderInterface | |
{ | |
/** | |
* @var OrderManager | |
*/ | |
protected $orderManager; | |
/** | |
* @param OrderManager $orderManager | |
*/ | |
public function __construct(OrderManager $orderManager) | |
{ | |
$this->orderManager = $orderManager; | |
} | |
/** | |
* Attempts to authenticate a TokenInterface object. | |
* | |
* @param TokenInterface $token The TokenInterface instance to authenticate | |
* | |
* @return TokenInterface An authenticated TokenInterface instance, never null | |
* | |
* @throws AuthenticationException if the authentication fails | |
*/ | |
public function authenticate(TokenInterface $token) | |
{ | |
/** @var $token SessionToken */ | |
$order = $this->orderManager | |
->findOneOrderBy(array( | |
'session' => $token->getSessionId(), | |
'status' => Order::STATUS_INITIATED | |
)); | |
if ($order) { | |
$token->setOrder($order); | |
} | |
return $token; | |
throw new AuthenticationException('No related orders.'); | |
} | |
/** | |
* Checks whether this provider supports the given token. | |
* | |
* @param TokenInterface $token A TokenInterface instance | |
* | |
* @return Boolean true if the implementation supports the Token, false otherwise | |
*/ | |
public function supports(TokenInterface $token) | |
{ | |
return ($token instanceof SessionToken); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\ShopBundle\Security; | |
use App\UserBundle\Entity\User; | |
class SessionToken extends ShopToken | |
{ | |
public function __construct($sessionId) | |
{ | |
parent::__construct(array('SHOPPING_CART')); | |
$this->setAttribute('session_id', $sessionId); | |
} | |
/** | |
* Returns the user credentials. | |
* | |
* @return mixed The user credentials | |
*/ | |
public function getCredentials() | |
{ | |
return $this->getAttribute('session_id'); | |
} | |
public function getSessionId() | |
{ | |
return $this->getAttribute('session_id'); | |
} | |
public function getUser() | |
{ | |
if (!$this->order) { | |
return null; | |
} | |
if ($this->order->getCustomer()) { | |
return $this->order->getCustomer(); | |
} | |
$user = new User(); | |
$user->setFirstName($this->order->getFirstName()); | |
$user->setLastName($this->order->getLastName()); | |
$user->setEmail($this->order->getCustomerEmail()); | |
$user->setCountry($this->order->getCountry()); | |
return $user; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\ShopBundle\Security\Authentication\Listener; | |
use App\ShopBundle\Model\OrderManager; | |
use App\ShopBundle\Security\SessionToken; | |
use App\ShopBundle\Security\ShopToken; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\Routing\RouterInterface; | |
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
use Symfony\Component\Security\Core\SecurityContextInterface; | |
use Symfony\Component\Security\Http\Firewall\ListenerInterface; | |
class ShopAuthenticationListener implements ListenerInterface | |
{ | |
/** | |
* @var \Symfony\Component\Security\Core\SecurityContextInterface | |
*/ | |
protected $security; | |
/** | |
* @var \Sftv\ShopBundle\Model\OrderManager | |
*/ | |
protected $orderManager; | |
/** | |
* @var \Symfony\Component\Routing\RouterInterface | |
*/ | |
protected $router; | |
/** | |
* @var \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface | |
*/ | |
protected $authManager; | |
/** | |
* @param SecurityContextInterface $security | |
* @param OrderManager $orderManager | |
* @param \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface $authManager | |
* @param RouterInterface $router | |
*/ | |
public function __construct(SecurityContextInterface $security, OrderManager $orderManager, AuthenticationManagerInterface $authManager, RouterInterface $router) | |
{ | |
$this->security = $security; | |
$this->orderManager = $orderManager; | |
$this->router = $router; | |
$this->authManager = $authManager; | |
} | |
/** | |
* This interface must be implemented by firewall listeners. | |
* | |
* @param GetResponseEvent $event | |
* @return \Symfony\Component\HttpFoundation\RedirectResponse | |
*/ | |
public function handle(GetResponseEvent $event) | |
{ | |
$token = new SessionToken($event->getRequest()->getSession()->getId()); | |
try { | |
$token = $this->authManager->authenticate($token); | |
$this->security->setToken($token); | |
return; | |
} catch (AuthenticationException $e) { | |
$response = new RedirectResponse($this->router->generate('shop_account_creation', $event->getRequest()->get('_route_params'))); | |
$event->setResponse($response); | |
} | |
$response = new RedirectResponse($this->router->generate('shop_account_creation', $event->getRequest()->get('_route_params'))); | |
$event->setResponse($response); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\ShopBundle\DependencyInjection\Security\Factory; | |
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface; | |
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Definition; | |
use Symfony\Component\DependencyInjection\DefinitionDecorator; | |
class ShopFactory implements SecurityFactoryInterface | |
{ | |
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint) | |
{ | |
$providerId = 'security.authentication.provider.shop_haut.' . $id; | |
$container->setDefinition($providerId, new DefinitionDecorator('shop_auth.security.auth_provider')); | |
$listenerId = 'security.authentication.listener.shop_auth.' . $id; | |
$container->setDefinition($listenerId, new DefinitionDecorator('shop_auth.security.auth_listener')); | |
return array($providerId, $listenerId, $defaultEntryPoint); | |
} | |
public function getPosition() | |
{ | |
return 'pre_auth'; | |
} | |
public function getKey() | |
{ | |
return 'shop_auth'; | |
} | |
public function addConfiguration(NodeDefinition $builder) | |
{ | |
// TODO: Implement addConfiguration() method. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment