Skip to content

Commit

Permalink
fix: remove password config from generis
Browse files Browse the repository at this point in the history
  • Loading branch information
siwane committed Jan 27, 2022
1 parent 0d31206 commit e41eb19
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 151 deletions.
9 changes: 0 additions & 9 deletions config/default/passwords.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
*/

return [
'constrains' => //constrains configuration params
[
'length' => 4,
'upper' => false,
'lower' => true,
'number' => false,
'spec' => false
],
'generator' => [
'chars' => 'abcdefghijklmnopqrstuvwxyz',
'nums' => '0123456789',
Expand All @@ -22,5 +14,4 @@
//used for human readable generator
'dictionary' => '/usr/share/dict/words'
]

];
8 changes: 8 additions & 0 deletions config/header/passwords.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* This file has been refactored to remove cyclic dependency
*
* Password constraints are now under ./config/tao/passwordConstraints.conf.php
* Usage of `constrains` key is deprecated and will trigger an error in the future
*/
150 changes: 8 additions & 142 deletions core/user/PasswordConstraintsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,158 +15,24 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2015-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* @author Mikhail Kamarouski, <[email protected]>
*/

declare(strict_types=1);

namespace oat\generis\model\user;

use common_ext_ExtensionException;
use common_ext_ExtensionsManager;
use oat\oatbox\service\ServiceManager;

/**
* Class PasswordConstraintsService used to verify password strength
* @deprecated Please use `oat\tao\model\password\PasswordConstraintsService::SERVICE_ID` instead
* @package generis
*/
class PasswordConstraintsService extends \tao_models_classes_Service
class PasswordConstraintsService
{
/**
* @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)
static public function singleton()
{
$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'];
return ServiceManager::getServiceManager()->get(\oat\tao\model\password\PasswordConstraintsService::SERVICE_ID);
}
}

0 comments on commit e41eb19

Please sign in to comment.