Add form field for active user's password (#3783)
authorMatthias Schmidt <gravatronics@live.com>
Sun, 6 Dec 2020 12:48:28 +0000 (13:48 +0100)
committerGitHub <noreply@github.com>
Sun, 6 Dec 2020 12:48:28 +0000 (13:48 +0100)
Close #3777

com.woltlab.wcf/templates/__userPasswordFormField.tpl [new file with mode: 0644]
syncTemplates.json
wcfsetup/install/files/acp/templates/__userPasswordFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/user/UserPasswordField.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/__userPasswordFormField.tpl b/com.woltlab.wcf/templates/__userPasswordFormField.tpl
new file mode 100644 (file)
index 0000000..23fe7f1
--- /dev/null
@@ -0,0 +1,10 @@
+<input type="password" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{$field->getValue()}" {*
+       *}class="long"{*
+       *}{if $field->isAutofocused()} autofocus{/if}{*
+       *}{if $field->isRequired()} required{/if}{*
+       *}{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}{*
+       *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{*
+*}>
index 8f0fea6ceaf4abe5c0fad1b179aad75f9074b652..958a40af08cba063c8894846bc2ef5255caa888f 100644 (file)
@@ -47,6 +47,7 @@
     "__topReaction",
     "__uploadFormField",
     "__userFormField",
+    "__userPasswordFormField",
     "__usernameFormField",
     "__valueFormFieldDependency",
     "__wysiwygAttachmentFormField",
diff --git a/wcfsetup/install/files/acp/templates/__userPasswordFormField.tpl b/wcfsetup/install/files/acp/templates/__userPasswordFormField.tpl
new file mode 100644 (file)
index 0000000..23fe7f1
--- /dev/null
@@ -0,0 +1,10 @@
+<input type="password" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{$field->getValue()}" {*
+       *}class="long"{*
+       *}{if $field->isAutofocused()} autofocus{/if}{*
+       *}{if $field->isRequired()} required{/if}{*
+       *}{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}{*
+       *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{*
+*}>
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/user/UserPasswordField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/user/UserPasswordField.class.php
new file mode 100644 (file)
index 0000000..0dab0ed
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+namespace wcf\system\form\builder\field\user;
+use wcf\system\form\builder\field\AbstractFormField;
+use wcf\system\form\builder\field\IAutoFocusFormField;
+use wcf\system\form\builder\field\IPlaceholderFormField;
+use wcf\system\form\builder\field\TAutoFocusFormField;
+use wcf\system\form\builder\field\TDefaultIdFormField;
+use wcf\system\form\builder\field\TPlaceholderFormField;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\system\WCF;
+
+/**
+ * Implementation of a form field to enter the password of the active user.
+ * 
+ * This field is only available for logged-in users not user third party providers for logging in.
+ * This field uses the `wcf.user.password` language item as the default form field label and uses
+ * `password` as the default node id.
+ * 
+ * @author      Matthias Schmidt
+ * @copyright   2001-2020 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package     WoltLabSuite\Core\System\Form\Builder\Field\User
+ * @since       5.4
+ */
+class UserPasswordField extends AbstractFormField implements IAutoFocusFormField, IPlaceholderFormField {
+       use TAutoFocusFormField;
+       use TDefaultIdFormField;
+       use TPlaceholderFormField;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value';
+       
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__userPasswordFormField';
+       
+       /**
+        * Creates a new instance of `UserPasswordField`.
+        */
+       public function __construct() {
+               $this->label('wcf.user.password');
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       protected static function getDefaultId() {
+               return 'password';
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function isAvailable() {
+               return WCF::getUser()->userID != 0 && !WCF::getUser()->authData && parent::isAvailable();
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function readValue() {
+               if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
+                       $this->value = $this->getDocument()->getRequestData($this->getPrefixedId());
+               }
+               
+               return $this;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               if ($this->isRequired() && !$this->getValue()) {
+                       $this->addValidationError(new FormFieldValidationError('empty'));
+               }
+               else if ($this->getValue() && !WCF::getUser()->checkPassword($this->getValue())) {
+                       $this->addValidationError(
+                               new FormFieldValidationError(
+                                       'false',
+                                       'wcf.user.password.error.false'
+                               )
+                       );
+               }
+               
+               parent::validate();
+       }
+}