|
15 | 15 | * along with this program; if not, write to the Free Software
|
16 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
17 | 17 | *
|
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); |
19 | 19 | * @author Mikhail Kamarouski, <[email protected]>
|
20 | 20 | */
|
21 | 21 |
|
| 22 | +declare(strict_types=1); |
| 23 | + |
22 | 24 | namespace oat\generis\model\user;
|
23 | 25 |
|
24 |
| -use common_ext_ExtensionException; |
25 |
| -use common_ext_ExtensionsManager; |
| 26 | +use oat\oatbox\service\ServiceManager; |
26 | 27 |
|
27 | 28 | /**
|
28 |
| - * Class PasswordConstraintsService used to verify password strength |
| 29 | + * @deprecated Please use `oat\tao\model\password\PasswordConstraintsService::SERVICE_ID` instead |
29 | 30 | * @package generis
|
30 | 31 | */
|
31 |
| -class PasswordConstraintsService extends \tao_models_classes_Service |
| 32 | +class PasswordConstraintsService |
32 | 33 | {
|
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() |
70 | 35 | {
|
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); |
171 | 37 | }
|
172 | 38 | }
|
0 commit comments