StrongPassword.php #1

  • //
  • guest/
  • perforce_software/
  • chronicle/
  • main/
  • library/
  • P4/
  • Validate/
  • StrongPassword.php
  • View
  • Commits
  • Open Download .zip Download (2 KB)
<?php
/**
 * Provides password strength validation according
 * to server security requirements
 *
 * @copyright   2011 Perforce Software. All rights reserved.
 * @license     Please see LICENSE.txt in top-level folder of this distribution.
 * @version     <release>/<patch>
 */
class P4_Validate_StrongPassword extends P4_Validate_Abstract
{
    const       WEAK_PASSWORD       = 'weakPassword';

    protected   $_messageTemplates  = null;
    
    /**
     * Initialize the message templates.
     */
    public function __construct()
    {
        $this->_messageTemplates = array(
            self::WEAK_PASSWORD =>
                "Passwords must be at least 8 characters long and contain "
              . "mixed case or both alphabetic and non-alphabetic characters."
        );
    }

    /**
     * Check if value is strong password according
     * to server requirements for strong password.
     *
     * @param string $value password
     * @return boolean
     */
    public function isValid($value)
    {
        $this->_setValue($value);

        $conditionsMet = 0;
        if ($this->_containsUppercaseLetter($value)) {
            $conditionsMet++;
        }
        if ($this->_containsLowercaseLetter($value)) {
            $conditionsMet++;
        }
        if ($this->_containsNonAlphabetic($value)) {
            $conditionsMet++;
        }

        if (strlen($value) < 8 || $conditionsMet < 2) {
            $this->_error(self::WEAK_PASSWORD);
            return false;
        }

        return true;
    }

    /**
     * Return true if value contains at least one uppercase letter,
     * otherwise return false.
     *
     * @param string $value password
     * @return boolean
     */
    protected function _containsUppercaseLetter($value)
    {
        return preg_match("/[A-Z]+/", $value);
    }

    /**
     * Return true if value contains at least one lowercase letter,
     * otherwise return false.
     *
     * @param string $value password
     * @return boolean
     */
    protected function _containsLowercaseLetter($value)
    {
        return preg_match("/[a-z]+/", $value);
    }

    /**
     * Return true if value contains at least one nonalphabetic character,
     * otherwise return false.
     *
     * @param string $value password
     * @return boolean
     */
    protected function _containsNonAlphabetic($value)
    {
        return preg_match("/[^a-zA-Z]+/", $value);
    }
}
# Change User Description Committed
#1 16170 perforce_software Move Chronicle files to follow new path scheme for branching.
//guest/perforce_software/chronicle/library/P4/Validate/StrongPassword.php
#1 8972 Matt Attaway Initial add of the Chronicle source code