You are here

function bakery_register_handler in Bakery Single Sign-On System 7.3

Special Bakery register callback registers the user and returns to slave.

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

File

./bakery.module, line 392

Code

function bakery_register_handler() {
  $bakery = bakery_get_bakery();
  $cookie = $bakery
    ->validateSubCookie();
  if (!$cookie) {
    return MENU_ACCESS_DENIED;
  }
  if (variable_get('user_register', 1)) {

    // Users are allowed to register.
    $data = array();

    // Save errors.
    $errors = array();
    $name = trim($cookie['data']['name']);
    $mail = trim($cookie['data']['mail']);

    // Check if user exists with same email.
    $account = user_load_by_mail($mail);
    if ($account) {
      $errors['mail'] = 1;
    }
    else {

      // Check username.
      $account = user_load_by_name($name);
      if ($account) {
        $errors['name'] = 1;
      }
    }
  }
  else {
    watchdog('bakery', 'Master Bakery site user registration is disabled but users are trying to register from a subsite.', array(), WATCHDOG_ERROR);
    $errors['register'] = 1;
  }
  if (empty($errors)) {

    // Create user.
    $userinfo = $cookie['data'];
    if (!$cookie['data']['pass']) {
      $pass = user_password();
    }
    else {
      $pass = $cookie['data']['pass'];
    }

    // Set additional properties.
    $userinfo['name'] = $name;
    $userinfo['mail'] = $mail;
    $userinfo['pass'] = $pass;
    $userinfo['init'] = $mail;
    $userinfo['status'] = 1;
    $userinfo['authname_bakery'] = $name;
    $account = user_save('', $userinfo);

    // Set some info to return to the slave.
    $data['uid'] = $account->uid;
    $data['mail'] = $mail;
    watchdog('user', 'New external user: %name using module bakery from slave !slave.', array(
      '%name' => $account->name,
      '!slave' => $cookie['slave'],
    ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

    // Redirect to slave.
    if (!variable_get('user_email_verification', TRUE)) {

      // Create identification cookie and log user in.
      $params = array(
        'name' => $account->name,
        'mail' => $account->mail,
        'init' => _bakery_init_field($account->uid),
        'uid' => $account->uid,
      );
      $bakery
        ->setSsoCookie($params);

      // If any implementations of hook_user_login() do a redirect it will
      // break Bakery's registration flow.
      bakery_user_external_login($account);
    }
    else {

      // The user needs to validate their email, redirect back to slave to
      // inform them.
      $errors['validate'] = 1;
    }
  }
  else {

    // There were errors.
    session_destroy();
  }

  // Redirect back to custom Bakery callback on slave.
  $data['errors'] = $errors;
  $data['name'] = $name;

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

  // Bake a new cookie for validation on the slave.
  $bakery
    ->setSubCookie($name, $data, $cookie['slave']);
  drupal_goto($cookie['slave'] . 'bakery/register');
}