Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/FieldType/User/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\User;
8
9 use DateTimeImmutable;
10 use DateTimeInterface;
11 use eZ\Publish\Core\FieldType\FieldType;
12 use eZ\Publish\Core\FieldType\ValidationError;
13 use eZ\Publish\SPI\FieldType\Value as SPIValue;
14 use eZ\Publish\SPI\Persistence\Content\FieldValue;
15 use eZ\Publish\Core\FieldType\Value as BaseValue;
16 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
17 use eZ\Publish\Core\Persistence\Cache\UserHandler;
18 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
19
25 class Type extends FieldType
26 {
27 public const PASSWORD_TTL_SETTING = 'PasswordTTL';
28 public const PASSWORD_TTL_WARNING_SETTING = 'PasswordTTLWarning';
29
31 protected $userHandler;
32
34 protected $settingsSchema = [
35 self::PASSWORD_TTL_SETTING => [
36 'type' => 'int',
37 'default' => null,
38 ],
39 self::PASSWORD_TTL_WARNING_SETTING => [
40 'type' => 'int',
41 'default' => null,
42 ],
43 ];
44
46 protected $validatorConfigurationSchema = [
47 'PasswordValueValidator' => [
48 'requireAtLeastOneUpperCaseCharacter' => [
49 'type' => 'int',
50 'default' => 1,
51 ],
52 'requireAtLeastOneLowerCaseCharacter' => [
53 'type' => 'int',
54 'default' => 1,
55 ],
56 'requireAtLeastOneNumericCharacter' => [
57 'type' => 'int',
58 'default' => 1,
59 ],
60 'requireAtLeastOneNonAlphanumericCharacter' => [
61 'type' => 'int',
62 'default' => null,
63 ],
64 'requireNewPassword' => [
65 'type' => 'int',
66 'default' => null,
67 ],
68 'minLength' => [
69 'type' => 'int',
70 'default' => 10,
71 ],
72 ],
73 ];
74
78 public function __construct(UserHandler $userHandler)
79 {
80 $this->userHandler = $userHandler;
81 }
82
88 public function getFieldTypeIdentifier()
89 {
90 return 'ezuser';
91 }
92
103 public function getName(SPIValue $value)
104 {
105 return (string)$value->login;
106 }
107
113 public function isSingular()
114 {
115 return true;
116 }
117
123 public function onlyEmptyInstance()
124 {
125 return true;
126 }
127
134 public function getEmptyValue()
135 {
136 return new Value();
137 }
138
146 protected function createValueFromInput($inputValue)
147 {
148 if (is_array($inputValue)) {
149 $inputValue = $this->fromHash($inputValue);
150 }
151
152 return $inputValue;
153 }
154
162 protected function checkValueStructure(BaseValue $value)
163 {
164 // Does nothing
165 }
166
170 protected function getSortInfo(BaseValue $value)
171 {
172 return false;
173 }
174
182 public function fromHash($hash)
183 {
184 if ($hash === null) {
185 return $this->getEmptyValue();
186 }
187
188 if (isset($hash['passwordUpdatedAt']) && $hash['passwordUpdatedAt'] !== null) {
189 $hash['passwordUpdatedAt'] = new DateTimeImmutable('@' . $hash['passwordUpdatedAt']);
190 }
191
192 return new Value($hash);
193 }
194
202 public function toHash(SPIValue $value)
203 {
204 if ($this->isEmptyValue($value)) {
205 return null;
206 }
207
208 $hash = (array)$value;
209 if ($hash['passwordUpdatedAt'] instanceof DateTimeInterface) {
210 $hash['passwordUpdatedAt'] = $hash['passwordUpdatedAt']->getTimestamp();
211 }
212
213 return $hash;
214 }
215
238 public function toPersistenceValue(SPIValue $value)
239 {
240 return new FieldValue(
241 [
242 'data' => null,
243 'externalData' => $this->toHash($value),
244 'sortKey' => null,
245 ]
246 );
247 }
248
258 public function fromPersistenceValue(FieldValue $fieldValue)
259 {
260 return $this->acceptValue($fieldValue->externalData);
261 }
262
273 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
274 {
275 $errors = [];
276
277 if ($this->isEmptyValue($fieldValue)) {
278 return $errors;
279 }
280
281 if (!$fieldValue->hasStoredLogin) {
282 try {
283 $login = $fieldValue->login;
284 $this->userHandler->loadByLogin($login);
285
286 // If you want to change this ValidationError message, please remember to change it also in Repository Forms in lib/Validator/Constraints/FieldValueValidatorMessages class
287 $errors[] = new ValidationError(
288 "The user login '%login%' is used by another user. You must enter a unique login.",
289 null,
290 [
291 '%login%' => $login,
292 ],
293 'username'
294 );
295 } catch (NotFoundException $e) {
296 // Do nothing
297 }
298 }
299
300 return $errors;
301 }
302
306 public function validateValidatorConfiguration($validatorConfiguration)
307 {
308 $validationErrors = [];
309
310 foreach ((array)$validatorConfiguration as $validatorIdentifier => $constraints) {
311 if ($validatorIdentifier !== 'PasswordValueValidator') {
312 $validationErrors[] = new ValidationError(
313 "Validator '%validator%' is unknown",
314 null,
315 [
316 'validator' => $validatorIdentifier,
317 ],
318 "[$validatorIdentifier]"
319 );
320 }
321 }
322
323 return $validationErrors;
324 }
325
329 public function validateFieldSettings($fieldSettings)
330 {
331 $validationErrors = [];
332
333 foreach ($fieldSettings as $name => $value) {
334 if (!isset($this->settingsSchema[$name])) {
335 $validationErrors[] = new ValidationError(
336 "Setting '%setting%' is unknown",
337 null,
338 [
339 '%setting%' => $name,
340 ],
341 "[$name]"
342 );
343
344 continue;
345 }
346
347 $error = null;
348 switch ($name) {
349 case self::PASSWORD_TTL_SETTING:
350 $error = $this->validatePasswordTTLSetting($name, $value);
351 break;
352 case self::PASSWORD_TTL_WARNING_SETTING:
353 $error = $this->validatePasswordTTLWarningSetting($name, $value, $fieldSettings);
354 break;
355 }
356
357 if ($error !== null) {
358 $validationErrors[] = $error;
359 }
360 }
361
362 return $validationErrors;
363 }
364
365 private function validatePasswordTTLSetting(string $name, $value): ?ValidationError
366 {
367 if ($value !== null && !is_int($value)) {
368 return new ValidationError(
369 "Setting '%setting%' value must be of integer type",
370 null,
371 [
372 '%setting%' => $name,
373 ],
374 "[$name]"
375 );
376 }
377
378 return null;
379 }
380
381 private function validatePasswordTTLWarningSetting(string $name, $value, $fieldSettings): ?ValidationError
382 {
383 if ($value !== null) {
384 if (!is_int($value)) {
385 return new ValidationError(
386 "Setting '%setting%' value must be of integer type",
387 null,
388 [
389 '%setting%' => $name,
390 ],
391 "[$name]"
392 );
393 }
394
395 if ($value > 0) {
396 $passwordTTL = $fieldSettings[self::PASSWORD_TTL_SETTING] ?? null;
397 if ($value >= (int)$passwordTTL) {
398 return new ValidationError(
399 'Password expiration warning value should be lower then password expiration value',
400 null,
401 [],
402 "[$name]"
403 );
404 }
405 }
406 }
407
408 return null;
409 }
410 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\Core\FieldType\FieldType
Base class for field types, the most basic storage unit of data inside eZ Publish.
Definition: Core/FieldType/FieldType.php:35
eZ\Publish\Core\FieldType\FieldType\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/FieldType.php:350
eZ\Publish\Core\FieldType\FieldType\acceptValue
acceptValue($inputValue)
Potentially builds and checks the type and structure of the $inputValue.
Definition: Core/FieldType/FieldType.php:376
eZ\Publish\Core\FieldType\User\Type
The User field type.
Definition: Core/FieldType/User/Type.php:26
eZ\Publish\Core\FieldType\User\Type\toPersistenceValue
toPersistenceValue(SPIValue $value)
Converts a $value to a persistence value.
Definition: Core/FieldType/User/Type.php:238
eZ\Publish\Core\FieldType\User\Type\PASSWORD_TTL_WARNING_SETTING
const PASSWORD_TTL_WARNING_SETTING
Definition: Core/FieldType/User/Type.php:28
eZ\Publish\Core\FieldType\User\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/User/Type.php:134
eZ\Publish\Core\FieldType\User\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
{Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct....
Definition: Core/FieldType/User/Type.php:329
eZ\Publish\Core\FieldType\User\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/User/Type.php:182
eZ\Publish\Core\FieldType\User\Type\$userHandler
$userHandler
Definition: Core/FieldType/User/Type.php:31
eZ\Publish\Core\FieldType\User\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/User/Type.php:103
eZ\Publish\Core\FieldType\User\Type\__construct
__construct(UserHandler $userHandler)
Definition: Core/FieldType/User/Type.php:78
eZ\Publish\Core\FieldType\User\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/User/Type.php:34
eZ\Publish\Core\FieldType\User\Type\fromPersistenceValue
fromPersistenceValue(FieldValue $fieldValue)
Converts a persistence $fieldValue to a Value.
Definition: Core/FieldType/User/Type.php:258
eZ\Publish\Core\FieldType\User\Type\validatePasswordTTLWarningSetting
validatePasswordTTLWarningSetting(string $name, $value, $fieldSettings)
Definition: Core/FieldType/User/Type.php:381
eZ\Publish\Core\FieldType\User\Type\$validatorConfigurationSchema
$validatorConfigurationSchema
Definition: Core/FieldType/User/Type.php:46
eZ\Publish\Core\FieldType\User\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/User/Type.php:88
eZ\Publish\Core\FieldType\User\Type\validatePasswordTTLSetting
validatePasswordTTLSetting(string $name, $value)
Definition: Core/FieldType/User/Type.php:365
eZ\Publish\Core\FieldType\User\Type\isSingular
isSingular()
Indicates if the field definition of this type can appear only once in the same ContentType.
Definition: Core/FieldType/User/Type.php:113
eZ\Publish\Core\FieldType\User\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/User/Type.php:146
eZ\Publish\Core\FieldType\User\Type\onlyEmptyInstance
onlyEmptyInstance()
Indicates if the field definition of this type can be added to a ContentType with Content instances.
Definition: Core/FieldType/User/Type.php:123
eZ\Publish\Core\FieldType\User\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/User/Type.php:162
eZ\Publish\Core\FieldType\User\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/User/Type.php:202
eZ\Publish\Core\FieldType\User\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/User/Type.php:273
eZ\Publish\Core\FieldType\User\Type\PASSWORD_TTL_SETTING
const PASSWORD_TTL_SETTING
Definition: Core/FieldType/User/Type.php:27
eZ\Publish\Core\FieldType\User\Type\getSortInfo
getSortInfo(BaseValue $value)
{}
Definition: Core/FieldType/User/Type.php:170
eZ\Publish\Core\FieldType\User\Type\validateValidatorConfiguration
validateValidatorConfiguration($validatorConfiguration)
{Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct...
Definition: Core/FieldType/User/Type.php:306
eZ\Publish\Core\FieldType\User\Value
Value for User field type.
Definition: Core/FieldType/User/Value.php:15
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\FieldType\Value
Abstract class for all field value classes.
Definition: Core/FieldType/Value.php:17
eZ\Publish\Core\Persistence\Cache\UserHandler
Cache handler for user module.
Definition: UserHandler.php:22
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\User
Definition: Core/FieldType/User/Type.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.