ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
AttributeMap.php
Go to the documentation of this file.
1 <?php
2 
3 
11 {
12 
16  private $map = array();
17 
21  private $duplicate = false;
22 
23 
32  public function __construct($config, $reserved)
33  {
34  parent::__construct($config, $reserved);
35 
36  assert(is_array($config));
37  $mapFiles = array();
38 
39  foreach ($config as $origName => $newName) {
40  if (is_int($origName)) {
41  if ($newName === '%duplicate') {
42  $this->duplicate = true;
43  } else {
44  // no index given, this is a map file
45  $mapFiles[] = $newName;
46  }
47  continue;
48  }
49 
50  if (!is_string($origName)) {
51  throw new Exception('Invalid attribute name: '.var_export($origName, true));
52  }
53 
54  if (!is_string($newName) && !is_array($newName)) {
55  throw new Exception('Invalid attribute name: '.var_export($newName, true));
56  }
57 
58  $this->map[$origName] = $newName;
59  }
60 
61  // load map files after we determine duplicate or rename
62  foreach ($mapFiles as &$file) {
63  $this->loadMapFile($file);
64  }
65  }
66 
67 
76  private function loadMapFile($fileName)
77  {
79 
80  $m = explode(':', $fileName);
81  if (count($m) === 2) { // we are asked for a file in a module
82  if (!SimpleSAML\Module::isModuleEnabled($m[0])) {
83  throw new Exception("Module '$m[0]' is not enabled.");
84  }
85  $filePath = SimpleSAML\Module::getModuleDir($m[0]).'/attributemap/'.$m[1].'.php';
86  } else {
87  $filePath = $config->getPathValue('attributenamemapdir', 'attributemap/').$fileName.'.php';
88  }
89 
90  if (!file_exists($filePath)) {
91  throw new Exception('Could not find attribute map file: '.$filePath);
92  }
93 
94  $attributemap = null;
95  include($filePath);
96  if (!is_array($attributemap)) {
97  throw new Exception('Attribute map file "'.$filePath.'" didn\'t define an attribute map.');
98  }
99 
100  if ($this->duplicate) {
101  $this->map = array_merge_recursive($this->map, $attributemap);
102  } else {
103  $this->map = array_merge($this->map, $attributemap);
104  }
105  }
106 
107 
113  public function process(&$request)
114  {
115  assert(is_array($request));
116  assert(array_key_exists('Attributes', $request));
117 
118  $mapped_attributes = array();
119 
120  foreach ($request['Attributes'] as $name => $values) {
121  if (array_key_exists($name, $this->map)) {
122  if (!is_array($this->map[$name])) {
123  if ($this->duplicate) {
124  $mapped_attributes[$name] = $values;
125  }
126  $mapped_attributes[$this->map[$name]] = $values;
127  } else {
128  foreach ($this->map[$name] as $to_map) {
129  $mapped_attributes[$to_map] = $values;
130  }
131  if ($this->duplicate && !in_array($name, $this->map[$name], true)) {
132  $mapped_attributes[$name] = $values;
133  }
134  }
135  } else {
136  $mapped_attributes[$name] = $values;
137  }
138  }
139 
140  $request['Attributes'] = $mapped_attributes;
141  }
142 }
$config
Definition: bootstrap.php:15
foreach($paths as $path) $request
Definition: asyncclient.php:32
static getModuleDir($module)
Retrieve the base directory for a module.
Definition: Module.php:39
$duplicate
Should attributes be duplicated or renamed.
process(&$request)
Apply filter to rename attributes.
Attribute-related utility methods.
$values
$map
Associative array with the mappings of attribute names.
$attributemap
Definition: addurnprefix.php:3
loadMapFile($fileName)
Loads and merges in a file with a attribute map.
__construct($config, $reserved)
Initialize this filter, parse configuration.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.