Download & Extend

User Access Keys API

User Access Keys API provides an API for modules that want to allow users to store access keys for third party services in their Drupal user profiles, as profile.module fields.

Specifically; Validation of user-input access keys via form-alters to profile.module forms and functions for *.install files to manage profile.module fields.

Usage guide

An implementation of the User Access Keys API is quite simple. An example implementation ("Example.com API User Access Keys") is included in the package and also demonstrated below.

Full implementations are included in the Toggl.com API and Redmine API packages.

Example Implementation

The implementing module implements two functions to allow User Access Keys API to manage the profile.module field and validation of the access key.

<?php
/**
* @file example.module
* An EXAMPLE implementation of the User Access Keys API.
*/

/**
* An implementation of hook_user_access_keys()
*/
function example_user_access_keys() {
  return (Object) array(
   
// The machine name of the profile.module field.
   
'field' => 'example_api_access_key',
   
// The human-readble name of the profile.module field.
   
'name' => t('Example.com API access key'),
   
// The callback function to validate an access key.
   
'validate' => 'example_validate',
   
// The URL of the third-party website where a user can find their API key.
   
'source' => 'https://example.com/my/account',
   
// Further instructions for the user to find their API key at that URL.
 
);
}

/**
* An access key validation callback.
*
* @param $access_key String
*    The user-input access key to be validated.
* @param $form Array
*    The Drupal Form API form from which the $access_key was sourced.
* @param $field String
*    The user_access_keys profile.module field, which is needed to access
*    $access_key in $form.
*    @see user_access_keys_category()
* @return Boolean
*    TRUE if $access_key is valid.
*/
function example_validate($access_key, $form, $field) {
 
// @todo Validate $access_key

  // @todo Consider implementing REST API Query module.
  // @see drupal.org/project/rest_api_query

  // Return TRUE if $access_key is valid, or FALSE if it is invalid.
 
return TRUE || FALSE;
}
?>

A few tweaks or additions to an implementing-module's *.install file will allow the User Access Keys API to create, check and uninstall a profile.module field.

<?php
/**
* @file example_user_access_keys.install
* Manages the "Example.com API access key" profile.module field.
*
* An implementation of the User Access Keys API.
*/

// Include the API functions.
user_access_keys_include_install();

/**
* Implementation of hook_requirements().
*/
function example_requirements($phase) {
 
$reqs = array();
 
// Check the profile.module field has not been removed or renamed.
 
user_access_keys_field_check_requirements($phase, $reqs, 'example_api_access_key');
  return
$reqs;
}

/**
* Implementation of hook_enable().
*/
function example_enable() {
 
// Create the profile.module field if it does not already exist.
 
user_access_keys_field_enable('example_api_access_key');
}

/**
* Implementation of hook_uninstall().
*/
function example_uninstall() {
 
// Delete the profile.module field and remove all it's data.
 
user_access_keys_field_uninstall('example_api_access_key');
}
?>

Downloads

Recommended releases

Version Downloads Date Links
6.x-1.0 tar.gz (11.83 KB) | zip (14.65 KB) 2011-Nov-30 Notes

Development releases

Version Downloads Date Links
6.x-1.x-dev tar.gz (11.83 KB) | zip (14.65 KB) 2011-Nov-30 Notes

Project Information


Maintainers for User Access Keys API

  • Bevan - 13 commits
    last: 25 weeks ago, first: 49 weeks ago

Issues for User Access Keys API

To avoid duplicates, please search before submitting a new issue.
All issues
Bug reports
nobody click here