Migrate activation form to form builder form
authorMarcel Werk <burntime@woltlab.com>
Mon, 27 Nov 2023 17:17:54 +0000 (18:17 +0100)
committerMarcel Werk <burntime@woltlab.com>
Mon, 27 Nov 2023 17:17:54 +0000 (18:17 +0100)
com.woltlab.wcf/page.xml
com.woltlab.wcf/templates/registerActivation.tpl
wcfsetup/install/files/lib/form/RegisterActivationForm.class.php

index b8692bac158f3ce30a4f106324feaaebae883fae..6582e68479df6a850779affc107e032334571eee 100644 (file)
                        <name language="de">Registrierung abschließen</name>
                        <name language="en">Complete Registration</name>
                        <hasFixedParent>1</hasFixedParent>
-                       <parent>com.woltlab.wcf.Register</parent>
                        <excludeFromLandingPage>1</excludeFromLandingPage>
                        <content language="en">
                                <title>Complete Registration</title>
index 924a7d54a68be51686d5bde0b991272c1967e10f..60573d516a2183bf133439d635a3c48872c5c7aa 100644 (file)
@@ -1,55 +1,9 @@
-{include file='header' __disableAds=true}
+{include file='authFlowHeader'}
 
 {if $__wcf->user->userID && !$__wcf->user->isEmailConfirmed()}
        <woltlab-core-notice type="info">{lang}wcf.user.registerActivation.info{/lang}</woltlab-core-notice>
 {/if}
 
-{include file='formError'}
+{@$form->getHtml()}
 
