You are here

services_token_access.module in Services Token Access 7

Module file for services_token_access module.

File

services_token_access.module
View source
<?php

/**
 * @file
 * Module file for services_token_access module.
 */

/**
 * Implements hook_services_authentication().
 */
function services_token_access_services_authentication_info() {
  return array(
    'file' => 'services_token_access.inc',
    'title' => t('Token access'),
    'description' => t('Access token URL parameter is specified'),
    'authenticate_call' => '_services_token_access_authenticate_call',
    'security_settings' => '_services_token_access_security_settings',
  );
}

/**
 * Implements hook_menu().
 */
function services_token_access_menu() {
  return array(
    'user/%user/services_token' => array(
      'title' => 'Services token',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'services_token_access_user_form',
        1,
      ),
      'access callback' => 'services_token_access_access',
      'access arguments' => array(
        1,
      ),
      'type' => MENU_LOCAL_TASK,
      'file' => 'services_token_access.inc',
    ),
  );
}

/**
 * Implements hook_admin_paths().
 */
function services_token_access_admin_paths() {
  return array(
    'user/*/services_token' => TRUE,
  );
}

/**
 * Implements hook_permission().
 */
function services_token_access_permission() {
  return array(
    'services use token authentication' => array(
      'title' => t('Use token authentication'),
      'description' => t('Allow the user to access services authenticated by the user token.'),
    ),
    'services manage own authentication token' => array(
      'title' => t('Manage own authentication token'),
      'description' => t('Allow the user to see and change its own authentication token.'),
    ),
  );
}

/**
 * Implements hook_user_load().
 */
function services_token_access_user_load($users) {
  $result = db_select('services_token_access_tokens', 's')
    ->fields('s', array(
    'uid',
    'token',
  ))
    ->condition('uid', array_keys($users), 'IN')
    ->execute();
  while ($row = $result
    ->fetchObject()) {
    $users[$row->uid]->services_token = $row->token;
  }
}

/**
 * Access callback for the token management page.
 *
 * @global object $user
 *   The current user
 *
 * @param object $account
 *   The user being managed
 *
 * @return bool
 *   TRUE if access, FALSE otherwise
 */
function services_token_access_access($account) {
  if (user_access('administer users')) {
    return TRUE;
  }
  else {
    global $user;
    return $user->uid == $account->uid && user_access('services manage own authentication token');
  }
}

/**
 * Get the token data based on user ID.
 *
 * @param int $uid
 *   User ID
 *
 * @param bool $raw_value
 *   If set to TRUE, the raw token string will be returned
 *
 * @return mixed
 *   Array with token data, or raw token string, FALSE if not found.
 */
function services_token_access_load_by_user($uid, $raw_value = FALSE) {
  $result = db_select('services_token_access_tokens', 's')
    ->fields('s')
    ->condition('uid', $uid)
    ->execute()
    ->fetchAssoc();
  if ($result) {
    return $raw_value ? $result['token'] : $result;
  }
  return FALSE;
}

/**
 * Get the token data based on token.
 *
 * @param string $token
 *   Token string
 *
 * @param bool $raw_value
 *   If set to TRUE, the raw user ID will be returned
 *
 * @return mixed
 *   Array with token data, or raw user ID, FALSE if not found.
 */
function services_token_access_load_by_token($token, $raw_value = FALSE) {
  $result = db_select('services_token_access_tokens', 's')
    ->fields('s')
    ->condition('token', $token)
    ->execute()
    ->fetchAssoc();
  if ($result) {
    return $raw_value ? $result['uid'] : $result;
  }
  return FALSE;
}

/**
 * Update the token string for a given user (or remove it).
 *
 * @param int $uid
 *   User ID
 *
 * @param bool $clear
 *   If set to TRUE, the token will be removed instead of updated.
 */
function services_token_access_update_token($uid, $clear = FALSE) {

  // Delete the row.
  if ($clear) {
    db_delete('services_token_access_tokens')
      ->condition('uid', $uid)
      ->execute();
    module_invoke_all('services_token_access_delete', $uid);
  }
  else {

    // Generate a token until we are sure that it is unique.
    do {
      $token = services_token_access_generate_value($uid);
    } while (services_token_access_load_by_token($token));
    $query = db_merge('services_token_access_tokens')
      ->key(array(
      'uid' => $uid,
    ))
      ->fields(array(
      'uid' => $uid,
      'token' => $token,
      'updated' => time(),
    ))
      ->execute();
    module_invoke_all('services_token_access_update', $uid, $token);
  }
}

/**
 * Generates a pseudo-unique token string based on user data.
 */
function services_token_access_generate_value($uid) {
  $account = user_load($uid);
  $string = $account->name . $account->mail . microtime(TRUE);
  return sha1($string);
}

/**
 * Removes all tokens from the database.
 */
function services_token_access_remove_all() {
  db_truncate('services_token_access_tokens')
    ->execute();
  module_invoke_all('services_token_access_truncate');
}

Functions

Namesort descending Description
services_token_access_access Access callback for the token management page.
services_token_access_admin_paths Implements hook_admin_paths().
services_token_access_generate_value Generates a pseudo-unique token string based on user data.
services_token_access_load_by_token Get the token data based on token.
services_token_access_load_by_user Get the token data based on user ID.
services_token_access_menu Implements hook_menu().
services_token_access_permission Implements hook_permission().
services_token_access_remove_all Removes all tokens from the database.
services_token_access_services_authentication_info Implements hook_services_authentication().
services_token_access_update_token Update the token string for a given user (or remove it).
services_token_access_user_load Implements hook_user_load().