Skip to content

Commit

Permalink
Merge 4f1a494 into a69d41b
Browse files Browse the repository at this point in the history
  • Loading branch information
soltmar committed Oct 14, 2022
2 parents a69d41b + 4f1a494 commit 684b6dd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/Plexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Ballen\Collection\Collection;
use Ballen\Plexity\Interfaces\PasswordHistoryInterface;
use Ballen\Plexity\Support\Validator;
use InvalidArgumentException;

/**
* Plexity
Expand All @@ -22,17 +23,18 @@
class Plexity
{

const RULE_UPPER = 'upper';
const RULE_LOWER = 'lower';
const RULE_SPECIAL = 'special';
const RULE_NUMERIC = 'numeric';
const RULE_LENGTH_MIN = 'length_min';
const RULE_LENGTH_MAX = 'length_max';
const RULE_NOT_IN = 'not_in';
public const RULE_UPPER = 'upper';
public const RULE_LOWER = 'lower';
public const RULE_SPECIAL = 'special';
public const RULE_NUMERIC = 'numeric';
public const RULE_LENGTH_MIN = 'length_min';
public const RULE_LENGTH_MAX = 'length_max';
public const RULE_NOT_IN = 'not_in';
public const RULE_NOT_HAVING = 'not_having';

/**
* The configured list of rules for the object.
* @var \Ballen\Collection\Collection
* @var Collection
*/
private $rules;

Expand All @@ -54,6 +56,7 @@ class Plexity
self::RULE_LENGTH_MIN => 0,
self::RULE_LENGTH_MAX => 0,
self::RULE_NOT_IN => null,
self::RULE_NOT_HAVING => null,
];

/**
Expand All @@ -69,7 +72,7 @@ public function __construct()
{
$this->rules = new Collection($this->defaultConfiguration);

$this->validator = new Validator;
$this->validator = new Validator();
}

/**
Expand Down Expand Up @@ -134,7 +137,7 @@ public function requireNumericCharacters($amount = 1)
public function minimumLength($minLength)
{
if (!is_int($minLength)) {
throw new \InvalidArgumentException('The minimum length value must be of type integer.');
throw new InvalidArgumentException('The minimum length value must be of type integer.');
}
$this->rules->put(self::RULE_LENGTH_MIN, $minLength);
return $this;
Expand All @@ -148,7 +151,7 @@ public function minimumLength($minLength)
public function maximumLength($maxLength)
{
if (!is_int($maxLength)) {
throw new \InvalidArgumentException('The maximum length value must be of type integer.');
throw new InvalidArgumentException('The maximum length value must be of type integer.');
}
$this->rules->put(self::RULE_LENGTH_MAX, $maxLength);
return $this;
Expand Down Expand Up @@ -178,6 +181,17 @@ public function notIn($history)
return $this;
}

/**
* Requires that the password/string doesn't contain any of the provided strings. Check is case-insensitive.
* @param array<string> An array of strings to check against.
* @return Plexity
*/
public function notHaving($stack)
{
$this->rules->put(self::RULE_NOT_HAVING, $stack);
return $this;
}

/**
* Check the string against the list of configured rules to ensure that it is valid.
* @param string $string The password/string to validate.
Expand Down
33 changes: 33 additions & 0 deletions lib/Support/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function validate(Plexity $configuration)
$this->checkNumericCharacters();
$this->checkSpecialCharacters();
$this->checkNotIn();
$this->checkNotHaving();
return true;
}

Expand Down Expand Up @@ -220,6 +221,24 @@ public function checkNotIn()

}

/**
* Validates if a string does not contain any words from the provided array.
* @return void
* @throws ValidationException
*/
public function checkNotHaving()
{

if ($this->configuration->rules()->get(Plexity::RULE_NOT_HAVING) === null) {
return;
}

if (is_array($this->configuration->rules()->get(Plexity::RULE_NOT_HAVING)) && count($this->configuration->rules()->get(Plexity::RULE_NOT_HAVING)) > 0) {
$this->validateNotHaving();
}

}

/**
* Validates the upper case requirements.
* @return boolean
Expand Down Expand Up @@ -339,4 +358,18 @@ private function countOccurrences(array $needles, $haystack)
}
return $count;
}

/**
* Validates the not_having requirements against a simple array.
* @return void
* @throws ValidationException
*/
private function validateNotHaving()
{
foreach((array)$this->configuration->rules()->get(Plexity::RULE_NOT_HAVING) as $needle){
if ( stripos($this->configuration->checkString() , $needle) !== false) {
throw new ValidationException('The string contains word from the list of disallowed values requirements.');
}
}
}
}
18 changes: 18 additions & 0 deletions tests/PlexityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,22 @@ public function testPasswordDoesNotExistInPasswordHistoryStore()
$password->notIn($passwordHistoryStore);
$this->assertTrue($password->check('Bingo!'));
}

public function testPasswordNotHavingWords()
{
$password = new Plexity();
$password->notHaving(['sol', 'red', 'joe', 'Pot']);
$this->assertTrue($password->check('S0l7y5'));
}

public function testPasswordNotHavingWordsFail()
{
$password = new Plexity();
$password->notHaving(['sol', 'red', 'joe', 'Pot']);
$this->expectException(
'Ballen\Plexity\Exceptions\ValidationException',
'The string contains word from the list of disallowed values requirements.'
);
$password->check('%tRedRig7');
}
}

0 comments on commit 684b6dd

Please sign in to comment.