You are here

function bakery_login in Bakery Single Sign-On System 6.2

Same name and namespace in other branches
  1. 7.2 bakery.module \bakery_login()

Special Bakery login callback authenticates the user and returns to slave.

1 string reference to 'bakery_login'
bakery_menu in ./bakery.module
Implementation of hook_menu().

File

./bakery.module, line 772

Code

function bakery_login() {
  $cookie = bakery_taste_oatmeal_cookie();
  if ($cookie) {

    // Make sure there are query defaults.
    $cookie['data'] += array(
      'query' => array(),
    );
    $errors = array();

    // Remove the data pass cookie.
    _bakery_eat_cookie('OATMEAL');

    // First see if the user_login form validation has any errors for them.
    $name = trim($cookie['data']['name']);
    $pass = trim($cookie['data']['pass']);

    // Execute the login form.
    $form_state = array();
    $form_state['values'] = $cookie['data'];
    drupal_execute('user_login', $form_state);
    $errors = form_get_errors();
    if (empty($errors)) {
      $account = NULL;

      // PHPass module hashes differently so cannot rely on user_load().
      if (module_exists('phpass')) {
        require_once drupal_get_path('module', 'phpass') . '/password.inc';
        $account = _phpass_load_user($name, $pass);
      }
      else {
        $account = user_load(array(
          'name' => $name,
          'pass' => $pass,
        ));
      }
      if ($account->uid) {

        // Check if the mail is denied.
        if (drupal_is_denied('user', $account->mail)) {
          $errors['name'] = t('The name %name is registered using a reserved e-mail address and therefore could not be logged in.', array(
            '%name' => $name,
          ));
        }
        else {

          // Passed all checks, create identification cookie and log in.
          $init = _bakery_init_field($account->uid);
          _bakery_bake_chocolatechip_cookie($account->name, $account->mail, $init);
          global $user;
          $user = $account;
          $edit = array(
            'name' => $user->name,
          );
          bakery_user_authenticate_finalize($edit);
        }
      }
      else {
        $errors['incorrect-credentials'] = 1;
      }
    }
    if (!empty($errors)) {

      // Report failed login.
      watchdog('user', 'Login attempt failed for %user.', array(
        '%user' => $name,
      ));

      // Clear the messages on the master's session, since they were set during
      // drupal_execute() and will be displayed out of context.
      drupal_get_messages();
    }

    // Bake a new cookie for validation on the slave.
    $data = array(
      'errors' => $errors,
      'name' => $name,
    );

    // Carry destination through login.
    if (isset($cookie['data']['destination'])) {
      $data['destination'] = $cookie['data']['destination'];
    }

    // Carry other query parameters through login.
    $data['query'] = $cookie['data']['query'];
    bakery_bake_oatmeal_cookie($name, $data);
    drupal_goto($cookie['slave'] . 'bakery/login');
  }
  drupal_access_denied();
}