You are here

function bakery_user_update in Bakery Single Sign-On System 7.2

Same name and namespace in other branches
  1. 8.2 bakery.module \bakery_user_update()
  2. 7.4 bakery.module \bakery_user_update()
  3. 7.3 bakery.module \bakery_user_update()

Implements hook_user_update().

File

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

Code

function bakery_user_update(&$edit, $account, $category) {
  global $user;

  // We need to push changes.
  if (variable_get('bakery_is_master', 0) && isset($_SESSION['bakery'])) {
    $type = 'stroopwafel';
    $key = variable_get('bakery_key', '');
    $payload['data'] = serialize($_SESSION['bakery']);
    $payload['timestamp'] = $_SERVER['REQUEST_TIME'];
    $payload['uid'] = $account->uid;
    $payload['category'] = $category;
    $payload['type'] = $type;
    $data = bakery_bake_data($payload);

    // Respond with encrypted and signed account information.
    $payload = drupal_http_build_query(array(
      $type => $data,
    ));
    unset($_SESSION['bakery']);

    // Now update the slaves.
    $slaves = variable_get('bakery_slaves', array());
    foreach ($slaves as $slave) {
      $options = array(
        'headers' => array(
          'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
        ),
        'method' => 'POST',
        'data' => $payload,
      );
      $result = drupal_http_request($slave . 'bakery/update', $options);
      if ($result->code != 200) {
        drupal_set_message(t('Error %error for site at %url', array(
          '%error' => $result->code . ' ' . $result->error,
          '%url' => $slave,
        )));
      }
      else {
        if (user_access('administer bakery')) {
          drupal_set_message($result->data);
        }

        // TODO: Roll back the change.
      }
    }
    if ($user->uid === $account->uid) {

      // Rebake SSO cookie so user stays authenticated.
      $init = _bakery_init_field($account->uid);
      _bakery_bake_chocolatechip_cookie($account->name, $account->mail, $init);
    }
  }
}