You are here

function bakery_user in Bakery Single Sign-On System 6

Same name and namespace in other branches
  1. 6.2 bakery.module \bakery_user()

Implementation of hook_user().

File

./bakery.module, line 87

Code

function bakery_user($op, &$array, &$account, $category = NULL) {
  if ($op == 'login') {
    if (variable_get('bakery_is_master', 0)) {
      _bakery_bake_chocolatechip_cookie($account->name, $account->mail, url("user/{$account->uid}/edit", array(
        'absolute' => TRUE,
      )));
      _bakery_taste_oatmeal_cookie();
    }
  }
  else {
    if ($op == 'logout') {

      // eat SSO cookie
      _bakery_eat_cookie();

      // eat session cookie
      _bakery_eat_cookie(session_name());
    }
    else {
      if ($op == 'update' && variable_get('bakery_is_master', 0)) {

        // We store email/name if they changed. We want to wait with doing
        // anything else until the changes are saved locally.
        $newly_saved_user = user_load($account->uid);
        foreach (variable_get('bakery_supported_fields', array(
          'mail' => 'mail',
          'name' => 'name',
        )) as $type => $enabled) {

          // Profile fields are unset by this point so we have to get them from the DB and use whichever is populated.
          $value = isset($array[$type]) ? $array[$type] : $newly_saved_user->{$type};
          if ($enabled && isset($value)) {
            $_SESSION['bakery'][$type] = $value;
          }
        }
      }
      else {
        if ($op == 'after_update' && variable_get('bakery_is_master', 0) && isset($_SESSION['bakery'])) {
          $key = variable_get('bakery_key', '');
          $payload['data'] = serialize($_SESSION['bakery']);
          $payload['timestamp'] = $_SERVER['REQUEST_TIME'];
          $payload['uid'] = $account->uid;
          $payload['category'] = $category;
          $payload['signature'] = hash_hmac('sha256', $payload['data'] . '/' . $payload['uid'] . '/' . $payload['timestamp'], $key);
          $payload = drupal_query_string_encode(array(
            'stroopwafel' => bakery_mix(serialize($payload), 1),
          ));
          unset($_SESSION['bakery']);

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

              // TODO: Roll back the change.
            }
          }
        }
        else {
          if ($op == 'view' && !variable_get('bakery_is_master', 0)) {
            if (substr($account->init, 0, strlen(variable_get('bakery_master', 'http://drupal.org/'))) == variable_get('bakery_master', 'http://drupal.org/')) {
              $account->content['summary']['master_profile'] = array(
                '#type' => 'item',
                '#title' => t('Master profile'),
                '#value' => l(t('Profile on master site'), substr($account->init, 0, strlen($account->init) - 5)),
                '#attributes' => array(
                  'class' => 'og_groups',
                ),
                '#access' => user_access('access user profiles'),
              );
            }
          }
        }
      }
    }
  }
}