diff --git a/src/Authorization/AuthorizationDataMuxer.php b/src/Authorization/AuthorizationDataMuxer.php index 475b79c63662b9c1836835cb434cf5acb2251f7b..49705938ccd5275313f954aacb71482524243b9d 100644 --- a/src/Authorization/AuthorizationDataMuxer.php +++ b/src/Authorization/AuthorizationDataMuxer.php @@ -23,15 +23,29 @@ class AuthorizationDataMuxer private function loadUserAttributesFromAuthorizationProvider(?string $userIdentifier, AuthorizationDataProviderInterface $authorizationDataProvider): void { - $userAttributes = []; + $userAttributes = $authorizationDataProvider->getUserAttributes($userIdentifier); - if ($userIdentifier !== null) { - $userAttributes = $authorizationDataProvider->getUserAttributes($userIdentifier); + foreach ($authorizationDataProvider->getAvailableAttributes() as $availableAttribute) { + if (array_key_exists($availableAttribute, $userAttributes)) { + $this->customAttributes[$availableAttribute] = $userAttributes[$availableAttribute]; + } } + } - foreach ($authorizationDataProvider->getAvailableAttributes() as $availableAttribute) { - $this->customAttributes[$availableAttribute] = $userAttributes[$availableAttribute] ?? null; + /** + * Returns an array of available attributes. + * + * @return string[] + */ + public function getAvailableAttributes(): array + { + $res = []; + foreach ($this->authorizationDataProviders as $authorizationDataProvider) { + $availableAttributes = $authorizationDataProvider->getAvailableAttributes(); + $res = array_merge($res, $availableAttributes); } + + return $res; } /** diff --git a/src/Authorization/DebugCommand.php b/src/Authorization/DebugCommand.php index ddbc71258ddf719cce3db9e78a9e994e8d02f17b..13c0794efdf062ffa820e025ed8ffba6008d602e 100644 --- a/src/Authorization/DebugCommand.php +++ b/src/Authorization/DebugCommand.php @@ -37,32 +37,26 @@ class DebugCommand extends Command implements LoggerAwareInterface protected function execute(InputInterface $input, OutputInterface $output): int { $username = $input->getArgument('username'); - if ($username === null) { - // No username, list all providers - $output->writeln("<fg=blue;options=bold>Attributes available for all provider</>\n"); - $providers = $this->provider->getAuthorizationDataProviders(); - foreach ($providers as $provider) { - $output->writeln('<fg=green;options=bold>['.get_class($provider).']</>'); - foreach ($provider->getAvailableAttributes() as $attr) { - $output->writeln($attr); - } - $output->writeln(''); - } - // TODO: list all attributes - } else { - // get all available attributes for a user per provider - $output->writeln('<fg=blue;options=bold>Attributes for user "'.$username."\":</>\n"); - $providers = $this->provider->getAuthorizationDataProviders(); - foreach ($providers as $provider) { - $output->writeln('<fg=green;options=bold>['.get_class($provider).']</>'); - foreach ($provider->getUserAttributes($username) as $attr => $value) { - $output->writeln($attr.'='.json_encode($value)); - } - $output->writeln(''); - } + // Fetch all attributes first (to get potential log spam first) + $providers = $this->provider->getAuthorizationDataProviders(); + $mux = new AuthorizationDataMuxer($providers); + $attrs = $mux->getAvailableAttributes(); + $all = []; + $default = new \stdClass(); + sort($attrs, SORT_STRING | SORT_FLAG_CASE); + foreach ($attrs as $attr) { + $all[$attr] = $mux->getCustomAttribute($username, $attr, $default); + } - // TODO: list all attributes + // Now print them out + $output->writeln('<fg=blue;options=bold>[Authorization attributes]</>'); + foreach ($all as $attr => $value) { + if ($value === $default) { + $output->writeln('<fg=green;options=bold>'.$attr.'</> = <fg=magenta;options=bold>\<N/A\></>'); + } else { + $output->writeln('<fg=green;options=bold>'.$attr.'</> = '.json_encode($value)); + } } return 0;