|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CustomerGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Model\AuthenticationInterface; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 15 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 16 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 17 | +use Magento\Framework\GraphQl\Query\Resolver\Value; |
| 18 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 19 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 20 | +use Magento\Framework\Validator\EmailAddress as EmailValidator; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class Resolver for ResetPassword |
| 24 | + */ |
| 25 | +class ResetPassword implements ResolverInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var AccountManagementInterface |
| 29 | + */ |
| 30 | + private $customerAccountManagement; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var EmailValidator |
| 34 | + */ |
| 35 | + private $emailValidator; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var AuthenticationInterface |
| 39 | + */ |
| 40 | + private $authentication; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var CustomerRepositoryInterface |
| 44 | + */ |
| 45 | + private $customerRepository; |
| 46 | + |
| 47 | + /** |
| 48 | + * ResetPassword constructor. |
| 49 | + * |
| 50 | + * @param AuthenticationInterface $authentication |
| 51 | + * @param CustomerRepositoryInterface $customerRepository |
| 52 | + * @param AccountManagementInterface $customerAccountManagement |
| 53 | + * @param EmailValidator $emailValidator |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + AuthenticationInterface $authentication, |
| 57 | + CustomerRepositoryInterface $customerRepository, |
| 58 | + AccountManagementInterface $customerAccountManagement, |
| 59 | + EmailValidator $emailValidator |
| 60 | + ) { |
| 61 | + $this->authentication = $authentication; |
| 62 | + $this->customerRepository = $customerRepository; |
| 63 | + $this->customerAccountManagement = $customerAccountManagement; |
| 64 | + $this->emailValidator = $emailValidator; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Reset old password and set new |
| 69 | + * |
| 70 | + * @param Field $field |
| 71 | + * @param ContextInterface $context |
| 72 | + * @param ResolveInfo $info |
| 73 | + * @param array|null $value |
| 74 | + * @param array|null $args |
| 75 | + * |
| 76 | + * @return bool|Value|mixed |
| 77 | + * |
| 78 | + * @throws GraphQlInputException |
| 79 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 80 | + */ |
| 81 | + public function resolve( |
| 82 | + Field $field, |
| 83 | + $context, |
| 84 | + ResolveInfo $info, |
| 85 | + array $value = null, |
| 86 | + array $args = null |
| 87 | + ) { |
| 88 | + if (empty($args['email'])) { |
| 89 | + throw new GraphQlInputException(__('You must specify an email address.')); |
| 90 | + } |
| 91 | + |
| 92 | + if (!$this->emailValidator->isValid($args['email'])) { |
| 93 | + throw new GraphQlInputException(__('The email address has an invalid format.')); |
| 94 | + } |
| 95 | + |
| 96 | + if (empty($args['resetPasswordToken'])) { |
| 97 | + throw new GraphQlInputException(__('resetPasswordToken must be specified')); |
| 98 | + } |
| 99 | + |
| 100 | + if (empty($args['newPassword'])) { |
| 101 | + throw new GraphQlInputException(__('newPassword must be specified')); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + $customer = $this->customerRepository->get($args['email']); |
| 106 | + } catch (LocalizedException $e) { |
| 107 | + throw new GraphQlInputException(__('Cannot set the customer\'s password'), $e); |
| 108 | + } |
| 109 | + |
| 110 | + if (true === $this->authentication->isLocked($customer->getId())) { |
| 111 | + throw new GraphQlInputException(__('The account is locked')); |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + return $this->customerAccountManagement->resetPassword( |
| 116 | + $args['email'], |
| 117 | + $args['resetPasswordToken'], |
| 118 | + $args['newPassword'] |
| 119 | + ); |
| 120 | + } catch (LocalizedException $e) { |
| 121 | + throw new GraphQlInputException(__('Cannot set the customer\'s password'), $e); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments