From 6f5059233cf41e86685c5108277df391aec25dde Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 20 Aug 2008 06:58:23 +0000
Subject: [PATCH] Add unique user id to module attribute processing state.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@820 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Auth/ProcessingChain.php | 45 +++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/lib/SimpleSAML/Auth/ProcessingChain.php b/lib/SimpleSAML/Auth/ProcessingChain.php
index 4ba54ba4d..a3b361529 100644
--- a/lib/SimpleSAML/Auth/ProcessingChain.php
+++ b/lib/SimpleSAML/Auth/ProcessingChain.php
@@ -163,6 +163,11 @@ class SimpleSAML_Auth_ProcessingChain {
 
 		$state[self::FILTERS_INDEX] = $this->filters;
 
+		if (!array_key_exists('UserID', $state)) {
+			/* No unique user ID present. Attempt to add one. */
+			self::addUserID($state);
+		}
+
 		while (count($state[self::FILTERS_INDEX]) > 0) {
 			$filter = array_shift($state[self::FILTERS_INDEX]);
 			$filter->process($state);
@@ -213,6 +218,46 @@ class SimpleSAML_Auth_ProcessingChain {
 		return SimpleSAML_Auth_State::loadState($id, self::COMPLETED_STAGE);
 	}
 
+
+	/**
+	 * Add unique user ID.
+	 *
+	 * This function attempts to add an unique user ID to the state.
+	 *
+	 * @param array &$state  The state we should update.
+	 */
+	private static function addUserID(&$state) {
+		assert('is_array($state)');
+		assert('array_key_exists("Attributes", $state)');
+
+		if (isset($state['Destination']['userid.attribute'])) {
+			$attributeName = $state['Destination']['userid.attribute'];
+		} elseif (isset($state['Source']['userid.attribute'])) {
+			$attributeName = $state['Source']['userid.attribute'];
+		} else {
+			/* Default attribute. */
+			$attributeName = 'eduPersonPrincipalName';
+		}
+
+		if (!array_key_exists($attributeName, $state['Attributes'])) {
+			return;
+		}
+
+		$uid = $state['Attributes'][$attributeName];
+		if (count($uid) === 0) {
+			SimpleSAML_Logger::warning('Empty user id attribute \'' . $attributeName . '\'.');
+			return;
+		}
+
+		if (count($uid) > 1) {
+			SimpleSAML_Logger::warning('Multiple attribute values for user id attribute \'' .
+				$attributeName . '\'.');
+		}
+
+		$uid = $uid[0];
+		$state['UserID'] = $uid;
+	}
+
 }
 
 ?>
\ No newline at end of file
-- 
GitLab