Make it possible to specify multiple terms that have to be accepted

master v0.2.0
Lars Vierbergen 7 years ago
parent 8e75482649
commit 07b9b0a399
  1. 7
      Controller/TosController.php
  2. 4
      DependencyInjection/AuthserverTosExtension.php
  3. 7
      DependencyInjection/Configuration.php
  4. 8
      EventListener/TosListener.php
  5. 26
      Form/AcceptTosType.php
  6. 19
      README.md
  7. 2
      Resources/config/services.xml
  8. 17
      Resources/views/Tos/accept.html.twig

@ -26,11 +26,11 @@ class TosController extends Controller
$em->persist($userTos); $em->persist($userTos);
} }
$tosUrl = $this->container->getParameter('vierbergenlars_tos.tos_url'); $terms = $this->container->getParameter('vierbergenlars_tos.terms');
$formBuilder = $this->createFormBuilder(); $formBuilder = $this->createFormBuilder();
$formBuilder->add('vl_tos', AcceptTosType::class, [ $formBuilder->add('vl_tos', AcceptTosType::class, [
'url' => $tosUrl 'terms' => $terms
]); ]);
$formBuilder->add('submit', SubmitType::class, [ $formBuilder->add('submit', SubmitType::class, [
@ -48,8 +48,7 @@ class TosController extends Controller
} }
return [ return [
'form' => $form, 'form' => $form
'tos_url' => $tosUrl
]; ];
} }
} }

@ -31,13 +31,13 @@ class AuthserverTosExtension extends Extension
{ {
$configuration = new Configuration(); $configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs); $config = $this->processConfiguration($configuration, $configs);
if ($config['url'] !== null) { if ($config['terms'] !== null) {
$servicesDirectory = __DIR__ . '/../Resources/config'; $servicesDirectory = __DIR__ . '/../Resources/config';
$fileLocator = new FileLocator($servicesDirectory); $fileLocator = new FileLocator($servicesDirectory);
$xmlLoader = new Loader\XmlFileLoader($container, $fileLocator); $xmlLoader = new Loader\XmlFileLoader($container, $fileLocator);
$xmlLoader->load('services.xml'); $xmlLoader->load('services.xml');
$container->setParameter('vierbergenlars_tos.tos_url', $config['url']); $container->setParameter('vierbergenlars_tos.terms', $config['terms']);
$container->setParameter('vierbergenlars_tos.tos_version', $config['version']); $container->setParameter('vierbergenlars_tos.tos_version', $config['version']);
} }
} }

@ -40,7 +40,14 @@ class Configuration implements ConfigurationInterface
// @formatter:off // @formatter:off
$rootNode $rootNode
->children() ->children()
->arrayNode('terms')
->prototype('array')
->children()
->scalarNode('label')->defaultNull()->info('Label for how the terms are named (terms of service, privacy policy, ...)')->end()
->scalarNode('url')->defaultNull()->info('Link to the terms of service')->end() ->scalarNode('url')->defaultNull()->info('Link to the terms of service')->end()
->end()
->end()
->end()
->integerNode('version')->defaultValue(0)->info('Version of the terms of service')->end() ->integerNode('version')->defaultValue(0)->info('Version of the terms of service')->end()
->end(); ->end();
// @formatter:on // @formatter:on

