You are here

function bakery_register in Bakery Single Sign-On System 6.2

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

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

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

File

./bakery.module, line 604

Code

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

    // Valid cookie.
    // Destroy the current oatmeal cookie, we'll set a new one when we return to the slave.
    _bakery_eat_cookie('OATMEAL');
    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']);

      // Run access rule checks.
      if (drupal_is_denied('user', $name)) {
        $errors['name_denied'] = 1;
      }
      if (drupal_is_denied('mail', $mail)) {
        $errors['mail_denied'] = 1;
      }

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

        // Check username.
        $account = user_load(array(
          '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.
        $init = _bakery_init_field($account->uid);
        _bakery_bake_chocolatechip_cookie($account->name, $account->mail, $init);
        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_bake_oatmeal_cookie($name, $data);
    drupal_goto($cookie['slave'] . 'bakery/register');
  }

  // Invalid request.
  drupal_access_denied();
}