|
| 1 | +<?php |
| 2 | +namespace wcf\system\form\builder\field; |
| 3 | +use wcf\system\form\builder\data\processor\CustomFormDataProcessor; |
| 4 | +use wcf\system\form\builder\field\validation\FormFieldValidationError; |
| 5 | +use wcf\system\form\builder\IFormDocument; |
| 6 | +use wcf\util\ArrayUtil; |
| 7 | +use wcf\util\JSON; |
| 8 | +use wcf\util\StringUtil; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implementation of a form field for single-line text values. |
| 12 | + * |
| 13 | + * @author Matthias Schmidt |
| 14 | + * @copyright 2001-2019 WoltLab GmbH |
| 15 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 16 | + * @package WoltLabSuite\Core\System\Form\Builder\Field |
| 17 | + * @since 5.2 |
| 18 | + */ |
| 19 | +class PasswordFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField, IMaximumLengthFormField, IMinimumLengthFormField, IPlaceholderFormField { |
| 20 | + use TAutoFocusFormField; |
| 21 | + use TImmutableFormField; |
| 22 | + use TMaximumLengthFormField; |
| 23 | + use TMinimumLengthFormField; |
| 24 | + use TPlaceholderFormField; |
| 25 | + |
| 26 | + /** |
| 27 | + * @inheritDoc |
| 28 | + */ |
| 29 | + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + protected $templateName = '__passwordFormField'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var integer |
| 38 | + */ |
| 39 | + protected $minimumPasswordStrength; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var mixed[] |
| 43 | + */ |
| 44 | + protected $passwordStrength; |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public function validate() { |
| 50 | + if ($this->isRequired() && ($this->getValue() === null || $this->getValue() === '')) { |
| 51 | + $this->addValidationError(new FormFieldValidationError('empty')); |
| 52 | + } |
| 53 | + else { |
| 54 | + $this->validateText($this->getValue()); |
| 55 | + } |
| 56 | + |
| 57 | + parent::validate(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Checks the length of the given password. |
| 62 | + * |
| 63 | + * @param string $text validated password |
| 64 | + */ |
| 65 | + protected function validateText($text) { |
| 66 | + $this->validateMinimumLength($text); |
| 67 | + $this->validateMaximumLength($text); |
| 68 | + $this->validateMinimumStrength($text); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @inheritDoc |
| 73 | + */ |
| 74 | + public function readValue() { |
| 75 | + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { |
| 76 | + $value = $this->getDocument()->getRequestData($this->getPrefixedId()); |
| 77 | + |
| 78 | + if (is_string($value)) { |
| 79 | + $this->value = StringUtil::trim($value); |
| 80 | + } |
| 81 | + } |
| 82 | + if ($this->getDocument()->hasRequestData($this->getPrefixedId() . '_passwordStrengthVerdict')) { |
| 83 | + $strength = $this->getDocument()->getRequestData($this->getPrefixedId() . '_passwordStrengthVerdict'); |
| 84 | + |
| 85 | + if (is_string($strength)) { |
| 86 | + $this->passwordStrength = ArrayUtil::trim(JSON::decode($strength)); |
| 87 | + } |
| 88 | + else if (is_array($strength)) { |
| 89 | + $this->passwordStrength = ArrayUtil::trim($strength); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Validates the minimum password strengh of the given password. |
| 98 | + * |
| 99 | + * @param string $text validated password |
| 100 | + * @param string $errorLanguageItem |
| 101 | + */ |
| 102 | + public function validateMinimumStrength($text, $errorLanguageItem = 'wcf.user.password.error.notSecure') { |
| 103 | + if ($this->getMinimumPasswordStrength() !== null && (empty($this->passwordStrength['score']) || $this->passwordStrength['score'] < $this->getMinimumPasswordStrength())) { |
| 104 | + $this->addValidationError(new FormFieldValidationError('notSecure', $errorLanguageItem)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param integer $minValue |
| 110 | + */ |
| 111 | + public function minimumPasswordStrength($minValue = PASSWORD_MIN_SCORE) { |
| 112 | + $this->minimumPasswordStrength = $minValue; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return integer |
| 117 | + */ |
| 118 | + public function getMinimumPasswordStrength() : int { |
| 119 | + return $this->minimumPasswordStrength; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @inheritDoc |
| 124 | + */ |
| 125 | + public function populate() { |
| 126 | + parent::populate(); |
| 127 | + |
| 128 | + $this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor('passwordStrengthVerdict', function(IFormDocument $document, array $parameters) { |
| 129 | + if (isset($parameters['data'][$this->getObjectProperty() . '_passwordStrengthVerdict'])) { |
| 130 | + $parameters[$this->getObjectProperty() . '_passwordStrengthVerdict'] = is_string($parameters['data'][$this->getObjectProperty() . '_passwordStrengthVerdict']) ? JSON::decode($parameters['data'][$this->getObjectProperty() . '_passwordStrengthVerdict']) : $parameters['data'][$this->getObjectProperty() . '_passwordStrengthVerdict']; |
| 131 | + } |
| 132 | + |
| 133 | + return $parameters; |
| 134 | + })); |
| 135 | + } |
| 136 | +} |
0 commit comments