You are here

function bakery_eat_stroopwafel_cookie in Bakery Single Sign-On System 7.2

Same name and namespace in other branches
  1. 6.2 bakery.module \bakery_eat_stroopwafel_cookie()
  2. 6 bakery.module \bakery_eat_stroopwafel_cookie()

Menu callback, invoked on the slave.

1 string reference to 'bakery_eat_stroopwafel_cookie'
bakery_menu in ./bakery.module
Implements hook_menu().

File

./bakery.module, line 1373
Module file for the Bakery.

Code

function bakery_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();
    $watchdog_message = array();
    $watchdog_variables = 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];
          if (in_array($type, array(
            'mail',
            'name',
          ))) {
            $watchdog_message[] = $type . ' %' . $type . '_old to %' . $type . '_new';
            $watchdog_variables['%' . $type . '_old'] = $account->{$type};
            $watchdog_variables['%' . $type . '_new'] = $stroopwafel[$type];
          }
        }
        else {
          $fields[$type] = $account->{$type};
        }
      }
    }
    $status = user_save($account, $fields);
    $uri = entity_uri('user', $account);
    $watchdog_message = '%name' . (empty($watchdog_message) ? '' : ': ' . implode(', ', $watchdog_message));
    $watchdog_variables['%name'] = $account->name;
    if ($status === FALSE) {
      watchdog('bakery', 'User update ' . $watchdog_message . ' failed.', $watchdog_variables, WATCHDOG_ERROR, l(t('View user'), $uri['path'], $uri['options']));
      $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 ' . $watchdog_message . '.', $watchdog_variables, WATCHDOG_NOTICE, l(t('View user'), $uri['path'], $uri['options']));
      $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;
}