Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony 6 compatibility #125

Closed
wants to merge 14 commits into from
11 changes: 7 additions & 4 deletions Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -43,21 +44,23 @@ public function __construct(
SettingsManagerInterface $settingsManager,
string $template,
bool $securityManageOwnSettings,
?string $securityRole
?string $securityRole,
TokenStorageInterface $tokenStorage
) {
$this->translator = $translator;
$this->settingsManager = $settingsManager;
$this->template = $template;
$this->securityManageOwnSettings = $securityManageOwnSettings;
$this->securityRole = $securityRole;
$this->tokenStorage = $tokenStorage;
}

/**
* @throws AccessDeniedException
*/
public function manageGlobalAction(Request $request): Response
{
if (null !== $this->securityRole && !$this->get('security.authorization_checker')->isGranted($this->securityRole)) {
if (null !== $this->securityRole && !$this->isGranted($this->securityRole)) {
throw new AccessDeniedException($this->translator->trans('not_allowed_to_edit_global_settings', [], 'settings'));
}

Expand All @@ -69,15 +72,15 @@ public function manageGlobalAction(Request $request): Response
*/
public function manageOwnAction(Request $request): Response
{
if (null === $this->get('security.token_storage')->getToken()) {
if (null === $this->tokenStorage->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'));
}

$user = $this->get('security.token_storage')->getToken()->getUser();
$user = $this->tokenStorage->getToken()->getUser();
if (!$user instanceof SettingsOwnerInterface) {
//For this to work the User entity must implement SettingsOwnerInterface
throw new AccessDeniedException();
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
Expand All @@ -32,7 +33,7 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('template')
->defaultValue('DmishhSettingsBundle:Settings:manage.html.twig')
->defaultValue('@DmishhSettings/Settings/manage.html.twig')
->end()
->scalarNode('cache_service')->defaultNull()->info('A service implementing Psr\Cache\CacheItemPoolInterface')->end()
->integerNode('cache_lifetime')->defaultValue(3600)->end()
Expand Down
53 changes: 18 additions & 35 deletions Entity/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,56 @@

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="dmishh_settings", indexes={@ORM\Index(name="name_owner_id_idx", columns={"name", "owner_id"})})
* @ORM\Entity
*/
#[ORM\Table(name: 'dmishh_settings', indexes: [new ORM\Index(name: 'name_owner_id_idx', columns: ['name', 'owner_id'])])]
#[ORM\Entity]
class Setting
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/

#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;

/**
* @var string|null
*
* @ORM\Column(type="string", length=255)
*/
#[ORM\Column(type: 'string', length: 255)]
private $name;

/**
* @var string|null
*
* @ORM\Column(type="text", nullable=true)
*/
#[ORM\Column(type: 'text', nullable: true)]
private $value;

/**
* @var string|null
*
* @ORM\Column(name="owner_id", type="string", length=255, nullable=true)
*/
#[ORM\Column(name: 'owner_id', type: 'string', length: 255, nullable: true)]
private $ownerId;

public function getId(): ?int
{
return $this->id;
}

public function setName(?string $name)
public function setName(?string $name): void
{
$this->name = $name;
}

public function getName(): ?string
{
return $this->name;
}

public function setValue(?string $value): void
{
$this->value = $value;
}

public function getValue(): ?string
{
return $this->value;
}

public function getOwnerId(): ?string
{
return $this->ownerId;
}

public function setOwnerId(?string $ownerId)
public function setOwnerId(?string $ownerId): void
{
$this->ownerId = $ownerId;
}
Expand Down
11 changes: 7 additions & 4 deletions Form/Type/SettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(array $settingsConfiguration)
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
foreach ($this->settingsConfiguration as $name => $configuration) {
// If setting's value exists in data and setting isn't disabled
Expand Down Expand Up @@ -68,16 +68,19 @@ function ($label) use ($fieldOptions) {
}
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'disabled_settings' => [],
]
);
}

public function getBlockPrefix()

/**
* @return string
*/
public function getBlockPrefix(): string
{
return 'settings_management';
}
Expand Down
6 changes: 4 additions & 2 deletions Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
dmishh_settings_manage_global:
path: /global
defaults: { _controller: DmishhSettingsBundle:Settings:manageGlobal }
defaults:
_controller: Dmishh\SettingsBundle\Controller\SettingsController::manageGlobalAction

dmishh_settings_manage_own:
path: /personal
defaults: { _controller: DmishhSettingsBundle:Settings:manageOwn }
defaults:
_controller: Dmishh\SettingsBundle\Controller\SettingsController::manageOwnAction
3 changes: 3 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ services:
- { name: twig.extension }

Dmishh\SettingsBundle\Controller\SettingsController:
public: true
autowire: true
tags: ['container.service_subscriber']
arguments:
- '@translator'
- '@Dmishh\SettingsBundle\Manager\SettingsManagerInterface'
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
],
"require": {
"php": "^7.2|^8.0",
"psr/cache": "^1.0",
"symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0",
"symfony/form": "^3.4 || ^4.3 || ^5.0",
"doctrine/orm": "^2.6.3|^2.7"
"psr/cache": "^3.0",
"symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"symfony/form": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"doctrine/orm": "^2.6.3|^2.7|^2.11"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
Expand All @@ -33,8 +33,8 @@
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"nyholm/symfony-bundle-test": "^1.6",
"symfony/translation": "^4.4 || ^5.0",
"symfony/security-core": "^4.4 || ^5.0",
"symfony/translation": "^4.4 || ^5.0 || ^6.0",
"symfony/security-core": "^4.4 || ^5.0 || ^6.0",
"twig/twig": "^2.0 || ^3.0"
},
"autoload": {
Expand Down