PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/user/components/YumPasswordValidator.php

https://github.com/yii-simon/yii-user-management
PHP | 173 lines | 98 code | 29 blank | 46 comment | 16 complexity | c97e6f3d6935ea3fa909599c58b52228 MD5 | raw file
  1. <?
  2. /**
  3. * YumPasswordValidator class file.
  4. *
  5. * @author Alexander Sieburg <alex.sieburg@gmail.com>
  6. * @link http://code.google.com/p/qwerty-yii-extensions/source/browse/trunk/PasswordValidator/
  7. * @license GPL v3
  8. */
  9. /**
  10. * YumPasswordValidator validates that the attribute value is a valid password.
  11. *
  12. * Future plans:
  13. * validate against dictionary
  14. * validate against password history
  15. * validate consecutive strings like abcdefgh, 123456...
  16. *
  17. * @author Alexander Sieburg <alex.sieburg@gmail.com>
  18. * @version 0.01
  19. */
  20. class YumPasswordValidator extends CValidator
  21. {
  22. /*
  23. * current yii enconding for use in mb string functions.
  24. */
  25. public $encoding;
  26. /*
  27. * $var int minimum required password length.
  28. */
  29. public $minLen = 8;
  30. /*
  31. * $var int maximum allowed password length.
  32. */
  33. public $maxLen = 0;
  34. /*
  35. * $var int minimum required upper case characters.
  36. */
  37. public $minUpperCase = 0;
  38. /*
  39. * $var int minimum required lower case characters.
  40. */
  41. public $minLowerCase = 0;
  42. /*
  43. * $var int minimum required numeric characters.
  44. */
  45. public $minDigits = 0;
  46. /*
  47. * $var int minimum required symbols (e.g: !"§$%&/()=?.....).
  48. */
  49. public $minSym = 0;
  50. /*
  51. * $var bool allow whitespaces.
  52. */
  53. public $allowWhiteSpace = false;
  54. /*
  55. * $var int maximum character repetition.
  56. */
  57. public $maxRepetition = 0;
  58. protected function validateAttribute($object, $attribute)
  59. {
  60. //$this->encoding = Yii::app()->charset;
  61. $value = $object->$attribute;
  62. if ($this->minLen > 0)
  63. {
  64. if (strlen($value) < $this->minLen)
  65. {
  66. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  67. '{attribute} is too short (min. {num} characters).',
  68. array('{num}' => $this->minLen));
  69. $this->addError($object, $attribute, $message);
  70. }
  71. }
  72. if ($this->maxLen > 0)
  73. {
  74. if (strlen($value) > $this->maxLen)
  75. {
  76. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  77. '{attribute} is too long (max. {num} characters).',
  78. array('{num}' => $this->maxLen));
  79. $this->addError($object, $attribute, $message);
  80. }
  81. }
  82. if ($this->minLowerCase > 0)
  83. {
  84. if (preg_match_all('/[a-z]/', $value, $matches) < $this->minLowerCase)
  85. {
  86. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  87. '{attribute} must include at least {num} lower case letters.',
  88. array('{num}' => $this->minLowerCase));
  89. $this->addError($object, $attribute, $message);
  90. }
  91. }
  92. if ($this->minUpperCase > 0)
  93. {
  94. if (preg_match_all('/[A-Z]/', $value, $matches) < $this->minUpperCase)
  95. {
  96. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  97. '{attribute} must include at least {num} upper case letters.',
  98. array('{num}' => $this->minUpperCase));
  99. $this->addError($object, $attribute, $message);
  100. }
  101. }
  102. if ($this->minDigits > 0)
  103. {
  104. if (preg_match_all('/[0-9]/', $value, $matches) < $this->minDigits)
  105. {
  106. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  107. '{attribute} must include at least {num} digits.',
  108. array('{num}' => $this->minDigits));
  109. $this->addError($object, $attribute, $message);
  110. }
  111. }
  112. if ($this->minSym > 0)
  113. {
  114. if (preg_match_all('/\W/', $value, $matches) < $this->minSym)
  115. {
  116. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  117. '{attribute} must include at least {num} symbols.',
  118. array('{num}' => $this->minSym));
  119. $this->addError($object, $attribute, $message);
  120. }
  121. }
  122. if (!$this->allowWhiteSpace)
  123. {
  124. if (preg_match('/\s/', $value))
  125. {
  126. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  127. '{attribute} must not contain whitespace.',
  128. array('{num}' => $this->minSym));
  129. $this->addError($object, $attribute, $message);
  130. }
  131. }
  132. if ($this->maxRepetition > 0)
  133. {
  134. if (preg_match('/(.){1}\\1{' . $this->maxRepetition . ',}/', $value))
  135. {
  136. $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
  137. '{attribute} must not contain more than {num} sequentially repeated characters.',
  138. array('{num}' => $this->maxRepetition + 1));
  139. $this->addError($object, $attribute, $message);
  140. }
  141. }
  142. }
  143. }
  144. ?>