|
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\App\ObjectManager; |
| 21 | +use Magento\Framework\AuthorizationInterface; |
| 22 | +use Magento\Framework\DataObjectFactory as ObjectFactory; |
| 23 | +use Magento\Framework\Encryption\EncryptorInterface as Encryptor; |
| 24 | +use Magento\Framework\Event\ManagerInterface; |
| 25 | +use Magento\Framework\Exception\AuthorizationException; |
| 26 | +use Magento\Framework\Mail\Template\TransportBuilder; |
| 27 | +use Magento\Framework\Math\Random; |
| 28 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 29 | +use Magento\Framework\Registry; |
| 30 | +use Magento\Framework\Stdlib\DateTime; |
| 31 | +use Magento\Framework\Stdlib\StringUtils as StringHelper; |
| 32 | +use Magento\Store\Model\StoreManagerInterface; |
| 33 | +use Psr\Log\LoggerInterface as PsrLogger; |
10 | 34 |
|
11 | 35 | /**
|
12 | 36 | * Account Management service implementation for external API access.
|
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 | + * Authorization level of a basic admin session |
| 46 | + * |
| 47 | + * @see _isAllowed() |
| 48 | + */ |
| 49 | + public const ADMIN_RESOURCE = 'Magento_Customer::manage'; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var PsrLogger |
| 53 | + */ |
| 54 | + protected $logger; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var StringHelper |
| 58 | + */ |
| 59 | + protected $stringHelper; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var DataObjectProcessor |
| 63 | + */ |
| 64 | + protected $dataProcessor; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var Registry |
| 68 | + */ |
| 69 | + protected $registry; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var CustomerViewHelper |
| 73 | + */ |
| 74 | + protected $customerViewHelper; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var DateTime |
| 78 | + */ |
| 79 | + protected $dateTime; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var ObjectFactory |
| 83 | + */ |
| 84 | + protected $objectFactory; |
| 85 | + |
| 86 | + /** |
| 87 | + * @var ExtensibleDataObjectConverter |
| 88 | + */ |
| 89 | + protected $extensibleDataObjectConverter; |
| 90 | + |
| 91 | + /** |
| 92 | + * @var CustomerModel |
| 93 | + */ |
| 94 | + protected $customerModel; |
| 95 | + |
| 96 | + /** |
| 97 | + * @var AuthenticationInterface |
| 98 | + */ |
| 99 | + protected $authentication; |
| 100 | + /** |
| 101 | + * @var AuthorizationInterface |
| 102 | + */ |
| 103 | + private $authorization; |
| 104 | + |
| 105 | + /** |
| 106 | + * @param CustomerFactory $customerFactory |
| 107 | + * @param ManagerInterface $eventManager |
| 108 | + * @param StoreManagerInterface $storeManager |
| 109 | + * @param Random $mathRandom |
| 110 | + * @param Validator $validator |
| 111 | + * @param ValidationResultsInterfaceFactory $validationResultsDataFactory |
| 112 | + * @param AddressRepositoryInterface $addressRepository |
| 113 | + * @param CustomerMetadataInterface $customerMetadataService |
| 114 | + * @param CustomerRegistry $customerRegistry |
| 115 | + * @param PsrLogger $logger |
| 116 | + * @param Encryptor $encryptor |
| 117 | + * @param ConfigShare $configShare |
| 118 | + * @param StringHelper $stringHelper |
| 119 | + * @param CustomerRepositoryInterface $customerRepository |
| 120 | + * @param ScopeConfigInterface $scopeConfig |
| 121 | + * @param TransportBuilder $transportBuilder |
| 122 | + * @param DataObjectProcessor $dataProcessor |
| 123 | + * @param Registry $registry |
| 124 | + * @param CustomerViewHelper $customerViewHelper |
| 125 | + * @param DateTime $dateTime |
| 126 | + * @param CustomerModel $customerModel |
| 127 | + * @param ObjectFactory $objectFactory |
| 128 | + * @param ExtensibleDataObjectConverter $extensibleDataObjectConverter |
| 129 | + * @param AuthorizationInterface|null $authorization |
| 130 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 131 | + */ |
| 132 | + public function __construct( |
| 133 | + CustomerFactory $customerFactory, |
| 134 | + ManagerInterface $eventManager, |
| 135 | + StoreManagerInterface $storeManager, |
| 136 | + Random $mathRandom, |
| 137 | + Validator $validator, |
| 138 | + ValidationResultsInterfaceFactory $validationResultsDataFactory, |
| 139 | + AddressRepositoryInterface $addressRepository, |
| 140 | + CustomerMetadataInterface $customerMetadataService, |
| 141 | + CustomerRegistry $customerRegistry, |
| 142 | + PsrLogger $logger, |
| 143 | + Encryptor $encryptor, |
| 144 | + ConfigShare $configShare, |
| 145 | + StringHelper $stringHelper, |
| 146 | + CustomerRepositoryInterface $customerRepository, |
| 147 | + ScopeConfigInterface $scopeConfig, |
| 148 | + TransportBuilder $transportBuilder, |
| 149 | + DataObjectProcessor $dataProcessor, |
| 150 | + Registry $registry, |
| 151 | + CustomerViewHelper $customerViewHelper, |
| 152 | + DateTime $dateTime, |
| 153 | + CustomerModel $customerModel, |
| 154 | + ObjectFactory $objectFactory, |
| 155 | + ExtensibleDataObjectConverter $extensibleDataObjectConverter, |
| 156 | + AuthorizationInterface $authorization = null |
| 157 | + ) { |
| 158 | + $objectManager = ObjectManager::getInstance(); |
| 159 | + $this->authorization = $authorization ?? $objectManager->get(AuthorizationInterface::class); |
| 160 | + parent::__construct( |
| 161 | + $customerFactory, |
| 162 | + $eventManager, |
| 163 | + $storeManager, |
| 164 | + $mathRandom, |
| 165 | + $validator, |
| 166 | + $validationResultsDataFactory, |
| 167 | + $addressRepository, |
| 168 | + $customerMetadataService, |
| 169 | + $customerRegistry, |
| 170 | + $logger, |
| 171 | + $encryptor, |
| 172 | + $configShare, |
| 173 | + $stringHelper, |
| 174 | + $customerRepository, |
| 175 | + $scopeConfig, |
| 176 | + $transportBuilder, |
| 177 | + $dataProcessor, |
| 178 | + $registry, |
| 179 | + $customerViewHelper, |
| 180 | + $dateTime, |
| 181 | + $customerModel, |
| 182 | + $objectFactory, |
| 183 | + $extensibleDataObjectConverter |
| 184 | + ); |
| 185 | + } |
| 186 | + |
19 | 187 | /**
|
20 | 188 | * @inheritDoc
|
21 | 189 | *
|
22 | 190 | * Override createAccount method to unset confirmation attribute for security purposes.
|
23 | 191 | */
|
24 | 192 | public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
|
25 | 193 | {
|
| 194 | + $this->validateCustomerRequest($customer); |
26 | 195 | $customer = parent::createAccount($customer, $password, $redirectUrl);
|
27 | 196 | $customer->setConfirmation(null);
|
28 | 197 |
|
29 | 198 | return $customer;
|
30 | 199 | }
|
| 200 | + |
| 201 | + /** |
| 202 | + * Validate anonymous request |
| 203 | + * |
| 204 | + * @param CustomerInterface $customer |
| 205 | + * @return void |
| 206 | + * @throws AuthorizationException |
| 207 | + */ |
| 208 | + private function validateCustomerRequest(CustomerInterface $customer): void |
| 209 | + { |
| 210 | + $groupId = $customer->getGroupId(); |
| 211 | + if (isset($groupId) && |
| 212 | + !$this->authorization->isAllowed(self::ADMIN_RESOURCE) |
| 213 | + ) { |
| 214 | + $params = ['resources' => self::ADMIN_RESOURCE]; |
| 215 | + throw new AuthorizationException( |
| 216 | + __("The consumer isn't authorized to access %resources.", $params) |
| 217 | + ); |
| 218 | + } |
| 219 | + } |
31 | 220 | }
|
0 commit comments