|
6 | 6 |
|
7 | 7 | namespace Magento\Customer\Model;
|
8 | 8 |
|
| 9 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 10 | +use Magento\Customer\Api\CustomerMetadataInterface; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
9 | 12 | use Magento\Customer\Api\Data\CustomerInterface;
|
| 13 | +use Magento\Customer\Api\Data\ValidationResultsInterfaceFactory; |
| 14 | +use Magento\Customer\Helper\View as CustomerViewHelper; |
| 15 | +use Magento\Customer\Model\Config\Share as ConfigShare; |
| 16 | +use Magento\Customer\Model\Customer as CustomerModel; |
| 17 | +use Magento\Customer\Model\Metadata\Validator; |
| 18 | +use Magento\Framework\Api\ExtensibleDataObjectConverter; |
| 19 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 20 | +use Magento\Framework\AuthorizationInterface; |
| 21 | +use Magento\Framework\DataObjectFactory as ObjectFactory; |
| 22 | +use Magento\Framework\Encryption\EncryptorInterface as Encryptor; |
| 23 | +use Magento\Framework\Event\ManagerInterface; |
| 24 | +use Magento\Framework\Exception\AuthorizationException; |
| 25 | +use Magento\Framework\Mail\Template\TransportBuilder; |
| 26 | +use Magento\Framework\Math\Random; |
| 27 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 28 | +use Magento\Framework\Registry; |
| 29 | +use Magento\Framework\Stdlib\DateTime; |
| 30 | +use Magento\Framework\Stdlib\StringUtils as StringHelper; |
| 31 | +use Magento\Store\Model\StoreManagerInterface; |
| 32 | +use Psr\Log\LoggerInterface as PsrLogger; |
10 | 33 |
|
11 | 34 | /**
|
12 | 35 | * Account Management service implementation for external API access.
|
| 36 | + * |
13 | 37 | * Handle various customer account actions.
|
14 | 38 | *
|
15 | 39 | * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
|
| 40 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
16 | 41 | */
|
17 | 42 | class AccountManagementApi extends AccountManagement
|
18 | 43 | {
|
| 44 | + /** |
| 45 | + * @var AuthorizationInterface |
| 46 | + */ |
| 47 | + private $authorization; |
| 48 | + |
| 49 | + /** |
| 50 | + * @param CustomerFactory $customerFactory |
| 51 | + * @param ManagerInterface $eventManager |
| 52 | + * @param StoreManagerInterface $storeManager |
| 53 | + * @param Random $mathRandom |
| 54 | + * @param Validator $validator |
| 55 | + * @param ValidationResultsInterfaceFactory $validationResultsDataFactory |
| 56 | + * @param AddressRepositoryInterface $addressRepository |
| 57 | + * @param CustomerMetadataInterface $customerMetadataService |
| 58 | + * @param CustomerRegistry $customerRegistry |
| 59 | + * @param PsrLogger $logger |
| 60 | + * @param Encryptor $encryptor |
| 61 | + * @param ConfigShare $configShare |
| 62 | + * @param StringHelper $stringHelper |
| 63 | + * @param CustomerRepositoryInterface $customerRepository |
| 64 | + * @param ScopeConfigInterface $scopeConfig |
| 65 | + * @param TransportBuilder $transportBuilder |
| 66 | + * @param DataObjectProcessor $dataProcessor |
| 67 | + * @param Registry $registry |
| 68 | + * @param CustomerViewHelper $customerViewHelper |
| 69 | + * @param DateTime $dateTime |
| 70 | + * @param CustomerModel $customerModel |
| 71 | + * @param ObjectFactory $objectFactory |
| 72 | + * @param ExtensibleDataObjectConverter $extensibleDataObjectConverter |
| 73 | + * @param AuthorizationInterface $authorization |
| 74 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 75 | + */ |
| 76 | + public function __construct( |
| 77 | + CustomerFactory $customerFactory, |
| 78 | + ManagerInterface $eventManager, |
| 79 | + StoreManagerInterface $storeManager, |
| 80 | + Random $mathRandom, |
| 81 | + Validator $validator, |
| 82 | + ValidationResultsInterfaceFactory $validationResultsDataFactory, |
| 83 | + AddressRepositoryInterface $addressRepository, |
| 84 | + CustomerMetadataInterface $customerMetadataService, |
| 85 | + CustomerRegistry $customerRegistry, |
| 86 | + PsrLogger $logger, |
| 87 | + Encryptor $encryptor, |
| 88 | + ConfigShare $configShare, |
| 89 | + StringHelper $stringHelper, |
| 90 | + CustomerRepositoryInterface $customerRepository, |
| 91 | + ScopeConfigInterface $scopeConfig, |
| 92 | + TransportBuilder $transportBuilder, |
| 93 | + DataObjectProcessor $dataProcessor, |
| 94 | + Registry $registry, |
| 95 | + CustomerViewHelper $customerViewHelper, |
| 96 | + DateTime $dateTime, |
| 97 | + CustomerModel $customerModel, |
| 98 | + ObjectFactory $objectFactory, |
| 99 | + ExtensibleDataObjectConverter $extensibleDataObjectConverter, |
| 100 | + AuthorizationInterface $authorization |
| 101 | + ) { |
| 102 | + $this->authorization = $authorization; |
| 103 | + parent::__construct( |
| 104 | + $customerFactory, |
| 105 | + $eventManager, |
| 106 | + $storeManager, |
| 107 | + $mathRandom, |
| 108 | + $validator, |
| 109 | + $validationResultsDataFactory, |
| 110 | + $addressRepository, |
| 111 | + $customerMetadataService, |
| 112 | + $customerRegistry, |
| 113 | + $logger, |
| 114 | + $encryptor, |
| 115 | + $configShare, |
| 116 | + $stringHelper, |
| 117 | + $customerRepository, |
| 118 | + $scopeConfig, |
| 119 | + $transportBuilder, |
| 120 | + $dataProcessor, |
| 121 | + $registry, |
| 122 | + $customerViewHelper, |
| 123 | + $dateTime, |
| 124 | + $customerModel, |
| 125 | + $objectFactory, |
| 126 | + $extensibleDataObjectConverter |
| 127 | + ); |
| 128 | + } |
| 129 | + |
19 | 130 | /**
|
20 | 131 | * @inheritDoc
|
21 | 132 | *
|
22 | 133 | * Override createAccount method to unset confirmation attribute for security purposes.
|
23 | 134 | */
|
24 | 135 | public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
|
25 | 136 | {
|
| 137 | + $this->validateCustomerRequest($customer); |
26 | 138 | $customer = parent::createAccount($customer, $password, $redirectUrl);
|
27 | 139 | $customer->setConfirmation(null);
|
28 | 140 |
|
29 | 141 | return $customer;
|
30 | 142 | }
|
| 143 | + |
| 144 | + /** |
| 145 | + * Validate anonymous request |
| 146 | + * |
| 147 | + * @param CustomerInterface $customer |
| 148 | + * @return void |
| 149 | + * @throws AuthorizationException |
| 150 | + */ |
| 151 | + private function validateCustomerRequest(CustomerInterface $customer): void |
| 152 | + { |
| 153 | + $groupId = $customer->getGroupId(); |
| 154 | + if (isset($groupId) && |
| 155 | + !$this->authorization->isAllowed(self::ADMIN_RESOURCE) |
| 156 | + ) { |
| 157 | + $params = ['resources' => self::ADMIN_RESOURCE]; |
| 158 | + throw new AuthorizationException( |
| 159 | + __("The consumer isn't authorized to access %resources.", $params) |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
31 | 163 | }
|
0 commit comments