Skip to content

Commit e41eb19

Browse files
committed
fix: remove password config from generis
1 parent 0d31206 commit e41eb19

File tree

3 files changed

+16
-151
lines changed

3 files changed

+16
-151
lines changed

config/default/passwords.conf.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
*/
66

77
return [
8-
'constrains' => //constrains configuration params
9-
[
10-
'length' => 4,
11-
'upper' => false,
12-
'lower' => true,
13-
'number' => false,
14-
'spec' => false
15-
],
168
'generator' => [
179
'chars' => 'abcdefghijklmnopqrstuvwxyz',
1810
'nums' => '0123456789',
@@ -22,5 +14,4 @@
2214
//used for human readable generator
2315
'dictionary' => '/usr/share/dict/words'
2416
]
25-
2617
];

config/header/passwords.conf.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/**
4+
* This file has been refactored to remove cyclic dependency
5+
*
6+
* Password constraints are now under ./config/tao/passwordConstraints.conf.php
7+
* Usage of `constrains` key is deprecated and will trigger an error in the future
8+
*/

core/user/PasswordConstraintsService.php

Lines changed: 8 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -15,158 +15,24 @@
1515
* along with this program; if not, write to the Free Software
1616
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717
*
18-
* Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
18+
* Copyright (c) 2015-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
1919
* @author Mikhail Kamarouski, <[email protected]>
2020
*/
2121

22+
declare(strict_types=1);
23+
2224
namespace oat\generis\model\user;
2325

24-
use common_ext_ExtensionException;
25-
use common_ext_ExtensionsManager;
26+
use oat\oatbox\service\ServiceManager;
2627

2728
/**
28-
* Class PasswordConstraintsService used to verify password strength
29+
* @deprecated Please use `oat\tao\model\password\PasswordConstraintsService::SERVICE_ID` instead
2930
* @package generis
3031
*/
31-
class PasswordConstraintsService extends \tao_models_classes_Service
32+
class PasswordConstraintsService
3233
{
33-
/**
34-
* @var array
35-
*/
36-
protected $validators = [];
37-
38-
protected function __construct()
39-
{
40-
parent::__construct();
41-
$config = $this->getConfig();
42-
$this->register($config);
43-
}
44-
45-
46-
/**
47-
* Test if password pass all constraints rules
48-
*
49-
* @param $password
50-
*
51-
* @return bool
52-
*/
53-
public function validate($password)
54-
{
55-
$result = true;
56-
/** @var \tao_helpers_form_Validator $validator */
57-
foreach ($this->validators as $validator) {
58-
$result &= $validator->evaluate($password);
59-
}
60-
61-
return (bool) $result;
62-
}
63-
64-
/**
65-
* Set up all validator according configuration file
66-
*
67-
* @param $config
68-
*/
69-
protected function register($config)
34+
static public function singleton()
7035
{
71-
$this->validators = [];
72-
73-
if (array_key_exists('length', $config) && (int) $config['length']) {
74-
$this->validators[] = new \tao_helpers_form_validators_Length([ 'min' => (int) $config['length'] ]);
75-
}
76-
77-
if (
78-
( array_key_exists('upper', $config) && $config['upper'] )
79-
|| ( array_key_exists('lower', $config) && $config['lower'] )
80-
) {
81-
$this->validators[] = new \tao_helpers_form_validators_Regex(
82-
[
83-
'message' => __('Must include at least one letter'),
84-
'format' => '/\pL/'
85-
],
86-
'letters'
87-
);
88-
}
89-
90-
if (( array_key_exists('upper', $config) && $config['upper'] )) {
91-
$this->validators[] = new \tao_helpers_form_validators_Regex(
92-
[
93-
'message' => __('Must include upper case letters'),
94-
'format' => '/(\p{Lu}+)/',
95-
],
96-
'caseUpper'
97-
);
98-
}
99-
100-
if (( array_key_exists('lower', $config) && $config['lower'] )) {
101-
$this->validators[] = new \tao_helpers_form_validators_Regex(
102-
[
103-
'message' => __('Must include lower case letters'),
104-
'format' => '/(\p{Ll}+)/'
105-
],
106-
'caseLower'
107-
);
108-
}
109-
110-
if (array_key_exists('number', $config) && $config['number']) {
111-
$this->validators[] = new \tao_helpers_form_validators_Regex(
112-
[
113-
'message' => __('Must include at least one number'),
114-
'format' => '/\pN/'
115-
],
116-
'number'
117-
);
118-
}
119-
120-
if (array_key_exists('spec', $config) && $config['spec']) {
121-
$this->validators[] = new \tao_helpers_form_validators_Regex(
122-
[
123-
'message' => __('Must include at least one special letter'),
124-
'format' => '/[^p{Ll}\p{Lu}\pL\pN]/'
125-
],
126-
'spec'
127-
);
128-
}
129-
}
130-
131-
/**
132-
* Any errors that was found during validation process
133-
* @return array
134-
*/
135-
public function getErrors()
136-
{
137-
$errors = [];
138-
/** @var \tao_helpers_form_Validator $validator */
139-
foreach ($this->validators as $validator) {
140-
$errors[] = $validator->getMessage();
141-
}
142-
143-
return $errors;
144-
}
145-
146-
/**
147-
* List of active validators
148-
* @return array
149-
*/
150-
public function getValidators()
151-
{
152-
return $this->validators;
153-
}
154-
155-
/**
156-
* Retrieve at least default config ( if extension is not yet installed )
157-
* @return array
158-
*/
159-
protected function getConfig()
160-
{
161-
if (\tao_install_utils_System::isTAOInstalled() && $this->getServiceLocator()->has(common_ext_ExtensionsManager::SERVICE_ID)) {
162-
$ext = $this->getServiceLocator()
163-
->get(common_ext_ExtensionsManager::SERVICE_ID)
164-
->getExtensionById('generis');
165-
$config = $ext->getConfig('passwords');
166-
} else {
167-
$config = require_once(__DIR__ . '/../../config/default/passwords.conf.php');
168-
}
169-
170-
return (array) $config['constrains'];
36+
return ServiceManager::getServiceManager()->get(\oat\tao\model\password\PasswordConstraintsService::SERVICE_ID);
17137
}
17238
}

0 commit comments

Comments
 (0)