*/ namespace oat\generis\model\user; use common_ext_ExtensionException; use common_ext_ExtensionsManager; /** * Class PasswordConstraintsService used to verify password strength * @package generis */ class PasswordConstraintsService extends \tao_models_classes_Service { /** * @var array */ protected $validators = []; protected function __construct() { parent::__construct(); $config = $this->getConfig(); $this->register($config); } /** * Test if password pass all constraints rules * * @param $password * * @return bool */ public function validate($password) { $result = true; /** @var \tao_helpers_form_Validator $validator */ foreach ($this->validators as $validator) { $result &= $validator->evaluate($password); } return (bool) $result; } /** * Set up all validator according configuration file * * @param $config */ protected function register($config) { $this->validators = []; if (array_key_exists('length', $config) && (int) $config['length']) { $this->validators[] = new \tao_helpers_form_validators_Length([ 'min' => (int) $config['length'] ]); } if ( ( array_key_exists('upper', $config) && $config['upper'] ) || ( array_key_exists('lower', $config) && $config['lower'] ) ) { $this->validators[] = new \tao_helpers_form_validators_Regex( [ 'message' => __('Must include at least one letter'), 'format' => '/\pL/' ], 'letters' ); } if (( array_key_exists('upper', $config) && $config['upper'] )) { $this->validators[] = new \tao_helpers_form_validators_Regex( [ 'message' => __('Must include upper case letters'), 'format' => '/(\p{Lu}+)/', ], 'caseUpper' ); } if (( array_key_exists('lower', $config) && $config['lower'] )) { $this->validators[] = new \tao_helpers_form_validators_Regex( [ 'message' => __('Must include lower case letters'), 'format' => '/(\p{Ll}+)/' ], 'caseLower' ); } if (array_key_exists('number', $config) && $config['number']) { $this->validators[] = new \tao_helpers_form_validators_Regex( [ 'message' => __('Must include at least one number'), 'format' => '/\pN/' ], 'number' ); } if (array_key_exists('spec', $config) && $config['spec']) { $this->validators[] = new \tao_helpers_form_validators_Regex( [ 'message' => __('Must include at least one special letter'), 'format' => '/[^p{Ll}\p{Lu}\pL\pN]/' ], 'spec' ); } } /** * Any errors that was found during validation process * @return array */ public function getErrors() { $errors = []; /** @var \tao_helpers_form_Validator $validator */ foreach ($this->validators as $validator) { $errors[] = $validator->getMessage(); } return $errors; } /** * List of active validators * @return array */ public function getValidators() { return $this->validators; } /** * Retrieve at least default config ( if extension is not yet installed ) * @return array */ protected function getConfig() { if (\tao_install_utils_System::isTAOInstalled() && $this->getServiceLocator()->has(common_ext_ExtensionsManager::SERVICE_ID)) { $ext = $this->getServiceLocator() ->get(common_ext_ExtensionsManager::SERVICE_ID) ->getExtensionById('generis'); $config = $ext->getConfig('passwords'); } else { $config = require_once(__DIR__ . '/../../config/default/passwords.conf.php'); } return (array) $config['constrains']; } }