-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow authenticated users to generate tokens via the UI #1016
base: 7.x-2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Page callbacks for restful_token_auth module. | ||
*/ | ||
|
||
/** | ||
* Generates and shows an API key. | ||
* | ||
* @param \stdClass $account | ||
* A user account object. | ||
* | ||
* @return array | ||
* A render array. | ||
*/ | ||
function restful_token_auth_get_credentials($account) { | ||
$query = new EntityFieldQuery(); | ||
$result = $query | ||
->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('<p>You don\'t have an API key yet. Click below to generate one.</p>'), | ||
); | ||
$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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? |
||
} | ||
|
||
/** | ||
* 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'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Default theme implementation to present a user's API token plus an | ||
* example with next steps. | ||
* | ||
* Available variables: | ||
* - $token: The API token. | ||
* | ||
* @ingroup themeable | ||
*/ | ||
?> | ||
<p>Your authentication token is: <code><?php print $token; ?></code></p> | ||
|
||
<p>Discover the API by running the following command in a terminal and replacing | ||
[your-token] by the above string:</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see |
||
|
||
<p> | ||
<code> | ||
curl -H 'access-token:<?php print $token; ?>' <?php print $GLOBALS['base_url'] ?>/api?all=true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better to have a preprocess function that uses |
||
</code> | ||
</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid markup in the translation string. Maybe consider
#prefix
and#suffix
for this.