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. 15
      DependencyInjection/Configuration.php
  4. 8
      EventListener/TosListener.php
  5. 26
      Form/AcceptTosType.php
  6. 19
      README.md
  7. 2
      Resources/config/services.xml
  8. 23
      Resources/views/Tos/accept.html.twig

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

@ -31,13 +31,13 @@ class AuthserverTosExtension extends Extension
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
if ($config['url'] !== null) {
if ($config['terms'] !== null) {
$servicesDirectory = __DIR__ . '/../Resources/config';
$fileLocator = new FileLocator($servicesDirectory);
$xmlLoader = new Loader\XmlFileLoader($container, $fileLocator);
$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']);
}
}

@ -1,17 +1,17 @@
<?php
/**
* Copyright (C) 2018 Lars Vierbergen
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -40,7 +40,14 @@ class Configuration implements ConfigurationInterface
// @formatter:off
$rootNode
->children()
->scalarNode('url')->defaultNull()->info('Link to the terms of service')->end()
->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()
->end()
->end()
->end()
->integerNode('version')->defaultValue(0)->info('Version of the terms of service')->end()
->end();
// @formatter:on

@ -21,7 +21,7 @@ use vierbergenlars\AuthserverTosBundle\Form\AcceptTosType;
class TosListener implements EventSubscriberInterface
{
private $tosUrl;
private $terms;
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->em = $em;
$this->tokenStorage = $tokenStorage;
@ -70,7 +70,7 @@ class TosListener implements EventSubscriberInterface
public function onBuildForm(RegistrationFormEvent $event)
{
$event->getFormBuilder()->add('vl_tos', AcceptTosType::class, [
'url' => $this->tosUrl,
'terms' => $this->terms,
'mapped' => false
]);
}

@ -9,16 +9,32 @@ use Symfony\Component\Validator\Constraints\IsTrue;
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)
{
$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, [
'label' => 'I accept the <a href="' . htmlentities($options['url'], ENT_HTML5 | ENT_QUOTES) . '">terms of service</a>',
'label' => 'I accept ' . $termsString,
'attr' => [
'align_with_widget' => true
],
'constraints' => [
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)
{
$resolver->setDefined('url')
->setRequired('url')
->addAllowedTypes('url', 'string')
$resolver->setDefined('terms')
->setRequired('terms')
->addAllowedTypes('terms', 'array')
->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.
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.
### 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">
<services>
<service class="vierbergenlars\AuthserverTosBundle\EventListener\TosListener">
<argument>%vierbergenlars_tos.tos_url%</argument>
<argument>%vierbergenlars_tos.terms%</argument>
<argument>%vierbergenlars_tos.tos_version%</argument>
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="security.token_storage" />

@ -1,15 +1,20 @@
{% extends '::base.html.twig' %}
{% block title %}{{ parent() }} - Updated terms and conditions{% endblock %}
{% block body %}
<div class="container-fluid">
<div class="container">
<div class="row">
<div class="col-xs-12">
<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>
<div class="col-xs-12">
<iframe src="{{ tos_url }}" style="width: 100%; height: 70vh;"></iframe>
</div>
<div class="col-xs-12">
{{ form(form, {'style': 'horizontal'}) }}
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Updated terms and conditions</h3>
</div>
<div class="panel-body">
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-body">
{{ form(form, {'style': 'horizontal'}) }}
</div>
</div>
</div>
</div>
</div>