diff --git a/modules/restful_token_auth/restful_token_auth.module b/modules/restful_token_auth/restful_token_auth.module index 1b24e28b..0870fd43 100644 --- a/modules/restful_token_auth/restful_token_auth.module +++ b/modules/restful_token_auth/restful_token_auth.module @@ -24,6 +24,17 @@ function restful_token_auth_menu() { 'access arguments' => array('administer restful'), 'file' => 'restful_token_auth.admin.inc', ); + // Add an API tab to the user profile. + $items['user/%user/api'] = array( + 'title' => 'API', + 'description' => 'API details', + 'page callback' => 'restful_token_auth_get_credentials', + 'access callback' => 'user_view_access', + 'access arguments' => array(1), + 'page arguments' => array(1), + 'file' => 'restful_token_auth.pages.inc', + 'type' => MENU_LOCAL_TASK, + ); return $items; } @@ -147,3 +158,19 @@ function restful_token_auth_user_update(&$edit, $account, $category) { entity_delete_multiple('restful_token_auth', array_keys($result['restful_token_auth'])); } + +/** + * Implementation of hook_theme(). + */ +function restful_token_auth_theme() { + $module_path = drupal_get_path('module', 'restful_token_auth'); + return array( + 'restful_token_auth_user_key' => array( + 'variables' => array( + 'token' => NULL, + ), + 'template' => 'user-key', + 'path' => $module_path . '/templates', + ) + ); +} diff --git a/modules/restful_token_auth/restful_token_auth.pages.inc b/modules/restful_token_auth/restful_token_auth.pages.inc new file mode 100644 index 00000000..bf35536d --- /dev/null +++ b/modules/restful_token_auth/restful_token_auth.pages.inc @@ -0,0 +1,92 @@ +entityCondition('entity_type', 'restful_token_auth') + ->entityCondition('bundle', 'access_token') + ->propertyCondition('uid', $account->uid) + ->execute(); + + if (empty($result['restful_token_auth'])) { + return drupal_get_form('restful_token_auth_generate_token_form', $account->uid); + } + else { + $id = key($result['restful_token_auth']); + $auth_token = entity_load_single('restful_token_auth', $id); + return array( + '#theme' => 'restful_token_auth_user_key', + '#token' => $auth_token->token, + ); + } +} + +/** + * Form callback to generate an API token. + * + * @param array $form + * The form array. + * @param array $form_state + * The form state array. + * @param int $uid + * The user identifier. + * + * @return array + * The form as a render array. + */ +function restful_token_auth_generate_token_form($form, &$form_state, $uid) { + $form['description'] = array( + '#markup' => t('

You don\'t have an API key yet. Click below to generate one.

'), + ); + $form['uid'] = array( + '#type' => 'value', + '#value' => $uid, + ); + + $form['submit_button'] = array( + '#type' => 'submit', + '#value' => t('Generate token'), + ); + + return $form; +} + +/** + * Form validator for generating a token. + * + * @param array $form + * The form array. + * @param array $form_state + * The form state array. + */ +function restful_token_auth_generate_token_form_validate($form, &$form_state) { +} + +/** + * Form submit handler for generating a token. + * + * @param array $form + * The form array. + * @param array $form_state + * The form state array. + */ +function restful_token_auth_generate_token_form_submit($form, &$form_state) { + /** @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */ + $controller = entity_get_controller('restful_token_auth'); + $controller->generateAccessToken($form_state['values']['uid']); + drupal_set_message(t('Token generated successfully.'), 'status'); +} diff --git a/modules/restful_token_auth/templates/user-key.tpl.php b/modules/restful_token_auth/templates/user-key.tpl.php new file mode 100644 index 00000000..ad999a61 --- /dev/null +++ b/modules/restful_token_auth/templates/user-key.tpl.php @@ -0,0 +1,23 @@ + +

Your authentication token is:

+ +

Discover the API by running the following command in a terminal and replacing + [your-token] by the above string:

+ +

+ + curl -H 'access-token:' /api?all=true + +