Skip to content

Commit

Permalink
update to support Symfony 3, 4 and 5 (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored Dec 12, 2019
1 parent 0036efe commit 1c7b580
Show file tree
Hide file tree
Showing 31 changed files with 409 additions and 607 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/
composer.lock
phpunit.xml

.php_cs.cache
.phpunit.result.cache
16 changes: 16 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return PhpCsFixer\Config::create()
->setRules(array(
'@Symfony' => true,
'@Symfony:risky' => true,
'no_superfluous_phpdoc_tags' => true,
))
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->name('*.php')
)
;
28 changes: 12 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
sudo: false

language: php

cache:
directories:
- $HOME/.composer/cache/files
env:
- SYMFONY_VERSION=4.4.*

php:
- 5.5
- 5.6
- 7.0
- hhvm
- 7.2
- 7.3
- 7.4


matrix:
include:
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 5.6
env: SYMFONY_VERSION=3.0.*
- php: 7.1
env: SYMFONY_VERSION=4.0.*

env:
- SYMFONY_VERSION=2.8.*
- php: 7.4
env: SYMFONY_VERSION=3.4.*
- php: 7.4
env: SYMFONY_VERSION=4.3.*
- php: 7.4
env: SYMFONY_VERSION=5.0.*

before_install:
- travis_retry composer self-update

install:
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-update
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Changelog

#### Version 3.0.0-beta1

* Added support for Symfony 4 & 5
* Added FCQN as service names
* Added PHP 7 type hints
* Dropped support for Symfony < 3.4
* Dropped support for PHP < 7.2

#### Version 2.0.0-dev

* Added optional caching
Expand Down
173 changes: 66 additions & 107 deletions Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,143 +11,102 @@

use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface;
use Dmishh\SettingsBundle\Form\Type\SettingsType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Dmishh\SettingsBundle\Manager\SettingsManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Contracts\Translation\TranslatorInterface;

