Skip to content

Commit 0848dae

Browse files
authored
Merge pull request #25 from jweiland-net/preventUploadOnFormError
Prevent upload on form error
2 parents e13ad69 + a8de485 commit 0848dae

27 files changed

+1424
-355
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.github/ export-ignore
22
/Build/ export-ignore
33
/Tests/ export-ignore
4+
/.crowdin.yml export-ignore
45
/.editorconfig export-ignore
56
/.gitattributes export-ignore
67
/.gitignore export-ignore

.github/workflows/ci.yml

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,17 @@ on: [pull_request]
66
jobs:
77
coding:
88
name: Coding Standard
9-
runs-on: 'ubuntu-18.04'
9+
runs-on: 'ubuntu-22.04'
1010

1111
steps:
1212
- name: Checkout
13-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4
1414

1515
- name: Setup PHP with tools
1616
uses: shivammathur/setup-php@v2
1717
with:
1818
php-version: '7.4'
1919

20-
- name: Get composer cache directory
21-
id: composer-cache
22-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
23-
24-
- name: Cache composer dependencies
25-
uses: actions/cache@v2
26-
with:
27-
path: ${{ steps.composer-cache.outputs.dir }}
28-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
29-
restore-keys: ${{ runner.os }}-composer-
30-
3120
- name: Validate composer.json and composer.lock
3221
run: composer validate
3322

@@ -44,7 +33,7 @@ jobs:
4433
run: composer ci:php:lint
4534

4635
testing:
47-
runs-on: 'ubuntu-18.04'
36+
runs-on: 'ubuntu-22.04'
4837
name: PHP Unit and Functional Tests
4938
needs: coding
5039

@@ -62,7 +51,7 @@ jobs:
6251

6352
steps:
6453
- name: Checkout
65-
uses: actions/checkout@v3
54+
uses: actions/checkout@v4
6655

6756
- name: Start MySQL Server at GitHub
6857
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }}
@@ -76,17 +65,6 @@ jobs:
7665
ini-values: memory_limit=512M, post_max_size=48M, max_execution_time=30
7766
coverage: none
7867

79-
- name: Get composer cache directory
80-
id: composer-cache
81-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
82-
83-
- name: Cache composer dependencies
84-
uses: actions/cache@v2
85-
with:
86-
path: ${{ steps.composer-cache.outputs.dir }}
87-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
88-
restore-keys: ${{ runner.os }}-composer-
89-
9068
- name: Install dependencies with typo3/cms-core:${{ matrix.typo3 }}
9169
run: |
9270
composer require typo3/cms-core:${{ matrix.typo3 }} --no-progress

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*~
1111
*.bak
1212
*.swp
13+
.ddev
1314
.DS_Store
1415

1516
# Ignore by common IDEs used directories/files
@@ -31,4 +32,4 @@ nbproject
3132
*.log
3233

3334
# Ignore composer stuff
34-
/composer.lock
35+
/composer.lock

Classes/Configuration/ExtConf.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace JWeiland\Checkfaluploads\Configuration;
1313

