You are here

farm_api.install in farmOS 7

Farm API install file.

File

modules/farm/farm_api/farm_api.install
View source
<?php

/**
 * @file
 * Farm API install file.
 */

/**
 * Implements hook_install().
 */
function farm_api_install() {

  // Grant oauth2 server permissions to all users.
  // Permission is needed for clients to access the
  // /oauth2/authorize and oauth2/token endpoints anonymously,
  // and as an authenticated user.
  $permissions = array(
    'use oauth2 server',
  );
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, $permissions);
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions);

  // Set variable for restws_oauth2_server configuration.
  $server_name = 'farmos_oauth';
  variable_set('restws_oauth2_server_name', $server_name);
}

/**
 * Implements hook_uninstall().
 */
function farm_api_uninstall() {

  // Assume that the user does not change the OAuth2 Server or Scope
  // because they are using this module with farmOS. Otherwise this could
  // potentially delete other configured OAuth2 Servers and Scopes.
  $server_name = variable_get('restws_oauth2_server_name', 'farmos_oauth');

  // Get enabled OAuth Clients.
  $enabled_clients = variable_get('farm_api_enabled_clients', array());
  foreach ($enabled_clients as $client_key) {

    // Delete OAuth Client.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'oauth2_server_client')
      ->entityCondition('client_key', $client_key);
    $result = $query
      ->execute();
    if (isset($result['oauth2_server_client'])) {
      $client_id = array_keys($result['oauth2_server_client']);
      entity_delete('oauth2_server_client', $client_id);
    }
  }

  // Features will delete the OAuth2 Server entity.
  // Features does not delete the existing OAuth Scopes.
  // Delete farmOS OAuth Scopes.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'oauth2_server_scope')
    ->entityCondition('server', $server_name);
  $result = $query
    ->execute();
  if (isset($result['oauth2_server_scope'])) {
    $scope_ids = array_keys($result['oauth2_server_scope']);
    entity_delete('oauth2_server_scope', $scope_ids);
  }

  // Delete variables
  variable_del('restws_oauth2_server_name');
}

/**
 * Enable the RESTful Web Services Field Collection module.
 */
function farm_api_update_7000(&$sandbox) {
  $module = 'restws_field_collection';
  if (!module_exists($module)) {
    module_enable(array(
      $module,
    ));
  }
}

/**
 * Enable the farmOS OAuth2 Server on existing sites.
 */
function farm_api_update_7001(&$sandbox) {

  // Install new dependencies.
  $modules = array(
    'oauth2_server',
    'restws_oauth2_server',
  );
  foreach ($modules as $module) {
    if (!module_exists($module)) {
      module_enable(array(
        $module,
      ));
    }
  }

  // Perform the tasks in farm_api_install() as of the time of this writing.
  $permissions = array(
    'use oauth2 server',
  );
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, $permissions);
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions);
  $server_name = 'farmos_oauth';
  variable_set('restws_oauth2_server_name', $server_name);
}

/**
 * Enable default farmOS OAuth client.
 */
function farm_api_update_7002(&$sandbox) {
  $label = 'farmOS (Default)';
  $client_key = 'farm';
  $client_secret = '';
  $redirect_uri = '';
  $settings = array(
    'override_grant_types' => TRUE,
    'allow_implicit' => FALSE,
    'grant_types' => array(
      'password' => 'password',
      'refresh_token' => 'refresh_token',
    ),
    'always_issue_new_refresh_token' => TRUE,
    'unset_refresh_token_after_use' => TRUE,
  );
  farm_api_enable_oauth_client($label, $client_key, $client_secret, $redirect_uri, $settings);
}

Functions

Namesort descending Description
farm_api_install Implements hook_install().
farm_api_uninstall Implements hook_uninstall().
farm_api_update_7000 Enable the RESTful Web Services Field Collection module.
farm_api_update_7001 Enable the farmOS OAuth2 Server on existing sites.
farm_api_update_7002 Enable default farmOS OAuth client.