|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\AdminAdobeIms\Plugin\Block\Adminhtml; |
| 10 | + |
| 11 | +use Magento\AdminAdobeIms\Model\Auth; |
| 12 | +use Magento\AdobeIms\Block\Adminhtml\SignIn; |
| 13 | +use Magento\AdminAdobeIms\Service\ImsConfig; |
| 14 | +use Magento\AdobeImsApi\Api\ConfigInterface; |
| 15 | +use Magento\AdobeImsApi\Api\ConfigProviderInterface; |
| 16 | +use Magento\AdobeImsApi\Api\UserAuthorizedInterface; |
| 17 | +use Magento\Framework\Serialize\Serializer\JsonHexTag; |
| 18 | + |
| 19 | +/** |
| 20 | + * Plugin to get authentication component configuration if Admin Adobe IMS is enabled |
| 21 | + */ |
| 22 | +class SignInPlugin |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ImsConfig |
| 26 | + */ |
| 27 | + private ImsConfig $adminAdobeImsConfig; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Auth |
| 31 | + */ |
| 32 | + private Auth $auth; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var UserAuthorizedInterface |
| 36 | + */ |
| 37 | + private UserAuthorizedInterface $userAuthorized; |
| 38 | + |
| 39 | + /** |
| 40 | + * JsonHexTag Serializer Instance |
| 41 | + * |
| 42 | + * @var JsonHexTag |
| 43 | + */ |
| 44 | + private JsonHexTag $serializer; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ConfigInterface |
| 48 | + */ |
| 49 | + private ConfigInterface $config; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param ImsConfig $adminAdobeImsConfig |
| 53 | + * @param Auth $auth |
| 54 | + * @param UserAuthorizedInterface $userAuthorized |
| 55 | + * @param JsonHexTag $serializer |
| 56 | + * @param ConfigInterface $config |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + ImsConfig $adminAdobeImsConfig, |
| 60 | + Auth $auth, |
| 61 | + UserAuthorizedInterface $userAuthorized, |
| 62 | + JsonHexTag $serializer, |
| 63 | + ConfigInterface $config |
| 64 | + ) { |
| 65 | + $this->adminAdobeImsConfig = $adminAdobeImsConfig; |
| 66 | + $this->auth = $auth; |
| 67 | + $this->userAuthorized = $userAuthorized; |
| 68 | + $this->serializer = $serializer; |
| 69 | + $this->config = $config; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get authentication component configuration if Admin Adobe IMS is enabled |
| 74 | + * |
| 75 | + * @param SignIn $subject |
| 76 | + * @param callable $proceed |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + public function aroundGetComponentJsonConfig(SignIn $subject, callable $proceed): string |
| 80 | + { |
| 81 | + if (!$this->adminAdobeImsConfig->enabled()) { |
| 82 | + return $proceed(); |
| 83 | + } |
| 84 | + |
| 85 | + return $this->serializer->serialize( |
| 86 | + array_replace_recursive( |
| 87 | + $this->getDefaultComponentConfig($subject), |
| 88 | + ...$this->getExtendedComponentConfig($subject) |
| 89 | + ) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get default UI component configuration |
| 95 | + * |
| 96 | + * @param SignIn $subject |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + private function getDefaultComponentConfig(SignIn $subject): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'component' => SignIn::ADOBE_IMS_JS_SIGNIN, |
| 103 | + 'template' => SignIn::ADOBE_IMS_SIGNIN, |
| 104 | + 'profileUrl' => $subject->getUrl(SignIn::ADOBE_IMS_USER_PROFILE), |
| 105 | + 'logoutUrl' => $subject->getUrl(SignIn::ADOBE_IMS_USER_LOGOUT), |
| 106 | + 'user' => $this->getUserData(), |
| 107 | + 'isGlobalSignInEnabled' => true, |
| 108 | + 'loginConfig' => [ |
| 109 | + 'url' => $this->config->getAuthUrl(), |
| 110 | + 'callbackParsingParams' => [ |
| 111 | + 'regexpPattern' => SignIn::RESPONSE_REGEXP_PATTERN, |
| 112 | + 'codeIndex' => SignIn::RESPONSE_CODE_INDEX, |
| 113 | + 'messageIndex' => SignIn::RESPONSE_MESSAGE_INDEX, |
| 114 | + 'successCode' => SignIn::RESPONSE_SUCCESS_CODE, |
| 115 | + 'errorCode' => SignIn::RESPONSE_ERROR_CODE |
| 116 | + ] |
| 117 | + ] |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get UI component configuration extension specified in layout configuration for block instance |
| 123 | + * |
| 124 | + * @param SignIn $subject |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + private function getExtendedComponentConfig(SignIn $subject): array |
| 128 | + { |
| 129 | + $configProviders = $subject->getData(SignIn::DATA_ARGUMENT_KEY_CONFIG_PROVIDERS); |
| 130 | + if (empty($configProviders)) { |
| 131 | + return []; |
| 132 | + } |
| 133 | + |
| 134 | + $configExtensions = []; |
| 135 | + foreach ($configProviders as $configProvider) { |
| 136 | + if ($configProvider instanceof ConfigProviderInterface) { |
| 137 | + $configExtensions[] = $configProvider->get(); |
| 138 | + } |
| 139 | + } |
| 140 | + return $configExtensions; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Get user profile information |
| 145 | + * |
| 146 | + * @return array |
| 147 | + */ |
| 148 | + private function getUserData(): array |
| 149 | + { |
| 150 | + if (!$this->userAuthorized->execute()) { |
| 151 | + return $this->getDefaultUserData(); |
| 152 | + } |
| 153 | + |
| 154 | + $user = $this->auth->getUser(); |
| 155 | + |
| 156 | + return [ |
| 157 | + 'isAuthorized' => true, |
| 158 | + 'name' => $user->getFirstName() . ' ' . $user->getLastName(), |
| 159 | + 'email' => $user->getEmail(), |
| 160 | + 'image' => '' |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get default user data for not authenticated or missing user profile |
| 166 | + * |
| 167 | + * @return array |
| 168 | + */ |
| 169 | + private function getDefaultUserData(): array |
| 170 | + { |
| 171 | + return [ |
| 172 | + 'isAuthorized' => false, |
| 173 | + 'name' => '', |
| 174 | + 'email' => '', |
| 175 | + 'image' => '', |
| 176 | + ]; |
| 177 | + } |
| 178 | +} |
0 commit comments