From 89c02ff1569f2a55523b25921346c9d510192170 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Mon, 27 Nov 2023 18:17:54 +0100 Subject: [PATCH] Migrate activation form to form builder form --- com.woltlab.wcf/page.xml | 1 - .../templates/registerActivation.tpl | 52 +---- .../lib/form/RegisterActivationForm.class.php | 179 ++++++++---------- 3 files changed, 86 insertions(+), 146 deletions(-) diff --git a/com.woltlab.wcf/page.xml b/com.woltlab.wcf/page.xml index b8692bac15..6582e68479 100644 --- a/com.woltlab.wcf/page.xml +++ b/com.woltlab.wcf/page.xml @@ -245,7 +245,6 @@ Registrierung abschließen Complete Registration 1 - com.woltlab.wcf.Register 1 Complete Registration diff --git a/com.woltlab.wcf/templates/registerActivation.tpl b/com.woltlab.wcf/templates/registerActivation.tpl index 924a7d54a6..60573d516a 100644 --- a/com.woltlab.wcf/templates/registerActivation.tpl +++ b/com.woltlab.wcf/templates/registerActivation.tpl @@ -1,55 +1,9 @@ -{include file='header' __disableAds=true} +{include file='authFlowHeader'} {if $__wcf->user->userID && !$__wcf->user->isEmailConfirmed()} {lang}wcf.user.registerActivation.info{/lang} {/if} -{include file='formError'} +{@$form->getHtml()} -
-
- -
-
- - {if $errorField == 'username'} - - {if $errorType == 'empty'} - {lang}wcf.global.form.error.empty{/lang} - {else} - {lang}wcf.user.username.error.{@$errorType}{/lang} - {/if} - - {/if} -
- - - -
-
- - {if $errorField == 'activationCode'} - - {if $errorType == 'empty'} - {lang}wcf.global.form.error.empty{/lang} - {else} - {lang}wcf.user.activationCode.error.{@$errorType}{/lang} - {/if} - - {/if} - {lang}wcf.user.newActivationCode{/lang} -
- - - {event name='fields'} -
- - {event name='sections'} - -
- - {csrfToken} -
-
- -{include file='footer' __disableAds=true} +{include file='authFlowFooter'} diff --git a/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php b/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php index 9f8a559585..ab4c320a72 100644 --- a/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php +++ b/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php @@ -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 + * @license GNU Lesser General Public License */ -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('' . WCF::getLanguage()->get('wcf.user.newActivationCode') . '') + ->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(); - } } -- 2.20.1