[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/system/js/fields/ -> passwordstrength.js (source)

   1  /**
   2   * PasswordStrength script by Thomas Kjaergaard
   3   * License: MIT
   4   * Repo: https://github.com/tkjaergaard/Password-Strength
   5   *
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2014 Thomas Kjærgaard
   9   *
  10   * Permission is hereby granted, free of charge, to any person obtaining a copy
  11   * of this software and associated documentation files (the "Software"), to deal
  12   * in the Software without restriction, including without limitation the rights
  13   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14   * copies of the Software, and to permit persons to whom the Software is
  15   * furnished to do so, subject to the following conditions:
  16   *
  17   * The above copyright notice and this permission notice shall be included in all
  18   * copies or substantial portions of the Software.
  19   *
  20   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26   * SOFTWARE.
  27   */
  28  class PasswordStrength {
  29    constructor(settings) {
  30      this.lowercase = parseInt(settings.lowercase, 10) || 0;
  31      this.uppercase = parseInt(settings.uppercase, 10) || 0;
  32      this.numbers = parseInt(settings.numbers, 10) || 0;
  33      this.special = parseInt(settings.special, 10) || 0;
  34      this.length = parseInt(settings.length, 10) || 12;
  35    }
  36  
  37    getScore(value) {
  38      let score = 0;
  39      let mods = 0;
  40      const sets = ['lowercase', 'uppercase', 'numbers', 'special', 'length'];
  41      sets.forEach(set => {
  42        if (this[set] > 0) {
  43          mods += 1;
  44        }
  45      });
  46      score += this.constructor.calc(value, /[a-z]/g, this.lowercase, mods);
  47      score += this.constructor.calc(value, /[A-Z]/g, this.uppercase, mods);
  48      score += this.constructor.calc(value, /[0-9]/g, this.numbers, mods); // eslint-disable-next-line no-useless-escape
  49  
  50      score += this.constructor.calc(value, /[$!#?=;:*\-_€%&()`´]/g, this.special, mods);
  51  
  52      if (mods === 1) {
  53        score += value.length > this.length ? 100 : 100 / this.length * value.length;
  54      } else {
  55        score += value.length > this.length ? 100 / mods : 100 / mods / this.length * value.length;
  56      }
  57  
  58      return score;
  59    }
  60  
  61    static calc(value, pattern, length, mods) {
  62      const count = value.match(pattern);
  63  
  64      if (count && count.length > length && length !== 0) {
  65        return 100 / mods;
  66      }
  67  
  68      if (count && length > 0) {
  69        return 100 / mods / length * count.length;
  70      }
  71  
  72      return 0;
  73    }
  74  
  75  }
  76  /**
  77   * @copyright  (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
  78   * @license    GNU General Public License version 2 or later; see LICENSE.txt
  79   */
  80  
  81  
  82  ((Joomla, document) => {
  83    // Method to check the input and set the meter
  84    const getMeter = element => {
  85      const meter = document.querySelector('meter');
  86      const minLength = element.getAttribute('data-min-length');
  87      const minIntegers = element.getAttribute('data-min-integers');
  88      const minSymbols = element.getAttribute('data-min-symbols');
  89      const minUppercase = element.getAttribute('data-min-uppercase');
  90      const minLowercase = element.getAttribute('data-min-lowercase');
  91      const strength = new PasswordStrength({
  92        lowercase: minLowercase || 0,
  93        uppercase: minUppercase || 0,
  94        numbers: minIntegers || 0,
  95        special: minSymbols || 0,
  96        length: minLength || 12
  97      });
  98      const score = strength.getScore(element.value);
  99      const i = meter.getAttribute('id').replace(/^\D+/g, '');
 100      const label = element.parentNode.parentNode.querySelector(`#password-$i}`);
 101  
 102      if (score === 100) {
 103        label.innerText = Joomla.Text._('JFIELD_PASSWORD_INDICATE_COMPLETE');
 104      } else {
 105        label.innerText = Joomla.Text._('JFIELD_PASSWORD_INDICATE_INCOMPLETE');
 106      }
 107  
 108      meter.value = score;
 109  
 110      if (!element.value.length) {
 111        label.innerText = '';
 112        element.setAttribute('required', '');
 113      }
 114    };
 115  
 116    document.addEventListener('DOMContentLoaded', () => {
 117      const fields = [].slice.call(document.querySelectorAll('.js-password-strength')); // Loop  through the fields
 118  
 119      fields.forEach((field, index) => {
 120        let initialVal = '';
 121  
 122        if (!field.value.length) {
 123          initialVal = 0;
 124        } // Create a progress meter and the label
 125  
 126  
 127        const meter = document.createElement('meter');
 128        meter.setAttribute('id', `progress-$index}`);
 129        meter.setAttribute('min', 0);
 130        meter.setAttribute('max', 100);
 131        meter.setAttribute('low', 40);
 132        meter.setAttribute('high', 99);
 133        meter.setAttribute('optimum', 100);
 134        meter.value = initialVal;
 135        const label = document.createElement('div');
 136        label.setAttribute('class', 'text-center');
 137        label.setAttribute('id', `password-$index}`);
 138        label.setAttribute('aria-live', 'polite');
 139        field.parentNode.insertAdjacentElement('afterEnd', label);
 140        field.parentNode.insertAdjacentElement('afterEnd', meter); // Add a data attribute for the required
 141  
 142        if (field.value.length > 0) {
 143          field.setAttribute('required', true);
 144        } // Add a listener for input data change
 145  
 146  
 147        field.addEventListener('keyup', ({
 148          target
 149        }) => {
 150          getMeter(target);
 151        });
 152      }); // Set a handler for the validation script
 153  
 154      if (fields[0]) {
 155        document.formvalidator.setHandler('password-strength', value => {
 156          const strengthElements = document.querySelectorAll('.js-password-strength');
 157          const minLength = strengthElements[0].getAttribute('data-min-length');
 158          const minIntegers = strengthElements[0].getAttribute('data-min-integers');
 159          const minSymbols = strengthElements[0].getAttribute('data-min-symbols');
 160          const minUppercase = strengthElements[0].getAttribute('data-min-uppercase');
 161          const minLowercase = strengthElements[0].getAttribute('data-min-lowercase');
 162          const strength = new PasswordStrength({
 163            lowercase: minLowercase || 0,
 164            uppercase: minUppercase || 0,
 165            numbers: minIntegers || 0,
 166            special: minSymbols || 0,
 167            length: minLength || 12
 168          });
 169          const score = strength.getScore(value);
 170  
 171          if (score === 100) {
 172            return true;
 173          }
 174  
 175          return false;
 176        });
 177      }
 178    });
 179  })(Joomla, document);


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer