Skip to content

Commit

Permalink
Populate custom fields automatically
Browse files Browse the repository at this point in the history
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#4.
  • Loading branch information
joas8211 committed Oct 6, 2017
1 parent 57100cf commit b5584f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 6 additions & 1 deletion includes/simplesamlphp_auth.drupal.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 15 additions & 11 deletions simplesamlphp_auth.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b5584f6

Please sign in to comment.