Yii Framework Forum: Password strength checker for YII - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Password strength checker for YII Rate Topic: -----

#1 User is offline   Q-efx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 12-April 09

Posted 24 May 2009 - 10:22 AM

Hello,

a "dirty" password strength checker for Yii. Easiest way to use it to drop it into "components" folder. with 'minLevel' => 3 . You set the minlevel.

Some info: For the freedom of the user you shouldnt set the lelvel to high. Only if you sell things have auctions or something similar "important" things. You should bind the user to a "secure" password.


<?php
/*
* A password strength checker!
* Drop it where you like it :)
*
* LINK: www.u-r-reality.de
*/

class EPassValidator extends CValidator
{
    public $minLevel;

    private $password;
    private $lvl;
    private $length;

    protected function validateAttribute($object, $attribute)
    {
        if(!$object->hasErrors())
        {
            $this->password = $object->$attribute;

            $this->minLevel = $this->minLevel ? $this->minLevel : 3;

            echo $this->minLevel;

            if($this->getLevel($this->password) < $this->minLevel)
            {
                $message = Yii::t('yii','The password is not secure enough.');
                $this->addError($object,$attribute,$message);
            }
        }
    }

    protected function getLevel($password)
    {
        if ( strlen( $password ) == 0 )
            return 1;

        $this->lvl = 0;

        $this->length = strlen($password);

        if(strtolower($password) != $password)
        $this->lvl += 2;

        if(strtoupper($password) == $password)
        $this->lvl += 2;

        if($this->length >= 8 && $this->length <= 15)
        $this->lvl += 2;

        if($this->length >= 16 && $this->length <=35)
        $this->lvl += 3;

        if($this->length > 35)
        $this->lvl += 4;

        preg_match_all('/[0-9]/', $password, $numbers);
        $this->lvl += count($numbers[0]);

        preg_match_all('/[|!@#$%&*/=?,;.:-_+~^\]/', $password, $specialchars);
        $this->lvl += sizeof($specialchars[0]);

        $chars = str_split($password);
        $num_unique_chars = sizeof( array_unique($chars) );
        $this->lvl += $num_unique_chars * 2;

        $this->lvl = $this->lvl > 99 ? 99 : $this->lvl;
        $this->lvl = floor($this->lvl / 10 + 1);

        return $this->lvl;
    }

}
?>


Do what ever you like with it. Just keep the link. And dont claim it as your own :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users