openid.pages.inc 4.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * User page callbacks for the openid module.
 */

/**
 * Menu callback; Process an OpenID authentication.
 */
function openid_authentication_page() {
12
  $result = openid_complete();
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  switch ($result['status']) {
    case 'success':
      return openid_authentication($result);
    case 'failed':
      drupal_set_message(t('OpenID login failed.'), 'error');
      break;
    case 'cancel':
      drupal_set_message(t('OpenID login cancelled.'));
      break;
  }
  drupal_goto();
}

/**
 * Menu callback; Manage OpenID identities for the specified user.
 */
function openid_user_identities($account) {
30
  drupal_set_title(user_format_name($account));
31
  drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css');
32 33

  // Check to see if we got a response
34 35 36
  $response = openid_complete();
  if ($response['status'] == 'success') {
    $identity = $response['openid.claimed_id'];
37 38 39 40 41 42 43
    $query = db_insert('authmap')
      ->fields(array(
        'uid' => $account->uid,
        'authname' => $identity,
        'module' => 'openid',
      ))
      ->execute();
44
    drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
45 46
    // Let other modules act on OpenID authentication.
    module_invoke_all('openid_response', $response, $account);
47 48 49 50 51
  }

  $header = array(t('OpenID'), t('Operations'));
  $rows = array();

52 53
  $result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=:uid", array(':uid' => $account->uid));
  foreach ($result as $identity) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    $row = array();
    $row[] = check_plain($identity->authname);
    $links = array();
    $links['delete'] = array(
      'title' => t('Delete'),
      'href' => 'user/' . $account->uid . '/openid/delete/' . $identity->aid,
    );
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );
    $rows[] = $row;
68 69
  }

70
  $build['openid_table'] = array(
71 72
    '#theme' => 'table',
    '#header' => $header,
73
    '#rows' => $rows,
74
    '#empty' => t('No OpenID identities available for this account.'),
75
  );
76 77
  $build['openid_user_add'] = drupal_get_form('openid_user_add');
  return $build;
78 79 80 81 82 83
}

/**
 * Form builder; Add an OpenID identity.
 *
 * @ingroup forms
84
 * @see openid_user_add_validate()
85 86
 */
function openid_user_add() {
87
  $form['openid_identifier'] = array(
88 89 90
    '#type' => 'textfield',
    '#title' => t('OpenID'),
  );
91
  $form['actions'] = array('#type' => 'actions');
92
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add an OpenID'));
93 94 95 96 97
  return $form;
}

function openid_user_add_validate($form, &$form_state) {
  // Check for existing entries.
98
  $claimed_id = openid_normalize($form_state['values']['openid_identifier']);
99
  if (db_query("SELECT authname FROM {authmap} WHERE authname = :authname", (array(':authname' => $claimed_id)))->fetchField()) {
100
    form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
101
  }
webchick's avatar
webchick committed
102 103 104 105 106
}

function openid_user_add_submit($form, &$form_state) {
  $return_to = url('user/' . arg(1) . '/openid', array('absolute' => TRUE));
  openid_begin($form_state['values']['openid_identifier'], $return_to);
107 108 109 110 111
}

/**
 * Menu callback; Delete the specified OpenID identity from the system.
 */
112
function openid_user_delete_form($form, $form_state, $account, $aid = 0) {
113
  $authname = db_query("SELECT authname FROM {authmap} WHERE uid = :uid AND aid = :aid AND module = 'openid'", array(
114 115 116 117
    ':uid' => $account->uid,
    ':aid' => $aid,
  ))
  ->fetchField();
118
  return confirm_form(array(), t('Are you sure you want to delete the OpenID %authname for %user?', array('%authname' => $authname, '%user' => $account->name)), 'user/' . $account->uid . '/openid');
119 120
}

121
function openid_user_delete_form_submit($form, &$form_state) {
122
  $query = db_delete('authmap')
123 124
    ->condition('uid', $form_state['build_info']['args'][0]->uid)
    ->condition('aid', $form_state['build_info']['args'][1])
125
    ->condition('module', 'openid')
126 127
    ->execute();
  if ($query) {
128 129
    drupal_set_message(t('OpenID deleted.'));
  }
130
  $form_state['redirect'] = 'user/' . $form_state['build_info']['args'][0]->uid . '/openid';
131
}