Skip to content

Commit

Permalink
Reduce reliance on ShopifyStoreInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Richtermeister committed Oct 1, 2017
1 parent e7cc7bf commit f2288eb
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 101 deletions.
13 changes: 6 additions & 7 deletions src/Command/WebhooksCommand.php
Expand Up @@ -54,20 +54,18 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$store = $this->storeManager->findStoreByName($input->getArgument('store'));

if (!$store) {
throw new StoreNotFoundException($input->getArgument('store'));
if (!$this->storeManager->storeExists($store = $input->getArgument('store'))) {
throw new StoreNotFoundException($store);
}

if ($input->getOption('list')) {
$output->writeln(print_r($this->webhookCreator->listWebhooks($store->getStoreName()), true));
$output->writeln(print_r($this->webhookCreator->listWebhooks($store), true));

return;
}

if ($input->getOption('delete')) {
$this->webhookCreator->deleteAllWebhooks($store->getStoreName());
$this->webhookCreator->deleteAllWebhooks($store);
$output->writeln('Webhooks deleted');

return;
Expand All @@ -77,7 +75,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \LogicException('No webhook topics configured');
}

$this->webhookCreator->createWebhooks($store->getStoreName(), $this->topics);
$this->webhookCreator->createWebhooks($store, $this->topics);

$output->writeln('Webhooks created');
}
}
2 changes: 1 addition & 1 deletion src/Controller/WebhookController.php
Expand Up @@ -44,7 +44,7 @@ public function handleWebhook(Request $request)
throw new NotFoundHttpException();
}

if (!$store = $this->storeManager->findStoreByName($storeName)) {
if (!$this->storeManager->storeExists($storeName)) {
throw new NotFoundHttpException();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Model/ShopifyStoreManagerInterface.php
Expand Up @@ -13,6 +13,12 @@ interface ShopifyStoreManagerInterface
*/
public function findStoreByName($storeName);

/**
* @param string $storeName
* @return bool
*/
public function storeExists($storeName): bool;

/**
* This method is called when a store initiates authentication via Shopify OAuth.
*
Expand Down
7 changes: 3 additions & 4 deletions src/Resources/config/services.yml
Expand Up @@ -54,12 +54,11 @@ services:
arguments:
- "%codecloud_shopify.oauth.shared_secret%"

codecloud_shopify.oauth.authenticator:
codecloud_shopify.security.session_authenticator:
public: false
class: CodeCloud\Bundle\ShopifyBundle\Security\OAuthAuthenticator
class: CodeCloud\Bundle\ShopifyBundle\Security\SessionAuthenticator
arguments:
- "@codecloud_shopify.signer"
- "@codecloud_shopify.store_manager"
- "@router"

codecloud_shopify.provider.store:
public: false
Expand Down
89 changes: 0 additions & 89 deletions src/Security/OAuthAuthenticator.php

This file was deleted.

84 changes: 84 additions & 0 deletions src/Security/SessionAuthenticator.php
@@ -0,0 +1,84 @@
<?php

namespace CodeCloud\Bundle\ShopifyBundle\Security;

use CodeCloud\Bundle\ShopifyBundle\EventListener\SessionAuthenticationListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
* Authenticates users via session parameter.
*/
class SessionAuthenticator extends AbstractGuardAuthenticator
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;

/**
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

public function getCredentials(Request $request)
{
if (!$session = $request->getSession()) {
return false;
}

if (!$session->has(SessionAuthenticationListener::SESSION_PARAMETER)) {
return false;
}

return [
'shop' => $session->get(SessionAuthenticationListener::SESSION_PARAMETER),
];
}

public function getUser($credentials, UserProviderInterface $userProvider)
{
return $userProvider->loadUserByUsername($credentials['shop']);
}

public function checkCredentials($credentials, UserInterface $user)
{
return true;
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
}

public function supportsRememberMe()
{
return false;
}

public function start(Request $request, AuthenticationException $authException = null)
{
if (!$request->get('shop')) {
return new Response('Your session has expired. Please access the app via Shopify Admin again.');
}

return new RedirectResponse(
$this->urlGenerator->generate('codecloud_shopify_auth', [
'shop' => $request->get('shop'),
])
);
}
}

0 comments on commit f2288eb

Please sign in to comment.