use dictionary validator

This commit is contained in:
Damodar Lohani
2022-12-26 10:22:49 +00:00
parent 5649bc8060
commit 0760ea90d6
3 changed files with 89 additions and 52 deletions
@@ -0,0 +1,78 @@
<?php
namespace Appwrite\Auth\Validator;
use Utopia\Database\Document;
/**
* Password.
*
* Validates user password string
*/
class PasswordDictionary extends Password
{
protected array $dictionary;
protected Document $project;
public function __construct(array $dictionary, Document $project)
{
$this->dictionary = $dictionary;
$this->project = $project;
}
/**
* Get Description.
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
return 'Password must be at least 8 characters and should not be one of the commonly used password.';
}
/**
* Is valid.
*
* @param mixed $value
*
* @return bool
*/
public function isValid($value): bool
{
if(!parent::isValid($value)) {
return false;
}
$dictionaryEnabled = $this->project->getAttribute('auths', [])['passwordDictionary'] ?? false;
if ($dictionaryEnabled && array_key_exists($value, $this->dictionary)) {
return false;
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_STRING;
}
}