Skip to content

Commit

Permalink
Add hash_check validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmudur Rahman committed Mar 3, 2020
1 parent 6e23d6a commit 4970d98
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
vendor
.idea
/.php_cs.cache
56 changes: 56 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Rules we follow are from PSR-2 as well as the rectified PSR-2 guide.
*
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer
* - https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
* - https://github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding-style-guide-additions.md
*
* If something isn't addressed in either of those, some other common community rules are
* used that might not be addressed explicitly in PSR-2 in order to improve code quality
* (so that devs don't need to comment on them in Code Reviews).
*
* For instance: removing trailing white space, removing extra line breaks where
* they're not needed (back to back, beginning or end of function/class, etc.),
* adding trailing commas in the last line of an array, etc.
*/

$finder = PhpCsFixer\Finder::create()
->exclude('node_modules')
->exclude('vendor')
->in(__DIR__);

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => [ 'syntax' => 'short' ],
'binary_operator_spaces' => [ 'align_equals' => false, 'align_double_arrow' => true ],
'cast_spaces' => true,
'combine_consecutive_unsets' => true,
'concat_space' => [ 'spacing' => 'one' ],
'linebreak_after_opening_tag' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_extra_consecutive_blank_lines' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_whitespace_in_blank_line' => true,
'no_spaces_around_offset' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'normalize_index_brace' => true,
'phpdoc_indent' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'single_quote' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'method_argument_space' => ['ensure_fully_multiline' => false],
'no_break_comment' => false,
'blank_line_before_statement' => true,
])
->setUsingCache(false)
->setFinder($finder);
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ $validator = Validator::make(['foo' => 'বার'], ['foo' => new Ascii()]);
$validator->fails() // true
```

### Hash Check validation rule

```php
use Illuminate\Support\Facades\Validator;

$hashedPassword = bcrypt('123456');

$validator = Validator::make(['password' => '123456'], ['password' => 'hash_check:' . $hashedPassword]);
$validator->passes() // true
```

### `number` function

```php
Expand Down
2 changes: 2 additions & 0 deletions src/LaravelUtilitiesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Mahmud\LaravelUtilities\Rules\Ascii;
use Mahmud\LaravelUtilities\Rules\HashCheck;

class LaravelUtilitiesServiceProvider extends ServiceProvider {

public function boot() {
Validator::extend('ascii', Ascii::class . '@passes');
Validator::extend('hash_check', HashCheck::class . '@handle');
}
}
18 changes: 18 additions & 0 deletions src/Rules/HashCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Mahmud\LaravelUtilities\Rules;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Hash;

class HashCheck
{
public function handle($attribute, $value, $parameters, Validator $validator)
{
if (! Hash::check($value, $parameters[0])) {
$validator->errors()->add($attribute, 'Incorrect password');
}

return $validator;
}
}
40 changes: 31 additions & 9 deletions tests/Features/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,52 @@
/**
* Created by MD. Mahmud Ur Rahman <mahmudkuet11@gmail.com>.
*/

namespace Mahmud\LaravelUtilities\Tests\Features;

use Illuminate\Support\Facades\Validator;
use Mahmud\LaravelUtilities\LaravelUtilitiesServiceProvider;
use Mahmud\LaravelUtilities\Rules\Ascii;
use Orchestra\Testbench\TestCase;

class RulesTest extends TestCase {
class RulesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
}

/**
* @test
*/
public function it_accepts_only_ascii_characters_in_a_string() {
public function it_accepts_only_ascii_characters_in_a_string()
{
$validator = Validator::make(['foo' => 'bar'], ['foo' => 'ascii']);

$this->assertFalse($validator->fails());

$validator = Validator::make(['foo' => 'বার'], ['foo' => new Ascii()]);

$this->assertTrue($validator->fails());
$this->assertEquals("The foo must contain ASCII characters", $validator->errors()->get('foo')[0]);
$this->assertEquals('The foo must contain ASCII characters', $validator->errors()->get('foo')[0]);
}

protected function getPackageProviders($app) {

/**
* @test
*/
public function it_checks_users_current_password()
{
$hashedPassword = bcrypt('123456');

$validator = Validator::make(['password' => 'wrong'], ['password' => 'hash_check:' . $hashedPassword], ['hash_check' => 'Test message']);
$this->assertTrue($validator->fails());
$this->assertEquals('Incorrect password', $validator->errors()->first('password'));

$validator = Validator::make(['password' => '123456'], ['password' => 'hash_check:' . $hashedPassword]);
$this->assertTrue($validator->passes());
}

protected function getPackageProviders($app)
{
return [
LaravelUtilitiesServiceProvider::class,
];
Expand Down

0 comments on commit 4970d98

Please sign in to comment.