user.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings) {
  8. Drupal.behaviors.password = {
  9. attach: function attach(context, settings) {
  10. var $passwordInput = $(context).find('input.js-password-field').once('password');
  11. if ($passwordInput.length) {
  12. var translate = settings.password;
  13. var $passwordInputParent = $passwordInput.parent();
  14. var $passwordInputParentWrapper = $passwordInputParent.parent();
  15. var $passwordSuggestions = void 0;
  16. $passwordInputParent.addClass('password-parent');
  17. $passwordInputParentWrapper.find('input.js-password-confirm').parent().append('<div aria-live="polite" aria-atomic="true" class="password-confirm js-password-confirm">' + translate.confirmTitle + ' <span></span></div>').addClass('confirm-parent');
  18. var $confirmInput = $passwordInputParentWrapper.find('input.js-password-confirm');
  19. var $confirmResult = $passwordInputParentWrapper.find('div.js-password-confirm');
  20. var $confirmChild = $confirmResult.find('span');
  21. if (settings.password.showStrengthIndicator) {
  22. var passwordMeter = '<div class="password-strength"><div class="password-strength__meter"><div class="password-strength__indicator js-password-strength__indicator"></div></div><div aria-live="polite" aria-atomic="true" class="password-strength__title">' + translate.strengthTitle + ' <span class="password-strength__text js-password-strength__text"></span></div></div>';
  23. $confirmInput.parent().after('<div class="password-suggestions description"></div>');
  24. $passwordInputParent.append(passwordMeter);
  25. $passwordSuggestions = $passwordInputParentWrapper.find('div.password-suggestions').hide();
  26. }
  27. var passwordCheckMatch = function passwordCheckMatch(confirmInputVal) {
  28. var success = $passwordInput.val() === confirmInputVal;
  29. var confirmClass = success ? 'ok' : 'error';
  30. $confirmChild.html(translate['confirm' + (success ? 'Success' : 'Failure')]).removeClass('ok error').addClass(confirmClass);
  31. };
  32. var passwordCheck = function passwordCheck() {
  33. if (settings.password.showStrengthIndicator) {
  34. var result = Drupal.evaluatePasswordStrength($passwordInput.val(), settings.password);
  35. if ($passwordSuggestions.html() !== result.message) {
  36. $passwordSuggestions.html(result.message);
  37. }
  38. $passwordSuggestions.toggle(result.strength !== 100);
  39. $passwordInputParent.find('.js-password-strength__indicator').css('width', result.strength + '%').removeClass('is-weak is-fair is-good is-strong').addClass(result.indicatorClass);
  40. $passwordInputParent.find('.js-password-strength__text').html(result.indicatorText);
  41. }
  42. if ($confirmInput.val()) {
  43. passwordCheckMatch($confirmInput.val());
  44. $confirmResult.css({ visibility: 'visible' });
  45. } else {
  46. $confirmResult.css({ visibility: 'hidden' });
  47. }
  48. };
  49. $passwordInput.on('input', passwordCheck);
  50. $confirmInput.on('input', passwordCheck);
  51. }
  52. }
  53. };
  54. Drupal.evaluatePasswordStrength = function (password, translate) {
  55. password = password.trim();
  56. var indicatorText = void 0;
  57. var indicatorClass = void 0;
  58. var weaknesses = 0;
  59. var strength = 100;
  60. var msg = [];
  61. var hasLowercase = /[a-z]/.test(password);
  62. var hasUppercase = /[A-Z]/.test(password);
  63. var hasNumbers = /[0-9]/.test(password);
  64. var hasPunctuation = /[^a-zA-Z0-9]/.test(password);
  65. var $usernameBox = $('input.username');
  66. var username = $usernameBox.length > 0 ? $usernameBox.val() : translate.username;
  67. if (password.length < 12) {
  68. msg.push(translate.tooShort);
  69. strength -= (12 - password.length) * 5 + 30;
  70. }
  71. if (!hasLowercase) {
  72. msg.push(translate.addLowerCase);
  73. weaknesses++;
  74. }
  75. if (!hasUppercase) {
  76. msg.push(translate.addUpperCase);
  77. weaknesses++;
  78. }
  79. if (!hasNumbers) {
  80. msg.push(translate.addNumbers);
  81. weaknesses++;
  82. }
  83. if (!hasPunctuation) {
  84. msg.push(translate.addPunctuation);
  85. weaknesses++;
  86. }
  87. switch (weaknesses) {
  88. case 1:
  89. strength -= 12.5;
  90. break;
  91. case 2:
  92. strength -= 25;
  93. break;
  94. case 3:
  95. strength -= 40;
  96. break;
  97. case 4:
  98. strength -= 40;
  99. break;
  100. }
  101. if (password !== '' && password.toLowerCase() === username.toLowerCase()) {
  102. msg.push(translate.sameAsUsername);
  103. strength = 5;
  104. }
  105. if (strength < 60) {
  106. indicatorText = translate.weak;
  107. indicatorClass = 'is-weak';
  108. } else if (strength < 70) {
  109. indicatorText = translate.fair;
  110. indicatorClass = 'is-fair';
  111. } else if (strength < 80) {
  112. indicatorText = translate.good;
  113. indicatorClass = 'is-good';
  114. } else if (strength <= 100) {
  115. indicatorText = translate.strong;
  116. indicatorClass = 'is-strong';
  117. }
  118. msg = translate.hasWeaknesses + '<ul><li>' + msg.join('</li><li>') + '</li></ul>';
  119. return {
  120. strength: strength,
  121. message: msg,
  122. indicatorText: indicatorText,
  123. indicatorClass: indicatorClass
  124. };
  125. };
  126. })(jQuery, Drupal, drupalSettings);