浏览代码

Forward port customValidation stuff.

euromark 11 年之前
父节点
当前提交
a5fbc01814

+ 26 - 1
docs/Behavior/Passwordable.md

@@ -25,7 +25,8 @@ Also capable of:
 - 'allowSame' => true, // Don't allow the old password on change
 - 'minLength' => PWD_MIN_LENGTH,
 - 'maxLength' => PWD_MAX_LENGTH,
-- 'validator' => 'default'
+- 'validator' => 'default',
+- 'customValidation' => null, // Custom validation rule(s) for the formField on top
 
 You can either pass those to the behavior at runtime, or globally via Configure and `app.php`:
 ```php
@@ -160,3 +161,27 @@ public function login() {
 }
 ```
 Note that the `passwordHasher` config has been set here globabally to assert the Fallback hasher class to kick in.
+
+
+### Adding custom validation rules on top
+If the default rules don't satisfy your needs, you can add some more on top:
+```php
+$rules = array('validateCustom' => array(
+		'rule' => array('custom', '#^[a-z0-9]+$#'), // Just a test example, never use this regex!
+		'message' => __('Foo Bar'),
+		'last' => true,
+	),
+	'validateCustomExt' => array(
+		'rule' => array('custom', '#^[a-z]+$#'), // Just a test example, never use this regex!
+		'message' => __('Foo Bar Ext'),
+		'last' => true,
+	)
+);
+$this->User->Behaviors->load('Tools.Passwordable', array(
+	'customValidation' => $rules));
+```
+But please do NOT use the above regex examples. Also never try to limit the chars to only a subset of characters.
+Always allow [a-z], [0-9] and ALL special chars a user can possibly type in.
+Regex rules can be useful to assert that the password is strong enough, though. That means, that it contains not just letters/numbers, but
+also some special chars. This would be way more secure and useful. But also try to be reasonable here, some developers tend to overreach here,
+making it very annoying to set up passwords.

+ 26 - 0
docs/Contributing.md

@@ -0,0 +1,26 @@
+# Contributing
+
+## Getting Started
+
+* Make sure you have a [GitHub account](https://github.com/signup/free)
+* Fork the repository on GitHub.
+
+## Making Changes
+
+I am looking forward to your contributions. There are several ways to help out:
+* Write missing testcases
+* Write patches for bugs/features, preferably with testcases included
+
+There are a few guidelines that I need contributors to follow:
+* Coding standards (see link below)
+* Passing tests (you can enable travis to assert your changes pass) for Windows and Unix
+
+Protip: Use my [MyCakePHP](https://github.com/dereuromark/cakephp-codesniffer/tree/master/Vendor/PHP/CodeSniffer/Standards/MyCakePHP) sniffs to
+assert coding standards are met. You can either use this pre-build repo and the convenience shell command `cake CodeSniffer.CodeSniffer run -p Tools --standard=MyCakePHP` or the manual `phpcs --standard=MyCakePHP /path/to/Tools`.
+
+# Additional Resources
+
+* [Coding standards guide (extending/overwriting the CakePHP ones)](https://github.com/php-fig-rectified/fig-rectified-standards/)
+* [CakePHP coding standards](http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html)
+* [General GitHub documentation](http://help.github.com/)
+* [GitHub pull request documentation](http://help.github.com/send-pull-requests/)

+ 5 - 0
docs/README.md

@@ -90,3 +90,8 @@ To test a specific file:
 
 	phpunit /path/to/class.php
 
+
+## Contributing
+Your help is greatly appreciated.
+
+* See [Contributing](Contributing.md) for details.

+ 7 - 7
src/Model/Behavior/PasswordableBehavior.php

@@ -49,7 +49,8 @@ class PasswordableBehavior extends Behavior {
 		'allowSame' => true, // Don't allow the old password on change
 		'minLength' => PWD_MIN_LENGTH,
 		'maxLength' => PWD_MAX_LENGTH,
-		'validator' => 'default'
+		'validator' => 'default',
+		'customValidation' => null // Custom validation rule(s) for the formField
 	);
 
 	/**
@@ -65,12 +66,6 @@ class PasswordableBehavior extends Behavior {
 			)
 		),
 		'formFieldRepeat' => array(
-			'between' => array(
-				'rule' => array('lengthBetween', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
-				'message' => array('valErrBetweenCharacters {0} {1}', PWD_MIN_LENGTH, PWD_MAX_LENGTH),
-				'last' => true,
-				//'provider' => 'table'
-			),
 			'validateIdentical' => array(
 				'rule' => array('validateIdentical', ['compare' => 'formField']),
 				'message' => 'valErrPwdNotMatch',
@@ -195,6 +190,11 @@ class PasswordableBehavior extends Behavior {
 				$validator->allowEmpty($formField, !$this->_config['require']);
 			}
 		}
+
+		// Add custom rule(s) if configured
+		if ($this->_config['customValidation']) {
+			$validator->add($formField, $this->_config['customValidation']);
+		}
 	}
 
 	/**

+ 58 - 1
tests/TestCase/Model/Behavior/PasswordableBehaviorTest.php

@@ -632,7 +632,6 @@ class PasswordableBehaviorTest extends TestCase {
 		$this->assertFalse($result);
 		$expected = array(
 			'pwd' => array('between' => __d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6)),
-			'pwd_repeat' => array('between' =>__d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6))
 		);
 		$this->assertEquals($expected, $user->errors());
 	}
@@ -666,4 +665,62 @@ class PasswordableBehaviorTest extends TestCase {
 		$this->assertTrue($hash !== $hash2);
 	}
 
+	/**
+	 * PasswordableBehaviorTest::testValidateCustomRule()
+	 *
+	 * @return void
+	 */
+	public function testValidateCustomRule() {
+		$rules = array(
+			'validateCustom' => array(
+				'rule' => array('custom', '#^[a-z0-9]+$#'), // Just a test example, never use this regexp!
+				'message' => 'Foo Bar',
+				'last' => true,
+			),
+			'validateCustomExt' => array(
+				'rule' => array('custom', '#^[a-z]+$#'), // Just a test example, never use this regexp!
+				'message' => 'Foo Bar Ext',
+				'last' => true,
+			)
+		);
+		$this->Users->addBehavior('Tools.Passwordable', array(
+			'customValidation' => $rules));
+
+		$user = $this->Users->newEntity();
+		$data = array(
+			'pwd' => '%123456',
+			'pwd_repeat' => '%123456'
+		);
+		$this->Users->patchEntity($user, $data);
+
+		$is = $this->Users->save($user);
+		$this->assertFalse($is);
+
+		$result = $user->errors();
+		$expected = array('pwd' => array('validateCustom' => 'Foo Bar'));
+		$this->assertSame($expected, $result);
+
+		$user = $this->Users->newEntity();
+		$data = array(
+			'pwd' => 'abc123',
+			'pwd_repeat' => 'abc123'
+		);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		$this->assertFalse($is);
+
+		$result = $user->errors();
+		$expected = array('pwd' => array('validateCustomExt' => 'Foo Bar Ext'));
+		$this->assertSame($expected, $result);
+
+		$user = $this->Users->newEntity();
+		$data = array(
+			'pwd' => 'abcdef',
+			'pwd_repeat' => 'abcdef'
+		);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		$this->assertTrue((bool)$is);
+	}
+
 }