From b5584f6fdf1d12cb17772285dcab5dc498300aa2 Mon Sep 17 00:00:00 2001 From: Jesse Sivonen Date: Fri, 6 Oct 2017 12:09:01 +0300 Subject: [PATCH] Populate custom fields automatically It makes perfect sense that fields from `Additional attributes` are populated automatically, instead of user having to hardcode how the fields are populated in source code of this module. This implementation has no support for translations or more complex fields. But it should work fine with basic fields that have simple value column. The implementation is inspired by comment by @pablo-tapia in AngelAlvarado/multiple_idp_simplesamlphp#4. --- includes/simplesamlphp_auth.drupal.inc | 7 ++++++- simplesamlphp_auth.inc | 26 +++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/includes/simplesamlphp_auth.drupal.inc b/includes/simplesamlphp_auth.drupal.inc index 0cdd638..7395a8e 100644 --- a/includes/simplesamlphp_auth.drupal.inc +++ b/includes/simplesamlphp_auth.drupal.inc @@ -137,7 +137,12 @@ class SimpleSAML_Drupal { if ($key == 'cs_fields') { foreach ($data as $additional_data) { $additional_data = explode(':', $additional_data); - $this->_simplesaml_auth_source->{$additional_data[0]} = $additional_data[1]; + if (isset($additional_data[1]) && !empty($additional_data[1])) { + // Wrap all custom fields around the custom fields property to access them later + $this->_simplesaml_auth_source->custom_fields[$additional_data[0]] = $additional_data[1]; + } else { + watchdog('simplesamlphp_auth', 'Unable to load custom field into the SAML object, verify that you\'re using the correct operator', array(), WATCHDOG_DEBUG); + } // end if - else } // end foreach } // end if $this->_simplesaml_auth_source->{$key} = $data; diff --git a/simplesamlphp_auth.inc b/simplesamlphp_auth.inc index 4a853dd..78cbb6f 100644 --- a/simplesamlphp_auth.inc +++ b/simplesamlphp_auth.inc @@ -133,22 +133,26 @@ function _simplesaml_auth_user_register($authname) { 'timezone' => date_default_timezone_get(), ); + /** + * Populate custom fields + */ + if (isset($simplesamlphp_authsource->custom_fields)) { + foreach($simplesamlphp_authsource->custom_fields as $field_name => $attribute_name) { + if (!empty($field_name) && !empty($attribute_name)) { + $userinfo[$field_name] = array( + 'und' => array(array('value' => $simplesamlphp_drupal->getAttrsFromAssertion($attribute_name))) + ); + } // end if + } // end foreach + } // end if + /** - * If you want, you can place more custom fields and populate them here - * @example - * if (isset($simplesamlphp_authsource->firstname)) { - * $userinfo['field_first_name'] = array( - * 'und' => array(array('value' => $simplesamlphp_drupal->getAttrsFromAssertion($simplesamlphp_authsource->firstname))) - * ); - * } - */ - /** - * Populate roles + * Populate roles */ $roles = array(); try { // Get SAML attributes - $attrs = $simplesamlphp_auth_as->getAttributes(); + $attrs = $simplesamlphp_auth_as->getAttributes(); if ($simplesamlphp_authsource->syncroles) { // The roles provided by the IdP.