class SettingsController extends Controller
class SettingsController extends AbstractController
{
/**
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
* @var string|null
*/
public function manageGlobalAction(Request $request)
{
$securitySettings = $this->container->getParameter('settings_manager.security');

if (!empty($securitySettings['manage_global_settings_role']) &&
!$this->getAuthorizationChecker()->isGranted($securitySettings['manage_global_settings_role'])
) {
throw new AccessDeniedException(
$this->container->get('translator')->trans(
'not_allowed_to_edit_global_settings',
array(),
'settings'
)
);
}

return $this->manage($request);
}
private $securityRole;

/**
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
* @var bool
*/
public function manageOwnAction(Request $request)
{
$securityContext = $this->getSecurityContext();

if (!$securityContext->getToken()) {
throw new AccessDeniedException(
$this->get('translator')->trans(
'must_be_logged_in_to_edit_own_settings',
array(),
'settings'
)
);
}

$securitySettings = $this->container->getParameter('settings_manager.security');
if (!$securitySettings['users_can_manage_own_settings']) {
throw new AccessDeniedException(
$this->get('translator')->trans(
'not_allowed_to_edit_own_settings',
array(),
'settings'
)
);
}
private $securityManageOwnSettings;

$user = $securityContext->getToken()->getUser();
/**
* @var TranslatorInterface
*/
private $translator;

if (!($user instanceof SettingsOwnerInterface)) {
//For this to work the User entity must implement SettingsOwnerInterface
throw new AccessDeniedException();
}
/**
* @var SettingsManagerInterface
*/
private $settingsManager;

return $this->manage($request, $user);
/**
* @var string
*/
private $template;

public function __construct(
TranslatorInterface $translator,
SettingsManagerInterface $settingsManager,
string $template,
bool $securityManageOwnSettings,
?string $securityRole
) {
$this->translator = $translator;
$this->settingsManager = $settingsManager;
$this->template = $template;
$this->securityManageOwnSettings = $securityManageOwnSettings;
$this->securityRole = $securityRole;
}

/**
* @param Request $request
* @param SettingsOwnerInterface|null $owner
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws AccessDeniedException
*/
protected function manage(Request $request, SettingsOwnerInterface $owner = null)
public function manageGlobalAction(Request $request): Response
{
$form = $this->createForm(SettingsType::class, $this->get('settings_manager')->all($owner));

if ($request->isMethod('post')) {
$form->handleRequest($request);

if ($form->isValid()) {
$this->get('settings_manager')->setMany($form->getData(), $owner);
$this->get('session')->getFlashBag()->add(
'success',
$this->get('translator')->trans('settings_updated', array(), 'settings')
);

return $this->redirect($request->getUri());
}
if (null !== $this->securityRole && !$this->get('security.authorization_checker')->isGranted($this->securityRole)) {
throw new AccessDeniedException($this->translator->trans('not_allowed_to_edit_global_settings', [], 'settings'));
}

return $this->render(
$this->container->getParameter('settings_manager.template'),
array(
'settings_form' => $form->createView(),
)
);
return $this->manage($request);
}

/**
* Get AuthorizationChecker service.
*
* @return \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|\Symfony\Component\Security\Core\SecurityContextInterface
* @throws AccessDeniedException
*/
private function getAuthorizationChecker()
public function manageOwnAction(Request $request): Response
{
// SF 2.6+
// http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
if ($this->has('security.authorization_checker')) {
return $this->get('security.authorization_checker');
if (null === $this->get('security.token_storage')->getToken()) {
throw new AccessDeniedException($this->translator->trans('must_be_logged_in_to_edit_own_settings', [], 'settings'));
}

if (!$this->securityManageOwnSettings) {
throw new AccessDeniedException($this->translator->trans('not_allowed_to_edit_own_settings', [], 'settings'));
}

// SF < 2.6
return $this->get('security.context');
$user = $this->get('security.token_storage')->getToken()->getUser();
if (!$user instanceof SettingsOwnerInterface) {
//For this to work the User entity must implement SettingsOwnerInterface
throw new AccessDeniedException();
}

return $this->manage($request, $user);
}

/**
* Get SecurityContext service.
*
* @return \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|\Symfony\Component\Security\Core\SecurityContextInterface
*/
private function getSecurityContext()
protected function manage(Request $request, ?SettingsOwnerInterface $owner = null): Response
{
// SF 2.6+
// http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
if ($this->has('security.token_storage')) {
return $this->get('security.token_storage');
$form = $this->createForm(SettingsType::class, $this->settingsManager->all($owner));
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->settingsManager->setMany($form->getData(), $owner);
$this->addFlash('success', $this->translator->trans('settings_updated', [], 'settings'));

return $this->redirect($request->getUri());
}

// SF < 2.6
return $this->get('security.context');
return $this->render($this->template, [
'settings_form' => $form->createView(),
]);
}
}
28 changes: 17 additions & 11 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class Configuration implements ConfigurationInterface
{
Expand All @@ -23,14 +24,19 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('dmishh_settings');
$treeBuilder = new TreeBuilder('dmishh_settings');
// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->root('dmishh_settings');
} else {
$rootNode = $treeBuilder->getRootNode();
}

$scopes = array(
$scopes = [
SettingsManagerInterface::SCOPE_ALL,
SettingsManagerInterface::SCOPE_GLOBAL,
SettingsManagerInterface::SCOPE_USER,
);
];

$rootNode
->children()
Expand All @@ -42,13 +48,13 @@ public function getConfigTreeBuilder()
->arrayNode('security')
->addDefaultsIfNotSet()
->children()
->scalarNode('manage_global_settings_role')->end()
->scalarNode('manage_global_settings_role')->defaultValue(null)->end()
->booleanNode('users_can_manage_own_settings')->defaultValue(true)->end()
->end()
->end()
->enumNode('serialization')
->defaultValue('php')
->values(array('php', 'json'))
->values(['php', 'json'])
->end()
->arrayNode('settings')
->prototype('array')
Expand All @@ -61,14 +67,14 @@ public function getConfigTreeBuilder()
->thenInvalid('Invalid scope %s. Valid scopes are: '.implode(', ', array_map(function ($s) { return '"'.$s.'"'; }, $scopes)).'.')
->end()
->end()
->scalarNode('type')->defaultValue('Symfony\Component\Form\Extension\Core\Type\TextType')->end()
->scalarNode('type')->defaultValue(TextType::class)->end()

->variableNode('options')
->info('The options given to the form builder')
->defaultValue(array())
->defaultValue([])
->validate()
->always(function ($v) {
if (!is_array($v)) {
if (!\is_array($v)) {
throw new InvalidTypeException();
}

Expand All @@ -78,10 +84,10 @@ public function getConfigTreeBuilder()
->end()
->variableNode('constraints')
->info('The constraints on this option. Example, use constraits found in Symfony\Component\Validator\Constraints')
->defaultValue(array())
->defaultValue([])
->validate()
->always(function ($v) {
if (!is_array($v)) {
if (!\is_array($v)) {
throw new InvalidTypeException();
}

Expand Down
Loading

0 comments on commit 1c7b580

Please sign in to comment.