Gracefully handle invalid license credentials
authorAlexander Ebert <ebert@woltlab.com>
Mon, 11 Dec 2023 16:50:18 +0000 (17:50 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 11 Dec 2023 16:50:18 +0000 (17:50 +0100)
See https://www.woltlab.com/community/thread/302503-button-lizensierte-produkte-f%C3%BChrt-zu-fehler/

wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php
wcfsetup/install/files/lib/acp/page/LicensePage.class.php

index 5a1d93ee3c36f9687c7c23f4889fcc6c38877049..5186b3376b05b39d1457c910ecc5252c20e200a4 100644 (file)
@@ -55,6 +55,8 @@ final class LicenseEditForm extends AbstractFormBuilderForm
 
     private string $url;
 
+    private bool $failedValidation = false;
+
     /**
      * @inheritDoc
      */
@@ -66,6 +68,10 @@ final class LicenseEditForm extends AbstractFormBuilderForm
         if ($url && ApplicationHandler::getInstance()->isInternalURL($url)) {
             $this->url = $url;
         }
+
+        if (isset($_GET['failedValidation'])) {
+            $this->failedValidation = true;
+        }
     }
 
     /**
@@ -146,6 +152,16 @@ final class LicenseEditForm extends AbstractFormBuilderForm
                     ->fieldId('noCredentialsConfirm')
             );
         }
+
+        if ($this->failedValidation) {
+            $formField = $this->form->getNodeById('serialNo');
+            if ($formField instanceof TextFormField) {
+                $formField->addValidationError(new FormFieldValidationError(
+                    'failedValidation',
+                    'wcf.acp.firstTimeSetup.license.credentials.error.failedValidation'
+                ));
+            }
+        }
     }
 
     /**
index b7020d050e7681a881a03f8258aebfd10467c49f..09004c712676028759df94111d532f18b6fa4bde 100644 (file)
@@ -2,7 +2,9 @@
 
 namespace wcf\acp\page;
 
+use GuzzleHttp\Exception\ConnectException;
 use Laminas\Diactoros\Response\RedirectResponse;
+use Psr\Http\Client\ClientExceptionInterface;
 use wcf\acp\form\LicenseEditForm;
 use wcf\data\package\Package;
 use wcf\data\package\update\PackageUpdate;
@@ -71,7 +73,20 @@ final class LicensePage extends AbstractPage
                 // Cache valid license data for 2 minutes.
                 || $licenseData->creationDate->getTimestamp() < (\TIME_NOW - 2 * 60)
             ) {
-                $licenseData = $licenseApi->fetchFromRemote();
+                try {
+                    $licenseData = $licenseApi->fetchFromRemote();
+                } catch (ConnectException | ClientExceptionInterface) {
+                    return new RedirectResponse(
+                        LinkHandler::getInstance()->getControllerLink(
+                            LicenseEditForm::class,
+                            [
+                                'failedValidation' => 1,
+                                'url' => LinkHandler::getInstance()->getControllerLink(LicensePage::class),
+                            ],
+                        ),
+                    );
+                }
+
                 $licenseApi->updateLicenseFile($licenseData);
             }
         } catch (ParsingFailed $e) {