You are here

function gauth_account_save in Google Auth 7.2

Same name and namespace in other branches
  1. 7 gauth.module \gauth_account_save()

Save an account.

@returns a account array

Same account array reflects the changes

Parameters

array $account: Account array that needs to be saved If you want to create a new account omit the id field in the array If you want to update existing account do have the id field

3 calls to gauth_account_save()
gauth_account_edit_form_submit in ./gauth.admin.inc
Submit handler for adding a new account to google auth accounts.
gauth_response_handler in ./gauth.module
Function to handle authentication and response from google.
gauth_user_services_user_account_create in gauth_user/gauth_user.pages.inc
Function creates a service account of the specified type.

File

./gauth.module, line 368
Google Auth Api for drupal.

Code

function gauth_account_save(&$account) {
  global $user;
  if (isset($account['id'])) {
    $fields = array(
      'id' => $account['id'],
      'uid' => isset($account['uid']) ? $account['uid'] : $user->uid,
    );
    if (isset($_SESSION['gauth_account_id']) && isset($account['access_token']) && $_SESSION['gauth_account_id'] == $account['id']) {
      $fields['access_token'] = $account['access_token'];
      $fields['is_authenticated'] = TRUE;
    }
    else {
      if (isset($account['name'])) {
        $fields['name'] = check_plain($account['name']);
      }
      $old_account = gauth_account_load($account['id'], FALSE);
      $is_authenticated = $old_account['is_authenticated'];
      if (isset($account['client_id'])) {
        $fields['client_id'] = check_plain($account['client_id']);
        if ($is_authenticated && $old_account['client_id'] != $account['client_id']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['client_secret'])) {
        $fields['client_secret'] = check_plain($account['client_secret']);
        if ($is_authenticated && $old_account['client_secret'] != $account['client_secret']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['developer_key'])) {
        $fields['developer_key'] = check_plain($account['developer_key']);
        if ($is_authenticated && $old_account['developer_key'] != $account['developer_key']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['services'])) {
        if (is_array($account['services'])) {
          $account['services'] = implode(",", $account['services']);
        }
        $fields['services'] = check_plain($account['services']);
        if ($is_authenticated && $old_account['services'] != $account['services']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['access_type'])) {
        $fields['access_type'] = check_plain($account['access_type']);
        if ($is_authenticated && $old_account['access_type'] != $account['access_type']) {
          $is_authenticated = FALSE;
        }
      }
      $fields['is_authenticated'] = $is_authenticated;
    }

    // Let other modules modify the account before saving existing account.
    foreach (module_implements('gauth_account_update') as $module) {
      $function = $module . '_gauth_account_update';
      $function($fields, gauth_account_load($fields['id'], FALSE));
    }
    if (drupal_write_record('gauth_accounts', $fields, 'id') == SAVED_UPDATED) {
      return $fields;
    }
    else {
      return FALSE;
    }
  }
  else {
    if (!isset($account['name'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Name is required for creating new account',
      );
    }
    if (!isset($account['client_id'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Client Id can\'t be Null',
      );
    }
    if (!isset($account['client_secret'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Client Secret can\'t be Null',
      );
    }
    if (!isset($account['developer_key'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Developer Key can\'t be Null',
      );
    }
    $fields = array(
      'name' => check_plain($account['name']),
      'developer_key' => check_plain($account['developer_key']),
      'client_id' => check_plain($account['client_id']),
      'client_secret' => check_plain($account['client_secret']),
      'access_type' => check_plain($account['access_type']),
      'uid' => $user->uid,
    );
    if (is_array($account['services'])) {
      $account['services'] = implode(",", $account['services']);
    }
    $fields['services'] = check_plain($account['services']);
    $accounts = gauth_account_load(NULL, TRUE, array(
      'name',
    ));
    $accounts = array_keys($accounts);
    if (in_array($account['name'], $accounts)) {
      return array(
        'is_error' => TRUE,
        'message' => 'Name is already in use. Please choose a unique name for the account',
      );
    }

    // Let other modules modify the account before saving new account.
    module_invoke_all('gauth_account_insert', $fields);
    if (drupal_write_record('gauth_accounts', $fields) == SAVED_NEW) {
      return $fields;
    }
    else {
      return FALSE;
    }
  }
}