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