You are here

function oauthconnector_edit_form_provider in OAuth Connector 7

Same name and namespace in other branches
  1. 6 oauthconnector.admin.inc \oauthconnector_edit_form_provider()

Form to edit the settings of a provider.

1 string reference to 'oauthconnector_edit_form_provider'
oauthconnector_edit_provider in ./oauthconnector.admin.inc
Edit a provider.

File

./oauthconnector.admin.inc, line 205
Administrative functions for the OAuth Connector module.

Code

function oauthconnector_edit_form_provider($form, &$form_state, $provider) {
  $form = array();
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => isset($provider->pid) ? $provider->pid : '',
  );
  $form['provider'] = array(
    '#type' => 'value',
    '#value' => $provider,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('A human-readable title for the provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->title,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#maxlength' => 24,
    '#default_value' => $provider->name,
    '#title' => t('Name'),
    '#description' => t('A unique machine-readable name used to identify this provider internally. It may only contain lowercase alphanumeric characters and underscores. No spaces or uppercase characters.'),
    '#required' => TRUE,
    '#machine_name' => array(
      'exists' => 'oauthconnector_edit_form_provider_validate_name_exist',
      'source' => array(
        'title',
      ),
    ),
  );
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Base URL'),
    '#description' => t('A URL to the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->url,
  );
  $form['consumer_key'] = array(
    '#type' => 'textfield',
    '#title' => t('OAuth Consumer Key'),
    '#description' => t('Your consumer key provided by the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => isset($provider->consumer->key) ? $provider->consumer->key : NULL,
  );
  $form['consumer_secret'] = array(
    '#type' => 'textfield',
    '#title' => t('OAuth Consumer Secret'),
    '#description' => t('Your consumer secret provided by the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => isset($provider->consumer->secret) ? $provider->consumer->secret : NULL,
  );
  $form['consumer_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('OAuth Consumer Advanced Settings'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['consumer_advanced']['oauth2'] = array(
    '#type' => 'checkbox',
    '#title' => t('Oauth v2'),
    '#default_value' => $provider->consumer_advanced['oauth2'],
  );
  $sign_methods = array(
    'HMAC-SHA1' => 'HMAC-SHA1',
  );
  foreach (hash_algos() as $algo) {
    $sign_methods['HMAC-' . strtoupper($algo)] = 'HMAC-' . strtoupper($algo);
  }
  $sign_methods['PLAINTEXT'] = 'PLAINTEXT';
  $form['consumer_advanced']['signature method'] = array(
    '#type' => 'select',
    '#title' => t('Signature method'),
    '#options' => $sign_methods,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['signature method'],
  );
  $form['consumer_advanced']['authentication realm'] = array(
    '#type' => 'textfield',
    '#title' => t('Authentication realm'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => $provider->consumer_advanced['authentication realm'],
    '#states' => array(
      'invisible' => array(
        ':input[name="consumer_advanced\\[oauth2\\]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['consumer_advanced']['request token endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Request token endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => $provider->consumer_advanced['request token endpoint'],
    '#states' => array(
      'invisible' => array(
        ':input[name="consumer_advanced\\[oauth2\\]"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        ':input[name="consumer_advanced\\[oauth2\\]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  $form['consumer_advanced']['authorization scope'] = array(
    '#type' => 'textarea',
    '#title' => t('Scope'),
    '#description' => t('Scope for the authorization endpoint.'),
    '#default_value' => $provider->consumer_advanced['authorization scope'],
    '#states' => array(
      'invisible' => array(
        ':input[name="consumer_advanced\\[oauth2\\]"]' => array(
          'checked' => FALSE,
        ),
      ),
      'required' => array(
        ':input[name="consumer_advanced\\[oauth2\\]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['consumer_advanced']['authorization endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Authorization endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['authorization endpoint'],
  );
  $form['consumer_advanced']['access token endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Access token endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['access token endpoint'],
  );
  $mapping_description = t('Map the attributes from the API response to the attributes useable by OAuth Connector.');
  if (!module_exists('querypath')) {
    $mapping_description .= '<br/>';
    $mapping_description .= t('If you have the !QueryPath module installed the field selection can be made by a CSS selector.', array(
      '!QueryPath' => l('QueryPath', 'http://drupal.org/project/querypath'),
    ));
  }
  $form['mapping'] = array(
    '#type' => 'fieldset',
    '#title' => t('Mapping'),
    '#description' => $mapping_description,
    '#tree' => TRUE,
    'fields' => array(),
  );
  $form['mapping']['format'] = array(
    '#type' => 'radios',
    '#title' => t('Format'),
    '#options' => array(
      'json' => 'JSON',
      'php' => 'PHP',
      'xml' => 'XML',
    ),
    '#default_value' => empty($provider->mapping['format']) ? 'json' : $provider->mapping['format'],
    '#weight' => -50,
  );
  $mappings = oauthconnector_fields();
  $profile_fields = array(
    'name' => t('Username'),
    'mail' => t('E-mail address'),
  );
  if (variable_get('user_pictures', 0)) {
    $profile_fields['picture'] = t('Picture');
  }
  if (variable_get('configurable_timezones', 0)) {
    $profile_fields['timezone'] = t('Time zone');
  }
  if (variable_get('user_signatures', 0)) {
    $profile_fields['signature'] = t('Signature');
  }
  if (variable_get('user_signatures', 0)) {
    $profile_fields['language'] = t('Language');
  }
  if (module_exists('field')) {
    $fields = field_info_instances('user', 'user');
    foreach ($fields as $key => $field) {
      $profile_fields[$key] = t($field['label']);
    }
  }
  foreach ($mappings as $key => $mapping) {
    $form['mapping']['fields'][$key] = array(
      '#type' => 'fieldset',
      '#title' => $mapping['title'],
      '#description' => $mapping['description'],
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    if ($key == 'uid') {
      $form['mapping']['fields'][$key]['#collapsible'] = FALSE;
      $form['mapping']['fields'][$key]['#weight'] = -49;
    }
    $form['mapping']['fields'][$key]['resource'] = array(
      '#type' => 'textfield',
      '#title' => t('Resource'),
      '#description' => t('The URL of the API resource representing the authorized user.'),
      '#size' => 32,
      '#maxlength' => 255,
      '#required' => !empty($mapping['required']),
      '#default_value' => empty($provider->mapping['fields'][$key]['resource']) ? '' : $provider->mapping['fields'][$key]['resource'],
    );
    $form['mapping']['fields'][$key]['method post'] = array(
      '#type' => 'checkbox',
      '#title' => t('POST request'),
      '#default_value' => !empty($provider->mapping['fields'][$key]['method post']),
    );
    $form['mapping']['fields'][$key]['field'] = array(
      '#type' => 'textfield',
      '#title' => t('Field'),
      '#size' => 32,
      '#maxlength' => 32,
      '#required' => !empty($mapping['required']),
      '#default_value' => empty($provider->mapping['fields'][$key]['field']) ? '' : $provider->mapping['fields'][$key]['field'],
    );
    $form['mapping']['fields'][$key]['querypath'] = array(
      '#type' => 'checkbox',
      '#title' => t('Field is a CSS selector'),
      '#default_value' => !empty($provider->mapping['fields'][$key]['querypath']),
      '#access' => module_exists('querypath'),
    );
    $form['mapping']['fields'][$key]['sync_with_field'] = array(
      '#type' => 'select',
      '#options' => array(
        '' => t('- Does not match -'),
      ) + $profile_fields,
      '#title' => t('Field to match on user profile'),
      '#default_value' => isset($provider->mapping['fields'][$key]['sync_with_field']) ? $provider->mapping['fields'][$key]['sync_with_field'] : '',
    );
  }
  $label = empty($provider->pid) ? t('Save and proceed') : t('Save');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $label,
  );
  return $form;
}