14+
use JWeiland\Checkfaluploads\Traits\ApplicationContextTrait;
15+
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
16+
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
1417
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
1518
use TYPO3\CMS\Core\SingletonInterface;
1619
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
@@ -20,23 +23,26 @@
2023
*/
2124
class ExtConf implements SingletonInterface
2225
{
26+
use ApplicationContextTrait;
27+
2328
/**
2429
* @var string
2530
*/
2631
protected $owner = '';
2732

2833
public function __construct(ExtensionConfiguration $extensionConfiguration)
2934
{
30-
// get global configuration
31-
$extConf = $extensionConfiguration->get('checkfaluploads');
32-
if (is_array($extConf)) {
33-
// call setter method foreach configuration entry
34-
foreach ($extConf as $key => $value) {
35-
$methodName = 'set' . ucfirst($key);
36-
if (method_exists($this, $methodName)) {
37-
$this->$methodName($value);
35+
try {
36+
$extConf = $extensionConfiguration->get('checkfaluploads');
37+
if (is_array($extConf)) {
38+
foreach ($extConf as $key => $value) {
39+
$methodName = 'set' . ucfirst($key);
40+
if (method_exists($this, $methodName)) {
41+
$this->$methodName($value);
42+
}
3843
}
3944
}
45+
} catch (ExtensionConfigurationExtensionNotConfiguredException | ExtensionConfigurationPathDoesNotExistException $e) {
4046
}
4147
}
4248

@@ -60,11 +66,16 @@ public function setOwner(string $owner): void
6066
*/
6167
public function getLabelForUserRights(): string
6268
{
69+
$langKey = 'dragUploader.fileRights.title';
70+
if ($this->isFrontendRequest()) {
71+
$langKey = 'frontend.imageUserRights';
72+
}
73+
6374
return LocalizationUtility::translate(
64-
'dragUploader.fileRights.title',
75+
$langKey,
6576
'checkfaluploads',
6677
[
67-
0 => $this->getOwner()
78+
0 => $this->getOwner(),
6879
]
6980
);
7081
}

Classes/EventListener/AddUserToFalRecordOnCreationEventListener.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111

1212
namespace JWeiland\Checkfaluploads\EventListener;
1313

14-
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
14+
use JWeiland\Checkfaluploads\Traits\ApplicationContextTrait;
15+
use JWeiland\Checkfaluploads\Traits\BackendUserAuthenticationTrait;
16+
use JWeiland\Checkfaluploads\Traits\TypoScriptFrontendControllerTrait;
1517
use TYPO3\CMS\Core\Database\ConnectionPool;
1618
use TYPO3\CMS\Core\Resource\Event\AfterFileAddedToIndexEvent;
17-
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
1819

1920
/**
2021
* Add the uid of the current user to the uploaded file
2122
*/
2223
class AddUserToFalRecordOnCreationEventListener
2324
{
25+
use ApplicationContextTrait;
26+
use BackendUserAuthenticationTrait;
27+
use TypoScriptFrontendControllerTrait;
28+
2429
/**
2530
* @var ConnectionPool
2631
*/
@@ -39,9 +44,9 @@ public function __invoke(AfterFileAddedToIndexEvent $event): void
3944
}
4045

4146
$fields = [];
42-
if (TYPO3_MODE === 'BE') {
47+
if ($this->isBackendRequest()) {
4348
$fields['cruser_id'] = (int)($this->getBackendUserAuthentication()->user['uid'] ?? 0);
44-
} elseif (TYPO3_MODE === 'FE') {
49+
} elseif ($this->isFrontendRequest()) {
4550
$fields['fe_cruser_id'] = (int)($this->getTypoScriptFrontendController()->fe_user->user['uid'] ?? 0);
4651
}
4752

@@ -54,14 +59,4 @@ public function __invoke(AfterFileAddedToIndexEvent $event): void
5459
]
5560
);
5661
}
57-
58-
protected function getBackendUserAuthentication(): BackendUserAuthentication
59-
{
60-
return $GLOBALS['BE_USER'];
61-
}
62-
63-
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
64-
{
65-
return $GLOBALS['TSFE'];
66-
}
6762
}

Classes/EventListener/AddUserToFalRecordOnUpdateEventListener.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111

1212
namespace JWeiland\Checkfaluploads\EventListener;
1313

14-
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
14+
use JWeiland\Checkfaluploads\Traits\ApplicationContextTrait;
15+
use JWeiland\Checkfaluploads\Traits\BackendUserAuthenticationTrait;
16+
use JWeiland\Checkfaluploads\Traits\TypoScriptFrontendControllerTrait;
1517
use TYPO3\CMS\Core\Database\ConnectionPool;
1618
use TYPO3\CMS\Core\Resource\Event\AfterFileUpdatedInIndexEvent;
17-
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
1819

1920
/**
2021
* Add the uid of the current user to the uploaded file
2122
*/
2223
class AddUserToFalRecordOnUpdateEventListener
2324
{
25+
use ApplicationContextTrait;
26+
use BackendUserAuthenticationTrait;
27+
use TypoScriptFrontendControllerTrait;
28+
2429
/**
2530
* @var ConnectionPool
2631
*/
@@ -39,9 +44,9 @@ public function __invoke(AfterFileUpdatedInIndexEvent $event): void
3944
}
4045

4146
$fields = [];
42-
if (TYPO3_MODE === 'BE') {
47+
if ($this->isBackendRequest()) {
4348
$fields['cruser_id'] = (int)$this->getBackendUserAuthentication()->user['uid'];
44-
} elseif (TYPO3_MODE === 'FE') {
49+
} elseif ($this->isFrontendRequest()) {
4550
$fields['fe_cruser_id'] = (int)$this->getTypoScriptFrontendController()->fe_user->user['uid'];
4651
}
4752

@@ -54,14 +59,4 @@ public function __invoke(AfterFileUpdatedInIndexEvent $event): void
5459
]
5560
);
5661
}
57-
58-
protected function getBackendUserAuthentication(): BackendUserAuthentication
59-
{
60-
return $GLOBALS['BE_USER'];
61-
}
62-
63-
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
64-
{
65-
return $GLOBALS['TSFE'];
66-
}
6762
}

