Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New validators #566

Merged
merged 4 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Library/Phalcon/Validation/Validator/ConfirmationOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: David Hubner <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Validation\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Validator;

/**
* Validates confirmation of other field value
*
* <code>
* new \Phalcon\Validation\Validator\ConfirmationOf([
* 'origField' => {string - original field attribute},
* 'message' => {string - validation message},
* 'allowEmpty' => {bool - allow empty value}
* ])
* </code>
*
* @package Phalcon\Validation\Validator
*/
class ConfirmationOf extends Validator
{

/**
* Value validation
*
* @param \Phalcon\Validation $validation - validation object
* @param string $attribute - validated attribute
* @return bool
* @throws \Phalcon\Validation\Exception
*/
public function validate(Validation $validation, $attribute)
{
if (!$this->hasOption('origField')) {
throw new Exception('Original field must be set');
}

$allowEmpty = $this->getOption('allowEmpty');
$value = $validation->getValue($attribute);

if ($allowEmpty && ((is_scalar($value) && (string) $value === '') || is_null($value))) {
return true;
}

$origField = $this->getOption('origField');
$origValue = $validation->getValue($origField);

if (is_string($value) && $value == $origValue) {
return true;
}

$message = ($this->hasOption('message') ? $this->getOption('message') : 'Value not confirmed');

$validation->appendMessage(
new Validation\Message($message, $attribute, 'ConfirmationOfValidator')
);

return false;
}
}
110 changes: 110 additions & 0 deletions Library/Phalcon/Validation/Validator/PasswordStrength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: David Hubner <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Validation\Validator;

use Phalcon\Validation;

/**
* Validates password strength
*
* <code>
* new \Phalcon\Validation\Validator\PasswordStrength([
* 'minScore' => {[1-4] - minimal password score},
* 'message' => {string - validation message},
* 'allowEmpty' => {bool - allow empty value}
* ])
* </code>
*
* @package Phalcon\Validation\Validator
*/
class PasswordStrength extends Validation\Validator
{

const MIN_VALID_SCORE = 2;

/**
* Value validation
*
* @param \Phalcon\Validation $validation - validation object
* @param string $attribute - validated attribute
* @return bool
*/
public function validate(Validation $validation, $attribute)
{
$allowEmpty = $this->getOption('allowEmpty');
$value = $validation->getValue($attribute);

if ($allowEmpty && ((is_scalar($value) && (string) $value === '') || is_null($value))) {
return true;
}

$minScore = ($this->hasOption('minScore') ? $this->getOption('minScore') : self::MIN_VALID_SCORE);

if (is_string($value) && $this->countScore($value) >= $minScore) {
return true;
}

$message = ($this->hasOption('message') ? $this->getOption('message') : 'Password too weak');

$validation->appendMessage(
new Validation\Message($message, $attribute, 'PasswordStrengthValidator')
);

return false;
}

/**
* Calculates password strength score
*
* @param string $value - password
* @return int (1 = very weak, 2 = weak, 3 = medium, 4+ = strong)
*/
private function countScore($value)
{
$score = 0;
$hasLower = preg_match('![a-z]!', $value);
$hasUpper = preg_match('![A-Z]!', $value);
$hasNumber = preg_match('![0-9]!', $value);

if ($hasLower && $hasUpper) {
++$score;
}
if (($hasNumber && $hasLower) || ($hasNumber && $hasUpper)) {
++$score;
}
if (preg_match('![^0-9a-zA-Z]!', $value)) {
++$score;
}

$length = mb_strlen($value);

if ($length >= 16) {
$score += 2;
} elseif ($length >= 8) {
++$score;
} elseif ($length <= 4 && $score > 1) {
--$score;
} elseif ($length > 0 && $score === 0) {
++$score;
}

return $score;
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ See [CONTRIBUTING.md](docs/CONTRIBUTING.md)
* [Phalcon\Avatar\Gravatar](Library/Phalcon/Avatar) - Provides an easy way to retrieve a user's profile image from Gravatar site based on a given email address (@sergeyklay)

### Validators
* [Phalcon\Validation\Validator\ConfirmationOf](Library/Phalcon/Validation/Validator) - Validates confirmation of other field value (@davihu)
* [Phalcon\Validation\Validator\MongoId](Library/Phalcon/Validation/Validator) - Validate MongoId value (@Kachit)
* [Phalcon\Validation\Validator\PasswordStrength](Library/Phalcon/Validation/Validator) - Validates password strength (@davihu)

## License

Expand Down
91 changes: 91 additions & 0 deletions tests/unit/Validation/Validator/ConfirmationOfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: David Hubner <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Test\Validation\Validator;

use Codeception\TestCase\Test;
use Codeception\Util\Stub;
use Phalcon\Validation\Validator\ConfirmationOf;

class ConfirmationOfTest extends Test
{

protected function _before()
{
}

protected function _after()
{
}

public function testValidateExceptionWithoutOrigField()
{
$validation = Stub::make('Phalcon\Validation');
$validator = new ConfirmationOf();
$this->setExpectedException('Phalcon\Validation\Exception');
$validator->validate($validation, 'confirmation');
}

public function testValidateSameAsOrig()
{
$validation = Stub::make('Phalcon\Validation', array('getValue' => 'value'));
$validator = new ConfirmationOf(array(
'origField' => 'original'
));
$this->assertTrue($validator->validate($validation, 'confirmation'));
}

public function testValidateNotSameAsOrig()
{
$validation = Stub::make('Phalcon\Validation', array('getValue' => Stub::consecutive('val1', 'val2'), 'appendMessage' => true));
$validator = new ConfirmationOf(array(
'origField' => 'original'
));
$this->assertFalse($validator->validate($validation, 'confirmation'));
}

public function testValidateAllowEmpty()
{
$validation = Stub::make('Phalcon\Validation', array('getValue' => Stub::consecutive('', 'val2')));
$validator = new ConfirmationOf(array(
'origField' => 'original',
'allowEmpty' => true
));
$this->assertTrue($validator->validate($validation, 'confirmation'));
}

public function testValidateNotAllowEmpty()
{
$validation = Stub::make('Phalcon\Validation', array('getValue' => Stub::consecutive('', 'val2'), 'appendMessage' => true));
$validator = new ConfirmationOf(array(
'origField' => 'original',
'allowEmpty' => false
));
$this->assertFalse($validator->validate($validation, 'confirmation'));
}

public function testValidateInvalidValue()
{
$validation = Stub::make('Phalcon\Validation', array('getValue' => array('value', 'value'), 'appendMessage' => true));
$validator = new ConfirmationOf(array(
'origField' => 'original'
));
$this->assertFalse($validator->validate($validation, 'confirmation'));
}
}
Loading