yii-user-management /user/components/YumPasswordValidator.php

Language PHP Lines 174
MD5 Hash c97e6f3d6935ea3fa909599c58b52228
Repository https://github.com/yii-simon/yii-user-management.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?

/**
 * YumPasswordValidator class file.
 *
 * @author Alexander Sieburg <alex.sieburg@gmail.com>
 * @link http://code.google.com/p/qwerty-yii-extensions/source/browse/trunk/PasswordValidator/
 * @license GPL v3
 */

/**
 * YumPasswordValidator validates that the attribute value is a valid password.
 *
 * Future plans:
 * validate against dictionary
 * validate against password history
 * validate consecutive strings like abcdefgh, 123456...
 *
 * @author Alexander Sieburg <alex.sieburg@gmail.com>
 * @version 0.01
 */
class YumPasswordValidator extends CValidator
{
  /*
   * current yii enconding for use in mb string functions.
   */

  public $encoding;

  /*
   * $var int minimum required password length.
   */
  public $minLen = 8;

  /*
   * $var int maximum allowed password length.
   */
  public $maxLen = 0;

  /*
   * $var int minimum required upper case characters.
   */
  public $minUpperCase = 0;

  /*
   * $var int minimum required lower case characters.
   */
  public $minLowerCase = 0;

  /*
   * $var int minimum required numeric characters.
   */
  public $minDigits = 0;

  /*
   * $var int minimum required symbols (e.g: !"§$%&/()=?.....).
   */
  public $minSym = 0;

  /*
   * $var bool allow whitespaces.
   */
  public $allowWhiteSpace = false;

  /*
   * $var int maximum character repetition.
   */
  public $maxRepetition = 0;

  protected function validateAttribute($object, $attribute)
  {
    //$this->encoding = Yii::app()->charset;

    $value = $object->$attribute;

    if ($this->minLen > 0)
    {
      if (strlen($value) < $this->minLen)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} is too short (min. {num} characters).',
            array('{num}' => $this->minLen));

        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->maxLen > 0)
    {
      if (strlen($value) > $this->maxLen)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} is too long (max. {num} characters).',
            array('{num}' => $this->maxLen));

        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->minLowerCase > 0)
    {
      if (preg_match_all('/[a-z]/', $value, $matches) < $this->minLowerCase)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must include at least {num} lower case letters.',
            array('{num}' => $this->minLowerCase));

        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->minUpperCase > 0)
    {
      if (preg_match_all('/[A-Z]/', $value, $matches) < $this->minUpperCase)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must include at least {num} upper case letters.',
            array('{num}' => $this->minUpperCase));

        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->minDigits > 0)
    {
      if (preg_match_all('/[0-9]/', $value, $matches) < $this->minDigits)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must include at least {num} digits.',
            array('{num}' => $this->minDigits));
        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->minSym > 0)
    {
      if (preg_match_all('/\W/', $value, $matches) < $this->minSym)
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must include at least {num} symbols.',
            array('{num}' => $this->minSym));

        $this->addError($object, $attribute, $message);
      }
    }

    if (!$this->allowWhiteSpace)
    {
      if (preg_match('/\s/', $value))
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must not contain whitespace.',
            array('{num}' => $this->minSym));

        $this->addError($object, $attribute, $message);
      }
    }

    if ($this->maxRepetition > 0)
    {
      if (preg_match('/(.){1}\\1{' . $this->maxRepetition . ',}/', $value))
      {
        $message = $this->message !== null ? $this->message : Yii::t('UserModule.YumPasswordValidator',
            '{attribute} must not contain more than {num} sequentially repeated characters.',
            array('{num}' => $this->maxRepetition + 1));

        $this->addError($object, $attribute, $message);
      }
    }
  }

}
?>
Back to Top