ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilSamlMappedUserAttributeValueParser.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
9 const ATTR_REGEX = '/^(.*?)(\|(\d+))?$/';
10
12 protected $rule;
14 protected $userData = [];
15
22 {
23 $this->rule = $rule;
24 $this->userData = $userData;
25 }
26
30 protected function getValueIndex() : int
31 {
32 $index = 0;
33
34 $matches = [];
35 preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
36
37 if (is_array($matches) && isset($matches[3]) && is_numeric($matches[3])) {
38 $index = (int) $matches[3];
39 }
40
41 return $index >= 0 ? $index : 0;
42 }
43
47 public function getAttributeKey() : string
48 {
49 $attribute = '';
50
51 $matches = [];
52 preg_match(self::ATTR_REGEX, $this->rule->getExternalAttribute(), $matches);
53
54 if (is_array($matches) && isset($matches[1]) && is_string($matches[1])) {
55 $attribute = $matches[1];
56 }
57
58 return $attribute;
59 }
60
65 public function parse() : string
66 {
67 $attributeKey = $this->getAttributeKey();
68
69 if (!array_key_exists($attributeKey, $this->userData)) {
70 throw new \ilSamlException(sprintf(
71 "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
72 $this->rule->getAttribute(),
73 $this->rule->getExternalAttribute()
74 ));
75 }
76
77 $value = $this->userData[$attributeKey];
78
79 if (is_array($value)) {
80 $valueIndex = $this->getValueIndex();
81
82 if (!array_key_exists($valueIndex, $value)) {
83 throw new \ilSamlException(sprintf(
84 "Configured external attribute of mapping '%s' -> '%s' does not exist in SAML attribute data.",
85 $this->rule->getAttribute(),
86 $this->rule->getExternalAttribute()
87 ));
88 }
89
90 $value = $value[$valueIndex];
91 }
92
93 if (!is_scalar($value)) {
94 throw new \ilSamlException(sprintf(
95 "Could not parse a scalar value based on the user attribute mapping '%s' -> '%s'.",
96 $this->rule->getAttribute(),
97 $this->rule->getExternalAttribute()
98 ));
99 }
100
101 return (string) $value;
102 }
103}
An exception for terminatinating execution or to throw for unit testing.
__construct(\ilExternalAuthUserAttributeMappingRule $rule, array $userData)
ilSamlMappedUserAttributeValueParser constructor.
$index
Definition: metadata.php:128