You are here

function oauth_common_edit_form_context_validate in OAuth 1.0 6.3

Same name and namespace in other branches
  1. 7.4 oauth_common.admin.inc \oauth_common_edit_form_context_validate()
  2. 7.3 oauth_common.admin.inc \oauth_common_edit_form_context_validate()

Validate submission of the preset edit form.

File

./oauth_common.admin.inc, line 416

Code

function oauth_common_edit_form_context_validate(&$form, &$form_state) {
  $values = $form_state['values'];

  // Test uniqueness of name:
  if (preg_match("/[^A-Za-z0-9_]/", $values['name'])) {
    form_error($form['name'], t('Context name must be alphanumeric or underscores only.'));
  }
  else {
    if (!empty($values['name'])) {
      $query = "SELECT cid FROM {oauth_common_context} WHERE name = '%s'";
      $args = array(
        ':name' => $values['name'],
      );
      if (!empty($values['cid']) && is_numeric($values['cid'])) {
        $query .= ' AND cid != %d';
        $args[':cid'] = $values['cid'];
      }
      if (db_result(db_query($query, $args))) {
        form_error($form['name'], t('Context name must be unique.'));
      }
    }
  }

  // Check that the authorization level names are unique within the context
  $levels = array();
  $auth_count = $values['authorization_level_count'];
  $default_exists = FALSE;
  $level_count = $values['authorization_level_count'];
  for ($idx = 0; $idx < $level_count; $idx++) {
    $level = _oauth_common_auth_level_from_values($idx, $values);
    if (!empty($level['name']) && !$level['delete']) {
      if (!empty($levels[$level['name']])) {
        form_error($form["l_{$idx}_name"], t('Authorization level name must be unique.'));
      }
      else {
        if (preg_match("/[^A-Za-z0-9_\\*]/", $level['name'])) {
          form_error($form["l_{$idx}_name"], t('Authorization level name must be alphanumeric or underscores only.'));
        }
      }
      if (empty($level['title'])) {
        form_error($form["l_{$idx}_title"], t('Authorization levels must have a title.'));
      }
      $default_exists = $default_exists || $level['default'];
      $levels[$level['name']] = TRUE;
    }
  }

  // Check that we actually got a number as access token lifetime
  if (!is_numeric($values['authorization_options']['access_token_lifetime'])) {
    form_error($form['authorization_options']['access_token_lifetime'], t('The access token lifetime must be numeric.'));
  }

  // Check that at least one default authorization level is checked when
  // authorization level selection is disabled.
  if (!$default_exists && $values['authorization_options']['disable_auth_level_selection']) {
    form_error($form['authorization_options']['disable_auth_level_selection'], t('You must select at least one default authorirization level if level selection is disabled.'));
  }
}