Password.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Customer\Attribute\Backend;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * @deprecated 101.0.0
  10. * Customer password attribute backend
  11. */
  12. class Password extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  13. {
  14. /**
  15. * Min password length
  16. */
  17. const MIN_PASSWORD_LENGTH = 6;
  18. /**
  19. * Magento string lib
  20. *
  21. * @var \Magento\Framework\Stdlib\StringUtils
  22. */
  23. protected $string;
  24. /**
  25. * @param \Magento\Framework\Stdlib\StringUtils $string
  26. */
  27. public function __construct(\Magento\Framework\Stdlib\StringUtils $string)
  28. {
  29. $this->string = $string;
  30. }
  31. /**
  32. * Special processing before attribute save:
  33. * a) check some rules for password
  34. * b) transform temporary attribute 'password' into real attribute 'password_hash'
  35. *
  36. * @param \Magento\Framework\DataObject $object
  37. * @return void
  38. * @throws \Magento\Framework\Exception\LocalizedException
  39. */
  40. public function beforeSave($object)
  41. {
  42. $password = $object->getPassword();
  43. $length = $this->string->strlen($password);
  44. if ($length > 0) {
  45. if ($length < self::MIN_PASSWORD_LENGTH) {
  46. throw new LocalizedException(
  47. __(
  48. 'The password needs at least %1 characters. Create a new password and try again.',
  49. self::MIN_PASSWORD_LENGTH
  50. )
  51. );
  52. }
  53. if (trim($password) !== $password) {
  54. throw new LocalizedException(
  55. __("The password can't begin or end with a space. Verify the password and try again.")
  56. );
  57. }
  58. $object->setPasswordHash($object->hashPassword($password));
  59. }
  60. }
  61. /**
  62. * @deprecated 101.0.0
  63. * @param \Magento\Framework\DataObject $object
  64. * @return bool
  65. */
  66. public function validate($object)
  67. {
  68. $password = $object->getPassword();
  69. if ($password && $password === $object->getPasswordConfirm()) {
  70. return true;
  71. }
  72. return parent::validate($object);
  73. }
  74. }