From 5b9d07366cddaaf7058a2b4fcf0676c4c102289b Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 6 Dec 2020 13:48:28 +0100 Subject: [PATCH] Add form field for active user's password (#3783) Close #3777 --- .../templates/__userPasswordFormField.tpl | 10 +++ syncTemplates.json | 1 + .../acp/templates/__userPasswordFormField.tpl | 10 +++ .../field/user/UserPasswordField.class.php | 90 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 com.woltlab.wcf/templates/__userPasswordFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__userPasswordFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/user/UserPasswordField.class.php diff --git a/com.woltlab.wcf/templates/__userPasswordFormField.tpl b/com.woltlab.wcf/templates/__userPasswordFormField.tpl new file mode 100644 index 0000000000..23fe7f1f76 --- /dev/null +++ b/com.woltlab.wcf/templates/__userPasswordFormField.tpl @@ -0,0 +1,10 @@ +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/syncTemplates.json b/syncTemplates.json index 8f0fea6cea..958a40af08 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -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 index 0000000000..23fe7f1f76 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__userPasswordFormField.tpl @@ -0,0 +1,10 @@ +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 index 0000000000..0dab0ed072 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/user/UserPasswordField.class.php @@ -0,0 +1,90 @@ + + * @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(); + } +} -- 2.20.1