@ -21,7 +21,7 @@ use vierbergenlars\AuthserverTosBundle\Form\AcceptTosType;
class TosListener implements EventSubscriberInterface class TosListener implements EventSubscriberInterface
{ {
private $tosUrl; private $terms;
private $tosVersion; private $tosVersion;
@ -58,9 +58,9 @@ class TosListener implements EventSubscriberInterface
]; ];
} }
public function __construct($tosUrl, $tosVersion, EntityManagerInterface $em, TokenStorageInterface $tokenStorage, UrlGeneratorInterface $urlGenerator) public function __construct($terms, $tosVersion, EntityManagerInterface $em, TokenStorageInterface $tokenStorage, UrlGeneratorInterface $urlGenerator)
{ {
$this->tosUrl = $tosUrl; $this->terms = $terms;
$this->tosVersion = $tosVersion; $this->tosVersion = $tosVersion;
$this->em = $em; $this->em = $em;
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
@ -70,7 +70,7 @@ class TosListener implements EventSubscriberInterface
public function onBuildForm(RegistrationFormEvent $event) public function onBuildForm(RegistrationFormEvent $event)
{ {
$event->getFormBuilder()->add('vl_tos', AcceptTosType::class, [ $event->getFormBuilder()->add('vl_tos', AcceptTosType::class, [
'url' => $this->tosUrl, 'terms' => $this->terms,
'mapped' => false 'mapped' => false
]); ]);
} }

@ -9,16 +9,32 @@ use Symfony\Component\Validator\Constraints\IsTrue;
class AcceptTosType extends AbstractType class AcceptTosType extends AbstractType
{ {
private static function generateTermLink(array $term)
{
return 'the <a href="' . htmlentities($term['url'], ENT_HTML5 | ENT_QUOTES) . '">' . htmlentities($term['label'], ENT_HTML5) . '</a>';
}
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$terms = $options['terms'];
if (count($terms) > 1) {
$lastItem = ' and ' . self::generateTermLink(array_pop($terms));
} else {
$lastItem = '';
}
$termsString = implode(', ', array_map([
self::class,
'generateTermLink'
], $terms)) . $lastItem;
$builder->add('accept', CheckboxType::class, [ $builder->add('accept', CheckboxType::class, [
'label' => 'I accept the <a href="' . htmlentities($options['url'], ENT_HTML5 | ENT_QUOTES) . '">terms of service</a>', 'label' => 'I accept ' . $termsString,
'attr' => [ 'attr' => [
'align_with_widget' => true 'align_with_widget' => true
], ],
'constraints' => [ 'constraints' => [
new IsTrue([ new IsTrue([
'message' => 'You must accept the terms of service.' 'message' => 'You must accept.'
]) ])
] ]
]); ]);
@ -31,9 +47,9 @@ class AcceptTosType extends AbstractType
*/ */
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver) public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
{ {
$resolver->setDefined('url') $resolver->setDefined('terms')
->setRequired('url') ->setRequired('terms')
->addAllowedTypes('url', 'string') ->addAllowedTypes('terms', 'array')
->setDefault('label', false); ->setDefault('label', false);
} }
} }

@ -12,5 +12,22 @@ For more details, see the [Authserver plugin documentation](https://github.com/v
You can configure the bundle in the authserver `app/config/parameters.yml` file. You can configure the bundle in the authserver `app/config/parameters.yml` file.
Set `tos.url` to a link to your terms of service. Add a new array key under `tos.terms` for all terms and conditions that you want to add.
`label` is the name you want to give the terms
`url` is the URL where the terms are located
Set `tos.version` to the version of your terms of service. This number should be increased every time the terms of service change, so users get prompted to accept the new terms. The version of the latest accepted version is stored per user. If the stored number is lower than the configured version, the user is prompted. Set `tos.version` to the version of your terms of service. This number should be increased every time the terms of service change, so users get prompted to accept the new terms. The version of the latest accepted version is stored per user. If the stored number is lower than the configured version, the user is prompted.
### Example
```yaml
tos:
version: 1
terms:
- title: terms of service
url: http://example.com/tos
- title: privacy policy
url: http://example.com/privacy
```

@ -20,7 +20,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services> <services>
<service class="vierbergenlars\AuthserverTosBundle\EventListener\TosListener"> <service class="vierbergenlars\AuthserverTosBundle\EventListener\TosListener">
<argument>%vierbergenlars_tos.tos_url%</argument> <argument>%vierbergenlars_tos.terms%</argument>
<argument>%vierbergenlars_tos.tos_version%</argument> <argument>%vierbergenlars_tos.tos_version%</argument>
<argument type="service" id="doctrine.orm.entity_manager" /> <argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="security.token_storage" /> <argument type="service" id="security.token_storage" />

@ -1,16 +1,21 @@
{% extends '::base.html.twig' %} {% extends '::base.html.twig' %}
{% block title %}{{ parent() }} - Updated terms and conditions{% endblock %}
{% block body %} {% block body %}
<div class="container-fluid"> <div class="container">
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="alert alert-info">Our terms of service have been updated.<br>You must accept these terms to be able to continue using the application.</div> <div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Updated terms and conditions</h3>
</div> </div>
<div class="col-xs-12"> <div class="panel-body">
<iframe src="{{ tos_url }}" style="width: 100%; height: 70vh;"></iframe> Our terms of service have been updated.<br>You must accept these terms to be able to continue using the application.
</div> </div>
<div class="col-xs-12"> <div class="panel-body">
{{ form(form, {'style': 'horizontal'}) }} {{ form(form, {'style': 'horizontal'}) }}
</div> </div>
</div> </div>
</div> </div>
</div>
</div>
{% endblock %} {% endblock %}