Classes/EventListener/UserMarkedCheckboxForRightsEventListener.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace JWeiland\Checkfaluploads\EventListener;
1313

14+
use JWeiland\Checkfaluploads\Helper\MessageHelper;
15+
use JWeiland\Checkfaluploads\Traits\ApplicationContextTrait;
1416
use TYPO3\CMS\Core\Messaging\AbstractMessage;
15-
use TYPO3\CMS\Core\Messaging\FlashMessage;
16-
use TYPO3\CMS\Core\Messaging\FlashMessageService;
1717
use TYPO3\CMS\Core\Resource\Event\BeforeFileAddedEvent;
1818
use TYPO3\CMS\Core\Resource\Event\BeforeFileReplacedEvent;
1919
use TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException;
@@ -26,14 +26,24 @@
2626
*/
2727
class UserMarkedCheckboxForRightsEventListener
2828
{
29+
use ApplicationContextTrait;
30+
2931
/**
3032
* @var ExtendedFileUtility
3133
*/
3234
protected $extendedFileUtility;
3335

34-
public function __construct(ExtendedFileUtility $extendedFileUtility)
35-
{
36+
/**
37+
* @var MessageHelper
38+
*/
39+
protected $messageHelper;
40+
41+
public function __construct(
42+
ExtendedFileUtility $extendedFileUtility,
43+
MessageHelper $messageHelper
44+
) {
3645
$this->extendedFileUtility = $extendedFileUtility;
46+
$this->messageHelper = $messageHelper;
3747
}
3848

3949
/**
@@ -42,9 +52,9 @@ public function __construct(ExtendedFileUtility $extendedFileUtility)
4252
public function checkForAddedFile(BeforeFileAddedEvent $event): void
4353
{
4454
// FE will not be checked here. This should be part of the extension itself.
45-
if (TYPO3_MODE === 'BE') {
55+
if ($this->isBackendRequest()) {
4656
$fileParts = GeneralUtility::split_fileref($event->getFileName());
47-
if (!in_array($fileParts['fileext'], ['youtube', 'vimeo'])) {
57+
if (!in_array($fileParts['fileext'], ['youtube', 'vimeo'], true)) {
4858
$userHasRights = GeneralUtility::_POST('userHasRights');
4959
if (empty($userHasRights)) {
5060
$message = LocalizationUtility::translate(
@@ -54,7 +64,7 @@ public function checkForAddedFile(BeforeFileAddedEvent $event): void
5464

5565
$this->extendedFileUtility->writeLog(1, 1, 105, $message, []);
5666

57-
$this->addMessageToFlashMessageQueue($message);
67+
$this->messageHelper->addFlashMessage($message, '', AbstractMessage::ERROR);
5868

5969
throw new InsufficientUserPermissionsException($message, 1396626278);
6070
}
@@ -68,7 +78,7 @@ public function checkForAddedFile(BeforeFileAddedEvent $event): void
6878
public function checkForReplacedFile(BeforeFileReplacedEvent $event): void
6979
{
7080
// FE will not be checked here. This should be part of the extension itself.
71-
if (TYPO3_MODE === 'BE') {
81+
if ($this->isBackendRequest()) {
7282
$fileParts = GeneralUtility::split_fileref($event->getFile()->getName());
7383
if (!in_array($fileParts['fileext'], ['youtube', 'vimeo'])) {
7484
$userHasRights = GeneralUtility::_POST('userHasRights');
@@ -80,36 +90,11 @@ public function checkForReplacedFile(BeforeFileReplacedEvent $event): void
8090

8191
$this->extendedFileUtility->writeLog(1, 1, 105, $message, []);
8292

83-
$this->addMessageToFlashMessageQueue($message);
93+
$this->messageHelper->addFlashMessage($message, '', AbstractMessage::ERROR);
8494

8595
throw new InsufficientUserPermissionsException($message, 1396626278);
8696
}
8797
}
8898
}
8999
}
90-
91-
protected function addMessageToFlashMessageQueue(
92-
string $message,
93-
int $severity = AbstractMessage::ERROR
94-
): void {
95-
if (TYPO3_MODE !== 'BE') {
96-
return;
97-
}
98-
99-
$flashMessage = GeneralUtility::makeInstance(
100-
FlashMessage::class,
101-
$message,
102-
'',
103-
$severity,
104-
true
105-
);
106-
$this->addFlashMessage($flashMessage);
107-
}
108-
109-
protected function addFlashMessage(FlashMessage $flashMessage): void
110-
{
111-
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
112-
$defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
113-
$defaultFlashMessageQueue->enqueue($flashMessage);
114-
}
115100
}

0 commit comments

Comments
 (0)