Skip to content
This repository has been archived by the owner on Aug 17, 2022. It is now read-only.

Commit

Permalink
utilise autowiring; refactored DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Guite committed Mar 16, 2019
1 parent 05282ac commit 7d79d55
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 126 deletions.
46 changes: 30 additions & 16 deletions AuthenticationMethod/AbstractAuthenticationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Zikula\Common\Translator\TranslatorInterface;
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
use Zikula\ExtensionsModule\Api\VariableApi;
use Zikula\OAuthModule\Entity\MappingEntity;
Expand All @@ -26,6 +27,11 @@

abstract class AbstractAuthenticationMethod implements ReEntrantAuthenticationMethodInterface
{
/**
* @var TranslatorInterface
*/
protected $translator;

/**
* @var Session
*/
Expand Down Expand Up @@ -68,18 +74,22 @@ abstract class AbstractAuthenticationMethod implements ReEntrantAuthenticationMe

/**
* AbstractAuthenticationMethod constructor.
* @param Session $session
*
* @param TranslatorInterface $translator
* @param RequestStack $requestStack
* @param RouterInterface $router
* @param MappingRepository $repository
* @param VariableApi $variableApi
*/
public function __construct(Session $session, RequestStack $requestStack, RouterInterface $router, MappingRepository $repository, VariableApi $variableApi)
{
if (version_compare(ZikulaKernel::VERSION, '1.4.3', '>=') && version_compare(ZikulaKernel::VERSION, '1.5.0', '<')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
$this->session = $session;
public function __construct(
TranslatorInterface $translator,
RequestStack $requestStack,
RouterInterface $router,
MappingRepository $repository,
VariableApi $variableApi
) {
$this->translator = $translator;
$this->session = $requestStack->getCurrentRequest()->getSession();
$this->requestStack = $requestStack;
$this->router = $router;
$this->repository = $repository;
Expand Down Expand Up @@ -136,7 +146,7 @@ public function authenticate(array $data = [])
header('Location: ' . $authUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($state) || ($state !== $this->session->get('oauth2state'))) {
$this->session->remove('oauth2state');
$this->session->getFlashBag()->add('error', 'Invalid State');
Expand All @@ -158,16 +168,20 @@ public function authenticate(array $data = [])
} else {
$registrationUrl = $this->router->generate('zikulausersmodule_registration_register');
$this->session->remove('oauth2state');
$this->session->getFlashBag()->add('error', sprintf(
'This user is not locally registered. You must first %s on this site before logging in with %s',
'<a href=\'' . $registrationUrl . '\'>create a new account</a>',
$this->getDisplayName()
));
$registerLink = '<a href="' . $registrationUrl . '">' . $this->translator->__('create a new account') . '</a>';
$this->session->getFlashBag()->add('error',
$this->translator->__f(
'This user is not locally registered. You must first %registerLink on this site before logging in with %displayName', [
'%registerLink' => $registerLink,
'%displayName' => $this->getDisplayName()
]
)
);
}

return $uid;
} catch (\Exception $e) {
$this->session->getFlashBag()->add('error', 'Could not obtain user details from Github. (' . $e->getMessage() . ')');
} catch (\Exception $exception) {
$this->session->getFlashBag()->add('error', $this->translator->__('Could not obtain user details from external service.') . ' (' . $exception->getMessage() . ')');

return null;
}
Expand All @@ -177,7 +191,7 @@ public function authenticate(array $data = [])
public function getId()
{
if (!$this->user) {
throw new \LogicException('User must authenticate first.');
throw new \LogicException($this->translator->__('User must authenticate first.'));
}

return $this->user->getId();
Expand Down
8 changes: 4 additions & 4 deletions AuthenticationMethod/FacebookAuthenticationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function getAlias()

public function getDisplayName()
{
return 'Facebook';
return $this->translator->__('Facebook');
}

public function getDescription()
{
return 'Login using Facebook via OAuth.';
return $this->translator->__('Login using Facebook via OAuth.');
}

public function getUname()
Expand All @@ -46,14 +46,14 @@ protected function setProvider($redirectUri)
{
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_FACEBOOK);
if (!isset($settings['id']) || !isset($settings['secret'])) {
throw new InvalidProviderConfigException('Invalid settings for Facebook OAuth provider.');
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Facebook OAuth provider.'));
}

$this->provider = new Facebook([
'clientId' => $settings['id'],
'clientSecret' => $settings['secret'],
'redirectUri' => $redirectUri,
'graphApiVersion' => 'v2.9',
'graphApiVersion' => 'v2.9'
]);
}
}
8 changes: 4 additions & 4 deletions AuthenticationMethod/GithubAuthenticationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function getAlias()

public function getDisplayName()
{
return 'Github';
return $this->translator->__('Github');
}

public function getDescription()
{
return 'Login using Github via OAuth.';
return $this->translator->__('Login using Github via OAuth.');
}

public function getUname()
Expand Down Expand Up @@ -77,13 +77,13 @@ protected function setProvider($redirectUri)
{
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GITHUB);
if (!isset($settings['id']) || !isset($settings['secret'])) {
throw new InvalidProviderConfigException('Invalid settings for Github OAuth provider.');
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Github OAuth provider.'));
}

$this->provider = new Github([
'clientId' => $settings['id'],
'clientSecret' => $settings['secret'],
'redirectUri' => $redirectUri,
'redirectUri' => $redirectUri
]);
}
}
8 changes: 4 additions & 4 deletions AuthenticationMethod/GoogleAuthenticationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function getAlias()

