You are here

restful_token_auth.install in RESTful 7

Same filename and directory in other branches
  1. 7.2 modules/restful_token_auth/restful_token_auth.install

Install, update, and uninstall functions for the RESTful token authentication module.

File

modules/restful_token_auth/restful_token_auth.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the RESTful token authentication
 * module.
 */

/**
 * Implements hook_schema().
 */
function restful_token_auth_schema() {
  $schema = array();
  $schema['restful_token_auth'] = array(
    'description' => 'The authentication token table.',
    'fields' => array(
      'id' => array(
        'description' => 'The authentication token unique ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The authentication token type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The user the authentication token belongs to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'description' => 'The authentication token name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'token' => array(
        'description' => 'The authentication token security token.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the authentication token was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'expire' => array(
        'description' => 'The Unix timestamp when the authentication token will expire.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => NULL,
      ),
    ),
    'foreign keys' => array(
      'uid' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );

  // Cache bins for Entity-cache module.
  $cache_schema = drupal_get_schema_unprocessed('system', 'cache');
  $types = array(
    'restful_token_auth',
  );
  foreach ($types as $type) {
    $schema["cache_entity_{$type}"] = $cache_schema;
    $schema["cache_entity_{$type}"]['description'] = "Cache table used to store {$type} entity records.";
  }
  return $schema;
}

/**
 * Implements hook_install().
 */
function restful_token_auth_install() {
  restful_token_auth_create_field_refresh_token();
}

/**
 * Implements hook_uninstall().
 */
function restful_token_auth_uninstall() {
  variable_del('restful_token_auth_delete_expired_tokens');
}

/**
 * Adds the refresh token entity reference.
 */
function restful_token_auth_update_7100(&$sandbox) {

  // Change the type column for existing tokens.
  db_update('restful_token_auth')
    ->fields(array(
    'type' => 'access_token',
  ))
    ->execute();

  // Attach the new field.
  restful_token_auth_create_field_refresh_token();
}

/**
 * Helper function to create the refresh token entity reference.
 */
function restful_token_auth_create_field_refresh_token() {

  // Add an entity reference field to the access_token bundle to link to the
  // corresponding refresh token.
  $field_name = 'refresh_token_reference';
  $field = array(
    'entity_types' => array(
      'restful_token_auth',
    ),
    'settings' => array(
      'handler' => 'base',
      'target_type' => 'restful_token_auth',
      'handler_settings' => array(
        'target_bundles' => array(
          'refresh_token' => 'refresh_token',
        ),
      ),
    ),
    'field_name' => $field_name,
    'type' => 'entityreference',
    'cardinality' => 1,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => $field_name,
    'bundle' => 'access_token',
    'entity_type' => 'restful_token_auth',
    'label' => t('Refresh token'),
    'description' => t('Token used to get a new access token once it is expired.'),
    'required' => FALSE,
  );
  field_create_instance($instance);
}

Functions

Namesort descending Description
restful_token_auth_create_field_refresh_token Helper function to create the refresh token entity reference.
restful_token_auth_install Implements hook_install().
restful_token_auth_schema Implements hook_schema().
restful_token_auth_uninstall Implements hook_uninstall().
restful_token_auth_update_7100 Adds the refresh token entity reference.