Skip to content

Commit

Permalink
replace usage of security service with TokenStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Oct 20, 2024
1 parent 163ffae commit 72786f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file.

## [3.4.1-DEV] - 2024-10-20
- Fixed: compatibility issues with symfony 7 and contao 5.4

## [3.4.0] - 2024-04-11
- Added: more options to DcaFields

Expand Down
17 changes: 10 additions & 7 deletions src/EventListener/DcaField/DcaAuthorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Contao\FrontendUser;
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class DcaAuthorListener extends AbstractDcaFieldListener
{
Expand All @@ -21,7 +21,10 @@ public function onLoadDataContainer(string $table): void

$options = AuthorField::getRegistrations()[$table];
$authorFieldName = $this->getAuthorFieldName($options);
$security = $this->container->get('security.helper');

/** @var TokenStorageInterface $tokenStorage */
$tokenStorage = $this->container->get(TokenStorageInterface::class);
$user = $tokenStorage->getToken()->getUser();

$authorField = [
'inputType' => 'select',
Expand All @@ -43,14 +46,14 @@ public function onLoadDataContainer(string $table): void

$authorField['default'] = 0;
if (AuthorField::TYPE_USER === $options->getType()) {
if ($security->getUser() instanceof BackendUser) {
$authorField['default'] = $security->getUser()->id;
if ($user instanceof BackendUser) {
$authorField['default'] = $user->id;
}
$authorField['foreignKey'] = 'tl_user.name';
$authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
} elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
if ($security->getUser() instanceof FrontendUser) {
$authorField['default'] = $security->getUser()->id;
if ($user instanceof FrontendUser) {
$authorField['default'] = $user->id;
}
$authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
$authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
Expand Down Expand Up @@ -104,7 +107,7 @@ protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
public static function getSubscribedServices(): array
{
$services = parent::getSubscribedServices();
$services['security.helper'] = Security::class;
$services[] = TokenStorageInterface::class;
return $services;
}
}

0 comments on commit 72786f9

Please sign in to comment.