|
| 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\AdobeIms\Block\Adminhtml; |
| 9 | + |
| 10 | +use Magento\AdobeImsApi\Api\ConfigProviderInterface; |
| 11 | +use Magento\AdobeImsApi\Api\ConfigInterface; |
| 12 | +use Magento\AdobeImsApi\Api\UserAuthorizedInterface; |
| 13 | +use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface; |
| 14 | +use Magento\Authorization\Model\UserContextInterface; |
| 15 | +use Magento\Backend\Block\Template; |
| 16 | +use Magento\Backend\Block\Template\Context; |
| 17 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 18 | +use Magento\Framework\Serialize\Serializer\JsonHexTag; |
| 19 | + |
| 20 | +/** |
| 21 | + * Provides required data for the Adobe service authentication component |
| 22 | + * |
| 23 | + * @api |
| 24 | + */ |
| 25 | +class SignIn extends Template |
| 26 | +{ |
| 27 | + private const DATA_ARGUMENT_KEY_CONFIG_PROVIDERS = 'configProviders'; |
| 28 | + private const RESPONSE_REGEXP_PATTERN = 'auth\\[code=(success|error);message=(.+)\\]'; |
| 29 | + private const RESPONSE_CODE_INDEX = 1; |
| 30 | + private const RESPONSE_MESSAGE_INDEX = 2; |
| 31 | + private const RESPONSE_SUCCESS_CODE = 'success'; |
| 32 | + private const RESPONSE_ERROR_CODE = 'error'; |
| 33 | + private const ADOBE_IMS_JS_SIGNIN = 'Magento_AdobeIms/js/signIn'; |
| 34 | + private const ADOBE_IMS_SIGNIN = 'Magento_AdobeIms/signIn'; |
| 35 | + private const ADOBE_IMS_USER_PROFILE = 'adobe_ims/user/profile'; |
| 36 | + private const ADOBE_IMS_USER_LOGOUT = 'adobe_ims/user/logout'; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ConfigInterface |
| 40 | + */ |
| 41 | + private $config; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var UserContextInterface |
| 45 | + */ |
| 46 | + private $userContext; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var UserAuthorizedInterface |
| 50 | + */ |
| 51 | + private $userAuthorized; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var UserProfileRepositoryInterface |
| 55 | + */ |
| 56 | + private $userProfileRepository; |
| 57 | + |
| 58 | + /** |
| 59 | + * JsonHexTag Serializer Instance |
| 60 | + * |
| 61 | + * @var JsonHexTag |
| 62 | + */ |
| 63 | + private $serializer; |
| 64 | + |
| 65 | + /** |
| 66 | + * SignIn constructor. |
| 67 | + * |
| 68 | + * @param Context $context |
| 69 | + * @param ConfigInterface $config |
| 70 | + * @param UserContextInterface $userContext |
| 71 | + * @param UserAuthorizedInterface $userAuthorized |
| 72 | + * @param UserProfileRepositoryInterface $userProfileRepository |
| 73 | + * @param JsonHexTag $json |
| 74 | + * @param array $data |
| 75 | + */ |
| 76 | + public function __construct( |
| 77 | + Context $context, |
| 78 | + ConfigInterface $config, |
| 79 | + UserContextInterface $userContext, |
| 80 | + UserAuthorizedInterface $userAuthorized, |
| 81 | + UserProfileRepositoryInterface $userProfileRepository, |
| 82 | + JsonHexTag $json, |
| 83 | + array $data = [] |
| 84 | + ) { |
| 85 | + $this->config = $config; |
| 86 | + $this->userContext = $userContext; |
| 87 | + $this->userAuthorized = $userAuthorized; |
| 88 | + $this->userProfileRepository = $userProfileRepository; |
| 89 | + $this->serializer = $json; |
| 90 | + parent::__construct($context, $data); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get configuration for UI component |
| 95 | + * |
| 96 | + * @return string |
| 97 | + */ |
| 98 | + public function getComponentJsonConfig(): string |
| 99 | + { |
| 100 | + return $this->serializer->serialize( |
| 101 | + array_replace_recursive( |
| 102 | + $this->getDefaultComponentConfig(), |
| 103 | + ...$this->getExtendedComponentConfig() |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get default UI component configuration |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + private function getDefaultComponentConfig(): array |
| 114 | + { |
| 115 | + return [ |
| 116 | + 'component' => self::ADOBE_IMS_JS_SIGNIN, |
| 117 | + 'template' => self::ADOBE_IMS_SIGNIN, |
| 118 | + 'profileUrl' => $this->getUrl(self::ADOBE_IMS_USER_PROFILE), |
| 119 | + 'logoutUrl' => $this->getUrl(self::ADOBE_IMS_USER_LOGOUT), |
| 120 | + 'user' => $this->getUserData(), |
| 121 | + 'loginConfig' => [ |
| 122 | + 'url' => $this->config->getAuthUrl(), |
| 123 | + 'callbackParsingParams' => [ |
| 124 | + 'regexpPattern' => self::RESPONSE_REGEXP_PATTERN, |
| 125 | + 'codeIndex' => self::RESPONSE_CODE_INDEX, |
| 126 | + 'messageIndex' => self::RESPONSE_MESSAGE_INDEX, |
| 127 | + 'successCode' => self::RESPONSE_SUCCESS_CODE, |
| 128 | + 'errorCode' => self::RESPONSE_ERROR_CODE |
| 129 | + ] |
| 130 | + ] |
| 131 | + ]; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get UI component configuration extension specified in layout configuration for block instance |
| 136 | + * |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + private function getExtendedComponentConfig(): array |
| 140 | + { |
| 141 | + $configProviders = $this->getData(self::DATA_ARGUMENT_KEY_CONFIG_PROVIDERS); |
| 142 | + if (empty($configProviders)) { |
| 143 | + return []; |
| 144 | + } |
| 145 | + |
| 146 | + $configExtensions = []; |
| 147 | + foreach ($configProviders as $configProvider) { |
| 148 | + if ($configProvider instanceof ConfigProviderInterface) { |
| 149 | + $configExtensions[] = $configProvider->get(); |
| 150 | + } |
| 151 | + } |
| 152 | + return $configExtensions; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get user profile information |
| 157 | + * |
| 158 | + * @return array |
| 159 | + */ |
| 160 | + private function getUserData(): array |
| 161 | + { |
| 162 | + if (!$this->userAuthorized->execute()) { |
| 163 | + return $this->getDefaultUserData(); |
| 164 | + } |
| 165 | + |
| 166 | + try { |
| 167 | + $userProfile = $this->userProfileRepository->getByUserId((int)$this->userContext->getUserId()); |
| 168 | + } catch (NoSuchEntityException $exception) { |
| 169 | + return $this->getDefaultUserData(); |
| 170 | + } |
| 171 | + |
| 172 | + return [ |
| 173 | + 'isAuthorized' => true, |
| 174 | + 'name' => $userProfile->getName(), |
| 175 | + 'email' => $userProfile->getEmail(), |
| 176 | + 'image' => $userProfile->getImage(), |
| 177 | + ]; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get default user data for not authenticated or missing user profile |
| 182 | + * |
| 183 | + * @return array |
| 184 | + */ |
| 185 | + private function getDefaultUserData(): array |
| 186 | + { |
| 187 | + return [ |
| 188 | + 'isAuthorized' => false, |
| 189 | + 'name' => '', |
| 190 | + 'email' => '', |
| 191 | + 'image' => '', |
| 192 | + ]; |
| 193 | + } |
| 194 | +} |
0 commit comments