Skip to content
Snippets Groups Projects
Commit 31cf4312 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Fix several issues with the core:AttributeValueMap filter.

parent 929edf0d
No related branches found
No related tags found
No related merge requests found
...@@ -12,32 +12,32 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter ...@@ -12,32 +12,32 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter
{ {
/** /**
* The attributename we should assign values to (ie target) * The name of the attribute we should assign values to (ie: the target attribute).
*/ */
private $targetattribute; private $targetattribute;
/** /**
* The attributename we should create values from * The name of the attribute we should create values from.
*/ */
private $sourceattribute; private $sourceattribute;
/** /**
* The required $sourceattribute values and target affiliations * The required $sourceattribute values and target affiliations.
*/ */
private $values = array(); private $values = array();
/** /**
* Whether $sourceattribute should be kept * Whether $sourceattribute should be kept or not.
*/ */
private $keep = false; private $keep = false;
/** /**
* Whether $target attribute values should be replaced by new values * Whether $target attribute values should be replaced by new values or not.
*/ */
private $replace = false; private $replace = false;
/** /**
* Initialize this filter. * Initialize the filter.
* *
* @param array $config Configuration information about this filter. * @param array $config Configuration information about this filter.
* @param mixed $reserved For future use. * @param mixed $reserved For future use.
...@@ -49,35 +49,49 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter ...@@ -49,35 +49,49 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter
assert('is_array($config)'); assert('is_array($config)');
// validate configuration // parse configuration
foreach ($config as $name => $value) { foreach ($config as $name => $value) {
if (is_int($name)) { if (is_int($name)) {
// check if this is an option // check if this is an option
if ($value === '%replace') { if ($value === '%replace') {
$this->replace = true; $this->replace = true;
} elseif ($value === '%keep') { } elseif ($value === '%keep') {
$this->keep = true; $this->keep = true;
} else { } else {
throw new \SimpleSAML_Error_Exception('Unknown flag : ' . var_export($value, true)); // unknown configuration option, log it and ignore the error
\SimpleSAML_Logger::warning(
"AttributeValueMap: unknown configuration flag '".var_export($value, true)."'"
);
} }
continue; continue;
} }
// set targetattribute // set the target attribute
if ($name === 'targetattribute') { if ($name === 'targetattribute') {
$this->targetattribute = $value; $this->targetattribute = $value;
} }
// set sourceattribute // set the source attribute
if ($name === 'sourceattribute') { if ($name === 'sourceattribute') {
$this->sourceattribute = $value; $this->sourceattribute = $value;
} }
// set values // set the values
if ($name === 'values') { if ($name === 'values') {
$this->values = $value; $this->values = $value;
} }
} }
// now validate it
if (!is_string($this->sourceattribute)) {
throw new \SimpleSAML_Error_Exception("AttributeValueMap: 'sourceattribute' configuration option not set.");
}
if (!is_string($this->targetattribute)) {
throw new \SimpleSAML_Error_Exception("AttributeValueMap: 'targetattribute' configuration option not set.");
}
if (!is_array($this->values)) {
throw new \SimpleSAML_Error_Exception("AttributeValueMap: 'values' configuration option is not an array.");
}
} }
...@@ -88,31 +102,34 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter ...@@ -88,31 +102,34 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter
*/ */
public function process(&$request) public function process(&$request)
{ {
\SimpleSAML_Logger::debug('AttributeValueMap - process'); \SimpleSAML_Logger::debug('Processing the AttributeValueMap filter.');
assert('is_array($request)'); assert('is_array($request)');
assert('array_key_exists("Attributes", $request)'); assert('array_key_exists("Attributes", $request)');
$attributes =& $request['Attributes']; $attributes =& $request['Attributes'];
// Make sure sourceattribute exists if (!array_key_exists($this->sourceattribute, $attributes)) {
assert('array_key_exists($this->sourceattribute, $attributes)'); // the source attribute does not exist, nothing to do here
// Make sure the targetattribute is set return;
assert('is_string($this->targetattribute)'); }
$sourceattribute = $attributes[$this->sourceattribute]; $sourceattribute = $attributes[$this->sourceattribute];
$targetvalues = array(); $targetvalues = array();
if (is_array($sourceattribute)) { if (is_array($sourceattribute)) {
foreach ($this->values as $value => $require) { foreach ($this->values as $value => $values) {
if (count(array_intersect($require, $sourceattribute)) > 0) { if (!is_array($values)) {
\SimpleSAML_Logger::debug('AttributeValueMap - intersect match for ' . $value); $values = array($values);
}
if (count(array_intersect($values, $sourceattribute)) > 0) {
\SimpleSAML_Logger::debug("AttributeValueMap: intersect match for '$value'");
$targetvalues[] = $value; $targetvalues[] = $value;
} }
} }
} }
if (count($targetvalues) > 0) { if (count($targetvalues) > 0) {
if ($this->replace or !@is_array($attributes[$this->targetattribute])) { if ($this->replace || !array_key_exists($this->targetattribute, $attributes)) {
$attributes[$this->targetattribute] = $targetvalues; $attributes[$this->targetattribute] = $targetvalues;
} else { } else {
$attributes[$this->targetattribute] = array_unique(array_merge( $attributes[$this->targetattribute] = array_unique(array_merge(
...@@ -123,6 +140,7 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter ...@@ -123,6 +140,7 @@ class AttributeValueMap extends \SimpleSAML_Auth_ProcessingFilter
} }
if (!$this->keep) { if (!$this->keep) {
// no need to keep the source attribute
unset($attributes[$this->sourceattribute]); unset($attributes[$this->sourceattribute]);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment