You are here

function oauth2_server_form_submit in OAuth2 Server 7

Form API submit callback for the type form.

File

includes/oauth2_server.server_admin.inc, line 244
Admin UI for servers.

Code

function oauth2_server_form_submit(&$form, &$form_state) {

  // Save the previous / initial server settings for later comparison.
  $previous_settings = $form_state['build_info']['args'][0]->settings;

  // Advanced settings are a part of the main settings array.
  $form_state['values']['settings'] += $form_state['values']['settings']['advanced_settings'];
  unset($form_state['values']['settings']['advanced_settings']);

  // Create the server from form values and save it.
  $server = entity_ui_form_submit_build_entity($form, $form_state);
  $server
    ->save();

  // If OpenID Connect was just enabled, create its scopes.
  $previous_value = !empty($previous_settings['use_openid_connect']);
  $current_value = !empty($server->settings['use_openid_connect']);
  if (!$previous_value && $current_value) {
    $site_name = variable_get('site_name', 'Drupal');
    $openid_scopes = array(
      'openid' => format_string('Know who you are on @site', array(
        '@site' => $site_name,
      )),
      'offline_access' => "Access the API when you're not present.",
      'email' => 'View your email address.',
      'profile' => 'View basic information about your account.',
    );
    foreach ($openid_scopes as $name => $description) {
      $scope = entity_create('oauth2_server_scope', array());
      $scope->server = $server->name;
      $scope->name = $name;
      $scope->description = $description;
      $scope
        ->save();
    }
  }

  // If OpenID Connect was just disabled, delete its scopes.
  if ($previous_value && !$current_value) {
    $scope_names = array(
      'openid',
      'offline_access',
      'email',
      'profile',
    );
    $scopes = oauth2_server_scope_load_multiple($server->name, $scope_names);
    foreach ($scopes as $scope) {
      $scope
        ->delete();
    }

    // If we just deleted a default scope, update the server.
    if (in_array($server->settings['default_scope'], $scope_names)) {
      $server->settings['default_scope'] = '';
      $server
        ->save();
    }
  }
  $form_state['redirect'] = 'admin/structure/oauth2-servers';
}