public function getDisplayName()
{
return 'Google';
return $this->translator->__('Google');
}

public function getDescription()
{
return 'Login using Google via OAuth.';
return $this->translator->__('Login using Google via OAuth.');
}

public function getUname()
Expand All @@ -46,13 +46,13 @@ protected function setProvider($redirectUri)
{
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GOOGLE);
if (!isset($settings['id']) || !isset($settings['secret'])) {
throw new InvalidProviderConfigException('Invalid settings for Google OAuth provider.');
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Google OAuth provider.'));
}

$this->provider = new Google([
'clientId' => $settings['id'],
'clientSecret' => $settings['secret'],
'redirectUri' => $redirectUri,
'redirectUri' => $redirectUri
]);
}
}
8 changes: 4 additions & 4 deletions AuthenticationMethod/LinkedInAuthenticationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function getAlias()

public function getDisplayName()
{
return 'LinkedIn';
return $this->translator->__('LinkedIn');
}

public function getDescription()
{
return 'Login using LinkedIn via OAuth.';
return $this->translator->__('Login using LinkedIn via OAuth.');
}

public function getUname()
Expand All @@ -46,13 +46,13 @@ protected function setProvider($redirectUri)
{
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_LINKEDIN);
if (!isset($settings['id']) || !isset($settings['secret'])) {
throw new InvalidProviderConfigException('Invalid settings for LinkedIn OAuth provider.');
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for LinkedIn OAuth provider.'));
}

$this->provider = new LinkedIn([
'clientId' => $settings['id'],
'clientSecret' => $settings['secret'],
'redirectUri' => $redirectUri,
'redirectUri' => $redirectUri
]);
}
}
23 changes: 14 additions & 9 deletions Controller/ConfigController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
// <?php

/*
* This file is part of the Zikula package.
Expand All @@ -11,12 +11,14 @@

namespace Zikula\OAuthModule\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Zikula\Core\Controller\AbstractController;
use Zikula\OAuthModule\Form\Type\SettingsType;
use Zikula\ThemeModule\Engine\Annotation\Theme;
use Zikula\UsersModule\Collector\AuthenticationMethodCollector;

/**
* Class ConfigController
Expand All @@ -27,20 +29,23 @@ class ConfigController extends AbstractController
* @Route("/settings/{method}")
* @Theme("admin")
* @Template("ZikulaOAuthModule:Config:settings.html.twig")
*
* @param Request $request
* @param AuthenticationMethodCollector $authenticationMethodCollector
* @param string $method
*
* @return array
*/
public function settingsAction(Request $request, $method = 'github')
{
public function settingsAction(
Request $request,
AuthenticationMethodCollector $authenticationMethodCollector,
$method = 'github'
) {
if (!$this->hasPermission('ZikulaOAuthModule', '::', ACCESS_ADMIN)) {
throw new AccessDeniedException();
}

$form = $this->createForm('Zikula\OAuthModule\Form\Type\SettingsType', $this->getVar($method, []), [
'translator' => $this->getTranslator()
]);

$form = $this->createForm(SettingsType::class, $this->getVar($method, []));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->get('save')->isClicked()) {
Expand All @@ -51,7 +56,7 @@ public function settingsAction(Request $request, $method = 'github')

return [
'form' => $form->createView(),
'method' => $this->get('zikula_users_module.internal.authentication_method_collector')->get($method)
'method' => $authenticationMethodCollector->get($method)
];
}
}
8 changes: 5 additions & 3 deletions Controller/MappingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Zikula\Core\Controller\AbstractController;
use Zikula\ThemeModule\Engine\Annotation\Theme;
use Zikula\OAuthModule\Entity\Repository\MappingRepository;

/**
* Class MappingController
Expand All @@ -26,16 +27,17 @@ class MappingController extends AbstractController
* @Route("/list")
* @Template("ZikulaOAuthModule:Mapping:list.html.twig")
* @Theme("admin")
*
* @param MappingRepository $mappingRepository
*/
public function listAction()
public function listAction(MappingRepository $mappingRepository)
{
if (!$this->hasPermission('ZikulaOAuthModule', '::', ACCESS_ADMIN)) {
throw new AccessDeniedException();
}
$mappings = $this->get('zikula_oauth_module.mapping_repository')->findAll();

return [
'mappings' => $mappings
'mappings' => $mappingRepository->findAll()
];
}
}
16 changes: 9 additions & 7 deletions Entity/Repository/MappingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@

namespace Zikula\OAuthModule\Entity\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Zikula\OAuthModule\Entity\MappingEntity;

class MappingRepository extends EntityRepository
class MappingRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, MappingEntity::class);
}

public function getZikulaId($method, $methodId)
{
$mapping = parent::findOneBy(['method' => $method, 'methodId' => $methodId]);

if (isset($mapping)) {
return $mapping->getZikulaId();
} else {
return null;
}
return isset($mapping) ? $mapping->getZikulaId() : null;
}

public function persistAndFlush(MappingEntity $entity)
Expand Down
Loading

0 comments on commit 7d79d55

Please sign in to comment.