You are here

function ga_login_create_form_submit in Google Authenticator login 7

Same name and namespace in other branches
  1. 6 ga_login.module \ga_login_create_form_submit()

Submit handler to create a new code.

File

./ga_login.pages.inc, line 262
ga_login pages.

Code

function ga_login_create_form_submit($form, &$form_state) {

  // If admin only allows one generation type, then assign that type
  // and skip the first page.
  $types = variable_get('ga_login_generation_types', 'BOTH');
  if ($form_state['step'] == '0' && $types != 'BOTH') {
    $form_state['values']['tokentype'] = $types;
    $form_state['step']++;
  }
  $step = $form_state['step'];
  switch ($step) {
    case '1':
      $account = user_load($form_state['values']['uid']);
      $username = _ga_login_username($account);
      $ga = _ga_login_get_class();

      // don't save the data to the db until the user approves it.
      $data = $ga
        ->unapprovedUser($username, $form_state['values']['tokentype']);
      $data['secret'] = $ga
        ->helperhex2b32($data['tokenkey']);
      $form_state['url'] = $ga
        ->createURL($username, $data);
      $data['username'] = _ga_login_username($account, FALSE);
      $form_state['data'] = $data;
      break;
    case '2':
      if (isset($form_state['values']['approve_current_submit'])) {

        // Don't need to save the code here, since the
        // ga_login_create_form_validate function does that for us.
        $account = user_load($form_state['values']['uid']);

        // Enable TFA for this account after they generate a code.
        user_save($account, array(
          'data' => array(
            'ga_login_force_tfa' => TRUE,
          ),
        ));
        if (module_exists('mobile_codes')) {
          if (file_unmanaged_delete($form_state['values']['code_image_path'])) {
            drupal_set_message(t('Mobile code image was successfully deleted.'));
          }
          else {
            drupal_set_message(t('Error while trying to delete the mobile code image.'), 'error');
          }
        }

        // Redirect the user to the page specified by the adminsitrator.
        $destination = variable_get('ga_login_redirect_after_save', '');
        switch ($destination) {
          case '':

            // Only redirect if user has still access.
            if (user_access('create own login code', $account) || user_access('create others login codes', $account)) {
              $form_state['redirect'] = 'user/' . $account->uid . '/ga_login';
            }
            else {
              $form_state['redirect'] = 'user/' . $account->uid;
            }
            break;
          case '<front>':
            $form_state['redirect'] = '';
            break;
          default:
            $form_state['redirect'] = $destination;
            break;
        }
      }
      drupal_set_message(t('You can now log in with your new code.'));
      break;
  }
  $form_state['step']++;
  if ($form_state['step'] > 2) {
    $form_state['step'] = 0;
  }
  if ($step == '0' || $step == '1') {
    $form_state['rebuild'] = TRUE;
  }
}