You are here

function bakery_form_alter in Bakery Single Sign-On System 7.2

Same name and namespace in other branches
  1. 8.2 bakery.module \bakery_form_alter()
  2. 6.2 bakery.module \bakery_form_alter()
  3. 6 bakery.module \bakery_form_alter()
  4. 7.4 bakery.module \bakery_form_alter()
  5. 7.3 bakery.module \bakery_form_alter()

Implements hook_form_alter().

File

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

Code

function bakery_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_profile_form':
    case 'user_edit_form':
      if (!variable_get('bakery_is_master', 0) && !user_access('administer users')) {
        $init_url = _bakery_init_field_url($form['#user']->init);
        $index = key($form);
        if (isset($form['account'])) {
          drupal_set_message(t('You can change the name, mail, and password <a href="!url">at the master site</a>.', array(
            '!url' => check_url($init_url),
          )), 'status', FALSE);
          $form['account']['#access'] = FALSE;
          $form['account']['name']['#access'] = FALSE;
          $form['account']['pass']['#access'] = FALSE;
          $form['account']['mail']['#access'] = FALSE;
        }
        foreach (variable_get('bakery_supported_fields', array(
          'mail' => 'mail',
          'name' => 'name',
        )) as $type => $value) {
          if ($value) {
            switch ($type) {
              case 'mail':
              case 'name':
                break;
              case 'picture':
                if (isset($form['picture'])) {
                  $form['picture']['picture_delete']['#access'] = FALSE;
                  $form['picture']['picture_upload']['#access'] = FALSE;
                  $form['picture']['#description'] = t('You can change the image <a href="!url">at the master site</a>.', array(
                    '!url' => check_url($init_url),
                  ));
                }
                break;
              case 'language':
                if (isset($form['locale'][$type])) {
                  $form['locale'][$type]['#disabled'] = TRUE;
                  $form['locale'][$type]['#description'] .= ' ' . t('You can change the language setting <a href="!url">at the master site</a>.', array(
                    '!url' => check_url($init_url),
                  ));
                }
                break;
              case 'signature':
                if (isset($form['signature_settings'][$type])) {
                  $form['signature_settings'][$type]['#disabled'] = TRUE;
                  $form['signature_settings'][$type]['#description'] .= ' ' . t('You can change the signature <a href="!url">at the master site</a>.', array(
                    '!url' => check_url($init_url),
                  ));
                }
                break;
              default:
                if (isset($form[$type])) {
                  $form[$type]['#disabled'] = TRUE;
                }
                if (isset($form[$type][$type])) {
                  $form[$type][$type]['#disabled'] = TRUE;
                  $form[$type][$type]['#description'] .= ' ' . t('You can change this setting <a href="!url">at the master site</a>.', array(
                    '!url' => check_url($init_url),
                  ));
                }
                break;
            }
          }
        }
      }
      break;
    case 'user_register_form':
      if (variable_get('bakery_is_master', FALSE)) {
        $form['#submit'][] = '_bakery_login_redirect';
      }
      else {

        // Provide register ability on the slave sites.
        if (arg(0) == 'admin') {

          // Admin create user form. Add a note about account synchronization.
          $form['account']['bakery_help'] = array(
            '#markup' => t('<strong>Note:</strong> Only use this form to create accounts for users who exist on <a href="!url">@master</a> and not on this site. Be sure to use the exact same username and e-mail for the account here that they have on @master.', array(
              '!url' => variable_get('bakery_master', 'http://drupal.org'),
              '@master' => variable_get('bakery_master', 'http://drupal.org'),
            )),
            '#weight' => -100,
          );
        }
        elseif (variable_get('bakery_subsite_login', 0)) {

          // Anonymous user registration form.
          // Populate fields if set from previous attempt.
          if (isset($_SESSION['bakery']['register'])) {
            $form['account']['name']['#default_value'] = $_SESSION['bakery']['register']['name'];
            $form['account']['mail']['#default_value'] = $_SESSION['bakery']['register']['mail'];
            unset($_SESSION['bakery']['register']);
          }

          // Replace the submit handler with our own.
          $form['#submit'] = array(
            '_bakery_register_submit',
          );
        }
        else {

          // Redirect to main site for registration.
          $query = array();
          if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
            $query['bd'] = url($_GET['destination'], array(
              'absolute' => TRUE,
            ));

            // Do not let drupal_goto() use the destination.
            unset($_GET['destination']);
          }
          else {
            $query['bd'] = url('<front>', [
              'absolute' => TRUE,
            ]);
          }
          drupal_goto(variable_get('bakery_master', 'http://drupal.org/') . 'user/register', array(
            'query' => $query,
          ));
        }
      }
      break;
    case 'user_pass':

      // Slave sites need to make sure the local account exists, if the master
      // account exists.
      if (!variable_get('bakery_is_master', FALSE)) {
        array_unshift($form['#validate'], '_bakery_pass_validate');
      }
      break;
    case 'user_login_block':

      // If this is a subsite, and subsite login is disabled, remove the login
      // form.
      if (!variable_get('bakery_is_master', FALSE) && !variable_get('bakery_subsite_login', 0)) {
        $form['name']['#access'] = FALSE;
        $form['pass']['#access'] = FALSE;
        $form['actions']['#access'] = FALSE;
        break;
      }
    case 'user_login':
      if (variable_get('bakery_is_master', FALSE)) {

        // Use both validate and submit, in case other modules like TFA are
        // also altering the login process.
        $form['#validate'][] = '_bakery_login_redirect';
        $form['#submit'][] = '_bakery_login_redirect';
      }
      else {

        // Only process login on subsites if enabled, or if this is bakery
        // logging the user in from bakery_user_external_login().
        if (variable_get('bakery_subsite_login', 0) || isset($form_state['build_info']['args'][0]) && $form_state['build_info']['args'][0] === 'via_bakery') {

          // Replace two validators from user module because they log the user in
          // and test if account exists. We want to check if the account exists on
          // the master instead.
          $form['#validate'] = array_diff($form['#validate'], array(
            'user_login_authenticate_validate',
            'user_login_final_validate',
          ));

          // Replace the submit handler with our own to set a redirect cookie.
          $form['#submit'] = array(
            '_bakery_login_submit',
          );
        }
        else {

          // Redirect to main site for login.
          $query = array();
          if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
            $query['bd'] = url($_GET['destination'], array(
              'absolute' => TRUE,
            ));

            // Do not let drupal_goto() use the destination.
            unset($_GET['destination']);
          }
          else {
            $query['bd'] = url('<front>', [
              'absolute' => TRUE,
            ]);
          }
          drupal_goto(variable_get('bakery_master', 'http://drupal.org/') . 'user', array(
            'query' => $query,
          ));
        }
      }
      break;
  }
}