Skip to content

Commit

Permalink
Merge pull request #222 from michalsn/validation_rules
Browse files Browse the repository at this point in the history
Support for assigning validation data to strong_password method
  • Loading branch information
lonnieezell authored May 8, 2020
2 parents 6a1c3a6 + ff2a433 commit a257846
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 10 deletions.
65 changes: 55 additions & 10 deletions src/Authentication/Passwords/ValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,38 @@ class ValidationRules
* better security if this is done manually, since you can
* personalize based on a specific user at that point.
*
* @param string $str
* @param string|null $error
* @param string $value Field value
* @param string $error1 Error that will be returned (for call without validation data array)
* @param array $data Validation data array
* @param string $error2 Error that will be returned (for call with validation data array)
*
* @return bool
*/
public function strong_password(string $str, string &$error = null)
public function strong_password(string $value, string &$error1 = null, array $data = [], string &$error2 = null)
{
$checker = service('passwords');
$user = (function_exists("user") && user()) ? user() : $this->buildUserFromRequest();

$result = $checker->check($str, $user);
if (function_exists('user') && user())
{
$user = user();
}
else
{
$user = empty($data) ? $this->buildUserFromRequest() : $this->buildUserFromData($data);
}

$result = $checker->check($value, $user);

if ($result === false)
{
$error = $checker->error();
if (empty($data))
{
$error1 = $checker->error();
}
else
{
$error2 = $checker->error();
}
}

return $result;
Expand All @@ -46,17 +63,45 @@ public function strong_password(string $str, string &$error = null)
/**
* Builds a new user instance from the global request.
*
* @return User
* @return \Myth\Auth\Entities\User
*/
protected function buildUserFromRequest()
{
$config = config('Auth');
$fields = array_merge($config->validFields, $config->personalFields);
$fields[] = 'password';
$fields = $this->prepareValidFields();

$data = service('request')->getPost($fields);

return new User($data);
}

/**
* Builds a new user instance from assigned data..
*
* @param array $data Assigned data
*
* @return \Myth\Auth\Entities\User
*/
protected function buildUserFromData(array $data = [])
{
$fields = $this->prepareValidFields();

$data = array_intersect_key($data, array_fill_keys($fields, null));

return new User($data);
}

/**
* Prepare valid user fields
*
* @return array
*/
protected function prepareValidFields(): array
{
$config = config('Auth');
$fields = array_merge($config->validFields, $config->personalFields);
$fields[] = 'password';

return $fields;
}

}
130 changes: 130 additions & 0 deletions tests/unit/ValidationRulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Validation\Validation;
use Config\Services;
use Myth\Auth\Authentication\Passwords\ValidationRules;


class ValidationRulesTest extends CIUnitTestCase
{
protected $validation;
protected $config = [
'ruleSets' => [
ValidationRules::class,
],
];

//--------------------------------------------------------------------

protected function setUp(): void
{
parent::setUp();

Services::reset(true);

$this->validation = new Validation((object) $this->config, \Config\Services::renderer());
$this->validation->reset();

$_REQUEST = [];
}

//--------------------------------------------------------------------

public function testStrongPasswordLongRule()
{
$rules = [
'password' => 'strong_password[]',
];

$data = [
'email' => 'john@smith.com',
'password' => '!!!gerard!!!abootylicious',
];

$this->validation->setRules($rules);

$this->assertTrue($this->validation->run($data));
}

//--------------------------------------------------------------------

public function testStrongPasswordLongRuleWithPostRequest()
{
$_REQUEST = $data = [
'email' => 'john@smith.com',
'password' => '!!!gerard!!!abootylicious',
];

$request = service('request');
$request->setMethod('post')->setGlobal('post', $data);

$this->validation->setRules([
'password' => 'strong_password[]',
]);

$result = $this->validation->withRequest($request)->run();
$this->assertTrue($result);
}

//--------------------------------------------------------------------

public function testStrongPasswordLongRuleWithRawInputRequest()
{
$data = [
'email' => 'john@smith.com',
'password' => '!!!gerard!!!abootylicious',
];

$request = service('request');
$request->setMethod('patch')->setBody(http_build_query($data));

$this->validation->setRules([
'password' => 'strong_password[]',
]);

$result = $this->validation->withRequest($request)->run();
$this->assertTrue($result);
}

//--------------------------------------------------------------------

public function testStrongPasswordShortRuleWithPostRequest()
{
$_REQUEST = $data = [
'email' => 'john@smith.com',
'password' => '!!!gerard!!!abootylicious',
];

$request = service('request');
$request->setMethod('post')->setGlobal('post', $data);

$this->validation->setRules([
'password' => 'strong_password',
]);

$result = $this->validation->withRequest($request)->run();
$this->assertTrue($result);
}

//--------------------------------------------------------------------

public function testStrongPasswordShortRuleWithRawInputRequest()
{
$data = [
'email' => 'john@smith.com',
'password' => '!!!gerard!!!abootylicious',
];

$request = service('request');
$request->setMethod('patch')->setBody(http_build_query($data));

$this->validation->setRules([
'password' => 'strong_password',
]);

$result = $this->validation->withRequest($request)->run();
$this->assertTrue($result);
}

}

0 comments on commit a257846

Please sign in to comment.