1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Fix issue processwire/processwire-issues#239 (display Password description and notes in correct locations). Plus add support for disabling and/or relaxing some of the strict password requirements per a previous request (which I can't seem to find at the moment).

This commit is contained in:
Ryan Cramer
2019-03-29 11:49:01 -04:00
parent 478d299322
commit 2a108b4cd0

View File

@@ -56,6 +56,12 @@ class InputfieldPassword extends InputfieldText {
*/ */
const requireOther = 'other'; const requireOther = 'other';
/**
* Requirements: disable all above
*
*/
const requireNone = 'none';
/** /**
* Require old password before changes? Auto * Require old password before changes? Auto
* *
@@ -102,6 +108,7 @@ class InputfieldPassword extends InputfieldText {
self::requireUpperLetter => $this->_('uppercase letter'), self::requireUpperLetter => $this->_('uppercase letter'),
self::requireDigit => $this->_('digit'), self::requireDigit => $this->_('digit'),
self::requireOther => $this->_('symbol/punctuation'), self::requireOther => $this->_('symbol/punctuation'),
self::requireNone => $this->_('none (disable all above)'),
)); ));
$this->set('showPass', false); // allow password to be rendered in renderValue and/or re-populated in form? $this->set('showPass', false); // allow password to be rendered in renderValue and/or re-populated in form?
} }
@@ -158,8 +165,6 @@ class InputfieldPassword extends InputfieldText {
*/ */
public function ___render() { public function ___render() {
$description = $this->getSetting('description');
if($description) $this->notes = $description;
$minlength = (int) $this->attr('minlength'); $minlength = (int) $this->attr('minlength');
$requirements = array(); $requirements = array();
@@ -168,8 +173,10 @@ class InputfieldPassword extends InputfieldText {
sprintf($this->_('at least %d characters long'), $minlength) . "[/span]"; sprintf($this->_('at least %d characters long'), $minlength) . "[/span]";
} }
$labels = $this->getSetting('requirementsLabels'); $labels = $this->getSetting('requirementsLabels');
foreach($this->getSetting('requirements') as $name) { if(!in_array(self::requireNone, $this->getSetting('requirements'))) {
$requirements[$name] = "[span.pass-require.pass-require-$name]" . $labels[$name] . "[/span]"; foreach($this->getSetting('requirements') as $name) {
$requirements[$name] = "[span.pass-require.pass-require-$name]" . $labels[$name] . "[/span]";
}
} }
if(isset($requirements['upper']) || isset($requirements['lower'])) { if(isset($requirements['upper']) || isset($requirements['lower'])) {
unset($requirements['letter']); unset($requirements['letter']);
@@ -177,6 +184,7 @@ class InputfieldPassword extends InputfieldText {
if(count($requirements)) { if(count($requirements)) {
$description = implode(", ", $requirements); $description = implode(", ", $requirements);
$description = sprintf($this->_('Minimum requirements: %s.'), $description); $description = sprintf($this->_('Minimum requirements: %s.'), $description);
$description = trim("$description " . $this->getSetting('description'));
$this->set('description', $description); $this->set('description', $description);
} }
@@ -193,7 +201,7 @@ class InputfieldPassword extends InputfieldText {
} }
$this->attr('data-banMode', $this->complexifyBanMode ? $this->complexifyBanMode : 'loose'); $this->attr('data-banMode', $this->complexifyBanMode ? $this->complexifyBanMode : 'loose');
$this->attr('data-factor', $this->complexifyFactor ? $this->complexifyFactor : '0.7'); $this->attr('data-factor', (float) $this->complexifyFactor >= 0 ? $this->complexifyFactor : 0);
$inputClass = $this->wire('sanitizer')->entities($this->attr('class')); $inputClass = $this->wire('sanitizer')->entities($this->attr('class'));
$this->addClass('InputfieldPasswordComplexify'); $this->addClass('InputfieldPasswordComplexify');
@@ -369,6 +377,11 @@ class InputfieldPassword extends InputfieldText {
$numErrors++; $numErrors++;
} }
if(in_array(self::requireNone, $requirements)) {
// early exit if all following requirements are disabled
return $numErrors === 0;
}
if(in_array(self::requireLetter, $requirements)) { if(in_array(self::requireLetter, $requirements)) {
// if(!preg_match('/[a-zA-Z]/', $value)) { // if(!preg_match('/[a-zA-Z]/', $value)) {
if(!preg_match('/\p{L}/', $value)) { if(!preg_match('/\p{L}/', $value)) {
@@ -441,7 +454,9 @@ class InputfieldPassword extends InputfieldText {
foreach($this->getSetting('requirementsLabels') as $value => $label) { foreach($this->getSetting('requirementsLabels') as $value => $label) {
$f->addOption($value, $label); $f->addOption($value, $label);
} }
$f->attr('value', $this->getSetting('requirements')); $value = $this->getSetting('requirements');
if(in_array(self::requireNone, $value)) $value = array('none');
$f->attr('value', $value);
$f->columnWidth = 50; $f->columnWidth = 50;
$inputfields->add($f); $inputfields->add($f);
@@ -459,6 +474,7 @@ class InputfieldPassword extends InputfieldText {
$f->attr('name', 'complexifyFactor'); $f->attr('name', 'complexifyFactor');
$f->label = $this->_('Complexify factor'); $f->label = $this->_('Complexify factor');
$f->description = $this->_('Lower numbers allow weaker passwords, higher numbers require stronger passwords.'); $f->description = $this->_('Lower numbers allow weaker passwords, higher numbers require stronger passwords.');
$f->description .= ' ' . $this->_('Specify -1 to disable this feature.');
$f->notes = $this->_('We recommend something between 0.5 and 1.0'); $f->notes = $this->_('We recommend something between 0.5 and 1.0');
$f->attr('value', $this->complexifyFactor); $f->attr('value', $this->complexifyFactor);
$f->columnWidth = 50; $f->columnWidth = 50;
@@ -468,6 +484,7 @@ class InputfieldPassword extends InputfieldText {
$f->attr('name', 'minlength'); $f->attr('name', 'minlength');
$f->label = $this->_('Minimum password length'); $f->label = $this->_('Minimum password length');
$f->attr('value', $this->attr('minlength')); $f->attr('value', $this->attr('minlength'));
$f->attr('min', 3);
$f->columnWidth = 50; $f->columnWidth = 50;
$inputfields->add($f); $inputfields->add($f);