-<form method="post" action="{link controller='RegisterActivation'}{/link}">
-       <div class="section">
-               <dl{if $errorField == 'username'} class="formError"{/if}>
-                       <dt><label for="username">{lang}wcf.user.username{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="username" name="username" value="{$username}" required class="medium">
-                               {if $errorField == 'username'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.user.username.error.{@$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                       </dd>
-               </dl>
-               
-               <dl{if $errorField == 'activationCode'} class="formError"{/if}>
-                       <dt><label for="activationCode">{lang}wcf.user.activationCode{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="activationCode" maxlength="40" name="activationCode" value="{$activationCode}" required class="medium">
-                               {if $errorField == 'activationCode'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.user.activationCode.error.{@$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                               <small><a href="{link controller='RegisterNewActivationCode'}{/link}">{lang}wcf.user.newActivationCode{/lang}</a></small>
-                       </dd>
-               </dl>
-               
-               {event name='fields'}
-       </div>
-       
-       {event name='sections'}
-       
-       <div class="formSubmit">
-               <input type="submit" value="{lang}wcf.global.button.submit{/lang}" accesskey="s">
-               {csrfToken}
-       </div>
-</form>
-
-{include file='footer' __disableAds=true}
+{include file='authFlowFooter'}
index 9f8a5595858ef1fc2cf2b8b2676c1793d7181c47..ab4c320a72990865c6c150c4da16645610d03817 100644 (file)
@@ -4,11 +4,13 @@ namespace wcf\form;
 
 use wcf\data\user\User;
 use wcf\data\user\UserAction;
-use wcf\system\event\EventHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\NamedUserException;
 use wcf\system\exception\PermissionDeniedException;
-use wcf\system\exception\UserInputException;
+use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\field\TextFormField;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\system\form\builder\field\validation\FormFieldValidator;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
@@ -17,92 +19,111 @@ use wcf\util\StringUtil;
 /**
  * Shows the user activation form.
  *
- * @author  Marcel Werk
+ * @author      Marcel Werk
  * @copyright   2001-2019 WoltLab GmbH
- * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  */
-class RegisterActivationForm extends AbstractForm
+final class RegisterActivationForm extends AbstractFormBuilderForm
 {
-    /**
-     * username
-     * @var string
-     */
-    public $username;
-
-    /**
-     * activation code
-     * @var string
-     */
-    public $activationCode = '';
-
-    /**
-     * User object
-     * @var User
-     */
-    public $user;
+    public User $user;
 
     /**
      * @inheritDoc
      */
-    public function readParameters()
+    protected function createForm()
     {
-        parent::readParameters();
-
-        if (!empty($_GET['u'])) {
-            $userID = \intval($_GET['u']);
-            $this->user = new User($userID);
-            if ($this->user->userID) {
-                $this->username = $this->user->username;
-            }
+        parent::createForm();
+
+        $this->form->appendChild(
+            FormContainer::create('data')
+                ->appendChildren([
+                    TextFormField::create('username')
+                        ->label('wcf.user.username')
+                        ->required()
+                        ->autoFocus()
+                        ->maximumLength(255)
+                        ->addValidator(new FormFieldValidator(
+                            'usernameValidator',
+                            $this->validateUsername(...)
+                        )),
+                    TextFormField::create('activationCode')
+                        ->label('wcf.user.activationCode')
+                        ->description('<a href="' . StringUtil::encodeHTML(LinkHandler::getInstance()->getControllerLink(RegisterNewActivationCodeForm::class)) . '">' . WCF::getLanguage()->get('wcf.user.newActivationCode') . '</a>')
+                        ->required()
+                        ->maximumLength(40)
+                        ->addValidator(new FormFieldValidator(
+                            'activationCodeValidator',
+                            $this->validateActivationCode(...)
+                        ))
+                ])
+        );
+    }
+
+    private function validateUsername(TextFormField $formField): void
+    {
+        $value = StringUtil::trim($formField->getValue());
+        $this->user = User::getUserByUsername($value);
+
+        if (!$this->user->userID) {
+            $formField->addValidationError(
+                new FormFieldValidationError(
+                    'notFound',
+                    'wcf.user.username.error.notFound',
+                    [
+                        'username' => $value,
+                    ]
+                )
+            );
+        }
+
+        if ($this->user->isEmailConfirmed()) {
+            throw new NamedUserException(
+                WCF::getLanguage()->get('wcf.user.registerActivation.error.userAlreadyEnabled')
+            );
         }
-        if (!empty($_GET['a'])) {
-            $this->activationCode = StringUtil::trim($_GET['a']);
+
+        if (!empty($this->user->getBlacklistMatches())) {
+            throw new PermissionDeniedException();
         }
     }
 
-    /**
-     * @inheritDoc
-     */
-    public function readFormParameters()
+    private function validateActivationCode(TextFormField $formField): void
     {
-        parent::readFormParameters();
-
-        if (isset($_POST['username'])) {
-            $this->username = StringUtil::trim($_POST['username']);
-            $this->user = User::getUserByUsername($this->username);
+        if (!isset($this->user)) {
+            return;
         }
-        if (isset($_POST['activationCode'])) {
-            $this->activationCode = StringUtil::trim($_POST['activationCode']);
+
+        if (!\hash_equals($this->user->emailConfirmed, StringUtil::trim($formField->getValue()))) {
+            $formField->addValidationError(
+                new FormFieldValidationError(
+                    'invalid',
+                    'wcf.user.activationCode.error.invalid'
+                )
+            );
         }
     }
 
     /**
      * @inheritDoc
      */
-    public function validate()
+    public function show()
     {
-        EventHandler::getInstance()->fireAction($this, 'validate');
-
-        // check given user name
-        if ($this->user === null || !$this->user->userID) {
-            throw new UserInputException('username', 'notFound');
-        }
-
-        // user email is already confirmed
-        if ($this->user->isEmailConfirmed()) {
-            throw new NamedUserException(
-                WCF::getLanguage()->get('wcf.user.registerActivation.error.userAlreadyEnabled')
-            );
+        if (!(REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER)) {
+            throw new IllegalLinkException();
         }
 
-        // check given activation code
-        if (!\hash_equals($this->user->emailConfirmed, $this->activationCode)) {
-            throw new UserInputException('activationCode', 'invalid');
+        if (empty($_POST) && !empty($_GET['u']) && !empty($_GET['a'])) {
+            $user = new User(\intval($_GET['u']));
+            $_POST['username'] = $user->userID ? $user->username : '';
+            $_POST['activationCode'] = $_GET['a'];
+            $_REQUEST['t'] = WCF::getSession()->getSecurityToken();
         }
 
-        if (!empty($this->user->getBlacklistMatches())) {
+        if (!empty(WCF::getUser()->getBlacklistMatches())) {
             throw new PermissionDeniedException();
         }
+
+        parent::show();
     }
 
     /**
@@ -110,9 +131,8 @@ class RegisterActivationForm extends AbstractForm
      */
     public function save()
     {
-        parent::save();
+        AbstractForm::save();
 
-        // enable user
         $this->objectAction = new UserAction([$this->user], 'confirmEmail', ['skipNotification' => true]);
         $this->objectAction->executeAction();
         $this->saved();
@@ -129,37 +149,4 @@ class RegisterActivationForm extends AbstractForm
 
         exit;
     }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        WCF::getTPL()->assign([
-            'username' => $this->username,
-            'activationCode' => $this->activationCode,
-        ]);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function show()
-    {
-        if (!(REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER)) {
-            throw new IllegalLinkException();
-        }
-
-        if (empty($_POST) && $this->user !== null && $this->activationCode != 0) {
-            $this->submit();
-        }
-
-        if ($this->user === null && !empty(WCF::getUser()->getBlacklistMatches())) {
-            throw new PermissionDeniedException();
-        }
-
-        parent::show();
-    }
 }