You are here

function bakery_profile_eat_stroopwafel_cookie in Bakery Single Sign-On System 7.4

Menu callback, invoked on the slave

1 string reference to 'bakery_profile_eat_stroopwafel_cookie'
bakery_profile_menu in ./bakery_profile.module
Implements hook_menu().

File

./bakery_profile.module, line 134

Code

function bakery_profile_eat_stroopwafel_cookie() {

  // the session got set during validation
  $stroopwafel = $_SESSION['bakery'];
  unset($_SESSION['bakery']);
  $init = _bakery_init_field($stroopwafel['uid']);

  // check if the user exists.
  $account = user_load_multiple(array(), array(
    'init' => $init,
  ));
  if (empty($account)) {

    // user not present
    $message = t('Account not found on %slave.', array(
      '%slave' => variable_get('site_name', ''),
    ));
  }
  else {
    $account = reset($account);
    drupal_add_http_header('X-Drupal-bakery-UID', $account->uid);

    // If profile field is enabled we manually save profile fields along the way.
    $fields = array();
    foreach (variable_get('bakery_supported_fields', array(
      'mail' => 'mail',
      'name' => 'name',
    )) as $type => $value) {
      if ($value) {

        // If the field is set in the cookie it's being updated, otherwise we'll
        // populate $fields with the existing values so nothing is lost.
        if (isset($stroopwafel[$type])) {
          $fields[$type] = $stroopwafel[$type];
        }
        else {
          $fields[$type] = $account->{$type};
        }
      }
    }
    $status = user_save($account, $fields);
    if ($status === FALSE) {
      watchdog('bakery', 'User update from name %name_old to %name_new, mail %mail_old to %mail_new failed.', array(
        '%name_old' => $account->name,
        '%name_new' => $stroopwafel['name'],
        '%mail_old' => $account->mail,
        '%mail_new' => $stroopwafel['mail'],
      ), WATCHDOG_ERROR);
      $message = t('There was a problem updating your account on %slave. Please contact the administrator.', array(
        '%slave' => variable_get('site_name', ''),
      ));
      header('HTTP/1.1 409 Conflict');
    }
    else {
      watchdog('bakery', 'user updated name %name_old to %name_new, mail %mail_old to %mail_new.', array(
        '%name_old' => $account->name,
        '%name_new' => $stroopwafel['name'],
        '%mail_old' => $account->mail,
        '%mail_new' => $stroopwafel['mail'],
      ));
      $message = t('Successfully updated account on %slave.', array(
        '%slave' => variable_get('site_name', ''),
      ));
    }

    // Invoke hook_bakery_receive().
    module_invoke_all('bakery_receive', $account, $stroopwafel);
  }
  module_invoke_all('exit');
  print $message;
  exit;
}