ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSamlMappedUserAttributeValueParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  private const ATTR_REGEX = '/^(.*?)(\|(\d+))?$/';
28 
32  public function __construct(
33  private readonly ilExternalAuthUserAttributeMappingRule $rule,
34  private readonly array $userData
35  ) {
36  }
37 
38  private function getValueIndex(): int
39  {
40  $index = 0;
41 
42  $matches = [];
43  preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
44 
45  if (is_array($matches) && isset($matches[3]) && is_numeric($matches[3])) {
46  $index = (int) $matches[3];
47  }
48 
49  return max($index, 0);
50  }
51 
52  public function getAttributeKey(): string
53  {
54  $attribute = '';
55 
56  $matches = [];
57  preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
58 
59  if (is_array($matches) && isset($matches[1]) && is_string($matches[1])) {
60  $attribute = $matches[1];
61  }
62 
63  return $attribute;
64  }
65 
66  public function parse(): string
67  {
68  $attributeKey = $this->getAttributeKey();
69 
70  if (!array_key_exists($attributeKey, $this->userData)) {
71  throw new ilSamlException(sprintf(
72  "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
73  $this->rule->getAttribute(),
74  $this->rule->getExternalAttribute()
75  ));
76  }
77 
78  $value = $this->userData[$attributeKey];
79 
80  if (is_array($value)) {
81  $valueIndex = $this->getValueIndex();
82 
83  if (!array_key_exists($valueIndex, $value)) {
84  throw new ilSamlException(sprintf(
85  "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
86  $this->rule->getAttribute(),
87  $this->rule->getExternalAttribute()
88  ));
89  }
90 
91  $value = $value[$valueIndex];
92  }
93 
94  if (!is_scalar($value)) {
95  throw new ilSamlException(sprintf(
96  "Could not parse a scalar value based on the user attribute mapping '%s' -> '%s'.",
97  $this->rule->getAttribute(),
98  $this->rule->getExternalAttribute()
99  ));
100  }
101 
102  return (string) $value;
103  }
104 }
Class ilSamlException.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(private readonly ilExternalAuthUserAttributeMappingRule $rule, private readonly array $userData)