Commit 9d33e749 authored by Tino Goratsch's avatar Tino Goratsch

- added a CaptchaRegistrar class to make the captcha providers pluggable

- added stub controller actions for the configurability of the captchas
parent 6e3ed60a
......@@ -69,7 +69,7 @@ class Settings implements SettingsInterface
*
* @return bool
*/
public function saveSettings($data, $module)
public function saveSettings(array $data, $module)
{
$bool = $bool2 = false;
$moduleId = $this->systemModuleRepository->getModuleId($module);
......
......@@ -25,5 +25,5 @@ interface SettingsInterface
*
* @return bool
*/
public function saveSettings($data, $module);
public function saveSettings(array $data, $module);
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Controller\Admin\Index;
use ACP3\Core\Controller\AbstractAdminAction;
class Index extends AbstractAdminAction
{
public function execute()
{
}
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Controller\Admin\Index;
use ACP3\Core\Controller\AbstractAdminAction;
use ACP3\Modules\ACP3\Captcha\Installer\Schema;
class Settings extends AbstractAdminAction
{
/**
* @return array
*/
public function execute()
{
$settings = $this->config->getSettings(Schema::MODULE_NAME);
return [
'form' => array_merge($settings, $this->request->getPost()->all())
];
}
/**
* @return array|string|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
return $this->config->saveSettings($formData, Schema::MODULE_NAME);
});
}
}
......@@ -4,14 +4,10 @@
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Controller\Index;
namespace ACP3\Modules\ACP3\Captcha\Controller\Frontend\Index;
use ACP3\Core;
/**
* Class Image
* @package ACP3\Modules\ACP3\Captcha\Controller\Index
*/
class Image extends Core\Controller\AbstractFrontendAction
{
/**
......
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class CaptchaCompilerPass implements CompilerPassInterface
{
/**
* @inheritdoc
*/
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('captcha.utility.captcha_registrar');
$plugins = $container->findTaggedServiceIds('captcha.extension.captcha');
foreach ($plugins as $serviceId => $tags) {
$definition->addMethodCall(
'registerCaptcha',
[new Reference($serviceId)]
);
}
}
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Extension;
interface CaptchaExtensionInterface
{
const CAPTCHA_DEFAULT_LENGTH = 5;
const CAPTCHA_DEFAULT_INPUT_ID = 'captcha';
/**
* @return string
*/
public function getCaptchaName();
/**
* Creates and returns the view of the captcha
*
* @param integer $captchaLength
* @param string $formFieldId
* @param bool $inputOnly
* @param string $path
*
* @return string
*/
public function getCaptcha(
$captchaLength = self::CAPTCHA_DEFAULT_LENGTH,
$formFieldId = self::CAPTCHA_DEFAULT_INPUT_ID,
$inputOnly = false,
$path = ''
);
/**
* Returns the name of the to be used validation rule
*
* @return string
*/
public function getValidationRule();
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Extension;
use ACP3\Core;
use ACP3\Core\I18n\Translator;
class NativeCaptchaExtension implements CaptchaExtensionInterface
{
/**
* @var Translator
*/
private $translator;
/**
* @var \ACP3\Core\Helpers\Secure
*/
private $secureHelper;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
private $request;
/**
* @var \ACP3\Core\Router\RouterInterface
*/
private $router;
/**
* @var \ACP3\Core\Session\SessionHandlerInterface
*/
private $sessionHandler;
/**
* @var \ACP3\Core\View
*/
private $view;
/**
* @var \ACP3\Modules\ACP3\Users\Model\UserModel
*/
private $user;
/**
* NativeCaptchaExtension constructor.
* @param Translator $translator
* @param \ACP3\Modules\ACP3\Users\Model\UserModel $user
* @param Core\Http\RequestInterface $request
* @param Core\Router\RouterInterface $router
* @param Core\Session\SessionHandlerInterface $sessionHandler
* @param Core\View $view
* @param Core\Helpers\Secure $secureHelper
*/
public function __construct(
Translator $translator,
Core\Http\RequestInterface $request,
Core\Router\RouterInterface $router,
Core\Session\SessionHandlerInterface $sessionHandler,
Core\View $view,
Core\Helpers\Secure $secureHelper,
\ACP3\Modules\ACP3\Users\Model\UserModel $user
) {
$this->translator = $translator;
$this->request = $request;
$this->router = $router;
$this->sessionHandler = $sessionHandler;
$this->view = $view;
$this->secureHelper = $secureHelper;
$this->user = $user;
}
/**
* @return string
*/
public function getCaptchaName()
{
return $this->translator->t('captcha', 'native');
}
/**
* @inheritdoc
*/
public function getCaptcha(
$captchaLength = self::CAPTCHA_DEFAULT_LENGTH,
$formFieldId = self::CAPTCHA_DEFAULT_INPUT_ID,
$inputOnly = false,
$path = ''
) {
if ($this->user->isAuthenticated() === false) {
$path = sha1($this->router->route(empty($path) === true ? $this->request->getQuery() : $path));
$this->sessionHandler->set('captcha_' . $path, $this->secureHelper->salt($captchaLength));
$this->view->assign('captcha', [
'width' => $captchaLength * 25,
'id' => $formFieldId,
'height' => 30,
'input_only' => $inputOnly,
'path' => $path
]);
return $this->view->fetchTemplate('Captcha/Partials/captcha_native.tpl');
}
return '';
}
/**
* @inheritdoc
*/
public function getValidationRule()
{
return '';
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
......@@ -9,8 +9,7 @@ namespace ACP3\Modules\ACP3\Captcha;
use ACP3\Core;
/**
* Class Helpers
* @package ACP3\Modules\ACP3\Captcha
* @deprecated Since 4.8.0, to be removed with version 5.0.0
*/
class Helpers
{
......@@ -45,12 +44,12 @@ class Helpers
/**
* Helpers constructor.
*
* @param \ACP3\Modules\ACP3\Users\Model\UserModel $user
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Modules\ACP3\Users\Model\UserModel $user
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Core\Session\SessionHandlerInterface $sessionHandler
* @param \ACP3\Core\View $view
* @param \ACP3\Core\Helpers\Secure $secureHelper
* @param \ACP3\Core\View $view
* @param \ACP3\Core\Helpers\Secure $secureHelper
*/
public function __construct(
\ACP3\Modules\ACP3\Users\Model\UserModel $user,
......@@ -72,9 +71,9 @@ class Helpers
* Erzeugt das Captchafeld für das Template
*
* @param integer $captchaLength
* @param string $formFieldId
* @param bool $inputOnly
* @param string $path
* @param string $formFieldId
* @param bool $inputOnly
* @param string $path
*
* @return string
*/
......@@ -96,7 +95,7 @@ class Helpers
'input_only' => $inputOnly,
'path' => $path
]);
return $this->view->fetchTemplate('Captcha/Partials/captcha.tpl');
return $this->view->fetchTemplate('Captcha/Partials/captcha_native.tpl');
}
return '';
}
......
......@@ -8,10 +8,6 @@ namespace ACP3\Modules\ACP3\Captcha\Installer;
use ACP3\Core\Modules\Installer\MigrationInterface;
/**
* Class Migration
* @package ACP3\Modules\ACP3\Captcha\Installer
*/
class Migration implements MigrationInterface
{
/**
......@@ -24,6 +20,9 @@ class Migration implements MigrationInterface
return [
31 => [
"DELETE FROM `{pre}acl_resources` WHERE `module_id` = '{moduleId}' AND `page` = 'functions';",
],
32 => [
"INSERT INTO `{pre}settings` (`id`, `module_id`, `name`, `value`) VALUES ('', '{moduleId}', 'captcha', 'captcha.extension.native_captcha_extension');",
]
];
}
......
......@@ -3,14 +3,10 @@ namespace ACP3\Modules\ACP3\Captcha\Installer;
use ACP3\Core\Modules;
/**
* Class Schema
* @package ACP3\Modules\ACP3\Captcha\Installer
*/
class Schema implements Modules\Installer\SchemaInterface
{
const MODULE_NAME = 'captcha';
/**
* @return array
*/
......@@ -32,7 +28,9 @@ class Schema implements Modules\Installer\SchemaInterface
*/
public function settings()
{
return [];
return [
'captcha' => 'captcha.extension.native_captcha_extension'
];
}
/**
......@@ -56,6 +54,6 @@ class Schema implements Modules\Installer\SchemaInterface
*/
public function getSchemaVersion()
{
return 31;
return 32;
}
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha;
use ACP3\Modules\ACP3\Captcha\DependencyInjection\CaptchaCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ModuleRegistration extends \ACP3\Core\Modules\ModuleRegistration
{
/**
* @inheritdoc
*/
public function build(ContainerBuilder $containerBuilder)
{
$containerBuilder->addCompilerPass(new CaptchaCompilerPass());
}
}
services:
captcha.controller.frontend.index.image:
class: ACP3\Modules\ACP3\Captcha\Controller\Index\Image
class: ACP3\Modules\ACP3\Captcha\Controller\Frontend\Index\Image
arguments:
- '@core.context.frontend'
- '@core.session'
services:
captcha.extension.native_captcha_extension:
class: ACP3\Modules\ACP3\Captcha\Extension\NativeCaptchaExtension
arguments:
- '@core.lang'
- '@core.http.request'
- '@core.router'
- '@core.session'
- '@core.view'
- '@core.helpers.secure'
- '@users.model.user_model'
tags:
- { name: 'captcha.extension.captcha' }
imports:
- { resource: components/controllers.yml }
- { resource: components/events.yml }
- { resource: components/extensions.yml }
- { resource: components/installer.yml }
- { resource: components/validation.yml }
services:
captcha.helpers:
class: ACP3\Modules\ACP3\Captcha\Helpers
deprecated: "The service %service_id% is deprecated since ACP3 version 4.8.0. Please use the 'captcha.extension.captcha_factory' service instead."
arguments:
- '@users.model.user_model'
- '@core.http.request'
......@@ -14,3 +16,6 @@ services:
- '@core.session'
- '@core.view'
- '@core.helpers.secure'
captcha.utility.captcha_registrar:
class: ACP3\Modules\ACP3\Captcha\Utility\CaptchaRegistrar
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Utility;
use ACP3\Modules\ACP3\Captcha\Extension\CaptchaExtensionInterface;
class CaptchaRegistrar
{
/**
* @var CaptchaExtensionInterface[]
*/
protected $availableCaptchas = [];
/**
* @param CaptchaExtensionInterface $captchaExtension
*/
public function registerCaptcha(CaptchaExtensionInterface $captchaExtension)
{
$this->availableCaptchas[$captchaExtension->getCaptchaName()] = $captchaExtension;
}
/**
* @return CaptchaExtensionInterface[]
*/
public function getAvailableCaptchas()
{
return $this->availableCaptchas;
}
}
......@@ -14,9 +14,7 @@ use Symfony\Component\DependencyInjection\Reference;
class SitemapAvailabilityCompilerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
* @inheritdoc
*/
public function process(ContainerBuilder $container)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment