Skip to content

Commit

Permalink
Complete editing functionality for access tokens implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
hugowetterberg committed Apr 1, 2009
1 parent 5663270 commit 83591ee
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 11 deletions.
4 changes: 4 additions & 0 deletions services_oauth.inc
Expand Up @@ -24,6 +24,10 @@ function _services_oauth_authenticate_call($method, $args) {
throw new OAuthException('Missing access token');
}

if (!$token->authorized) {
throw new OAuthException('The access token is not authorized');
}

if (!in_array('*', $token->services) && !in_array($method['#authorization level'], $token->services)) {
throw new OAuthException('The consumer is not authorized to access this service');
}
Expand Down
10 changes: 10 additions & 0 deletions services_oauth.module
Expand Up @@ -81,6 +81,16 @@ function services_oauth_menu() {
'type' => MENU_CALLBACK,
);

$menu['user/%user/applications/authorization/%/delete'] = array(
'title' => 'Edit authorization',
'page callback' => 'drupal_get_form',
'page arguments' => array('_services_oauth_user_authorization_delete', 1, 4),
'access callback' => 'oauth_services_user_access',
'access arguments' => array(1),
'file' => 'services_oauth.pages.inc',
'type' => MENU_CALLBACK,
);

$menu['admin/build/services/authentication'] = array(
'title' => 'Authentication',
'page callback' => 'drupal_get_form',
Expand Down
136 changes: 125 additions & 11 deletions services_oauth.pages.inc
Expand Up @@ -334,10 +334,20 @@ function _services_oauth_user_authorization_edit($form_state, $user, $key) {

drupal_set_title(t('Authorization for !app', array('!app' => $consumer->name)));

$form['user'] = array(
'#type' => 'value',
'#value' => $user->uid,
);

$form['key'] = array(
'#type' => 'value',
'#value' => $token->key,
);

$form['authorized'] = array(
'#type' => 'checkbox',
'#title' => t('Authorized'),
'#value' => $token->authorized,
'#default_value' => $token->authorized,
);

$form['created'] = array(
Expand All @@ -352,31 +362,135 @@ function _services_oauth_user_authorization_edit($form_state, $user, $key) {
'#value' => format_date($token->changed),
);

$form['key'] = array(
$form['key_item'] = array(
'#type' => 'item',
'#title' => t('Key'),
'#value' => $token->key,
);

$auth_txt = array();
foreach ($token->services as $service) {
if ($service == '*') {
$auth_txt[] = t('Full access');
}
}

$form['allowed'] = array(
'#type' => 'fieldset',
'#title' => t('Permissions'),
);

services_oauth_permissions_form($form['allowed'], $token->services);
services_oauth_permissions_form($form['allowed'], $consumer, $token->services);

$form['delete'] = array(
'#type' => 'item',
'#value' => l(t('Delete'), sprintf('user/%d/applications/authorization/%s/delete', $user->uid, $token->key)),
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);

return $form;
}

function _services_oauth_user_authorization_edit_submit($form, $form_state) {
$values = $form_state['values'];

// Collect the authorization levels
$services = array();
foreach ($values['levels'] as $level => $allowed) {
if ($allowed) {
$services[] = $level;
}
}

$token = DrupalOAuthToken::load($values['key']);
$consumer = DrupalOAuthConsumer::load($token->consumer_key);

$token->services = $services;
$token->authorized = $values['authorized'];
$token->write(TRUE);

drupal_set_message(t('The !consumer token !token was updated.', array(
'!consumer' => $consumer->name,
'!token' => $token->key)));
drupal_goto(sprintf('user/%d/applications', $form_state['values']['user']));
}

function _services_oauth_user_authorization_delete($form_state, $user, $key) {
$token = DrupalOAuthToken::load($key);
$consumer = DrupalOAuthConsumer::load($token->consumer_key);

drupal_set_title(t('Deleting authorization for "!title"', array(
'!title' => $consumer->name,
)));

$form = array(
'authorization' => array(
'#type' => 'value',
'#value' => $authorization,
),
);

$form['user'] = array(
'#type' => 'value',
'#value' => $user->uid,
);

$form['key'] = array(
'#type' => 'value',
'#value' => $token->key,
);

$form['description'] = array(
'#type' => 'item',
'#value' => t('Are you sure that you want to delete the authorization for !name?', array(
'!name' => $consumer->name,
)),
);

$form['cancel'] = array(
'#type' => 'item',
'#value' => l(t('Cancel'), sprintf('user/%d/applications/authorization/%s', $user->uid, $token->key)),
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);

return $form;
}

function services_oauth_permissions_form(&$form, $default_services=array('*')) {
function _services_oauth_user_authorization_delete_submit($form, $form_state) {
$token = DrupalOAuthToken::load($form_state['values']['key']);
$consumer = DrupalOAuthConsumer::load($token->consumer_key);
$token->delete();
drupal_set_message(t('The !consumer token !token was deleted.', array(
'!consumer' => $consumer->name,
'!token' => $token->key)));
drupal_goto(sprintf('user/%d/applications', $form_state['values']['user']));
}

function services_oauth_permissions_form(&$form, $consumer, $default_services=array('*')) {
$msgargs = array(
'!appname' => $consumer->name,
'!sitename' => variable_get('site_name', ''),
);

$auth_levels = array_merge(
array('*' => (object)array(
'title' => t('Full access'),
'description' => t('This will give !appname the same permissions that you normally have and will allow it to access the full range of services that !sitename provides.', $msgargs),
)), services_oauth_authorization_levels());
$form['authorization']['levels'] = array(
'#tree' => TRUE,
);

foreach ($auth_levels as $name => $level) {
$auth_opt = array(
'#type' => 'checkbox',
'#title' => t($level->title, $msgargs),
'#description' => t($level->description, $msgargs),
'#default_value' => in_array($name, $default_services),
);
$form['authorization']['levels'][$name] = $auth_opt;
}
}

function _services_oauth_user_applications_add($form_state, $account) {
Expand Down

0 comments on commit 83591ee

Please sign in to comment.