openid.pages.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the openid module.
  5. */
  6. /**
  7. * Menu callback; Process an OpenID authentication.
  8. */
  9. function openid_authentication_page() {
  10. $result = openid_complete();
  11. switch ($result['status']) {
  12. case 'success':
  13. return openid_authentication($result);
  14. case 'failed':
  15. drupal_set_message(t('OpenID login failed.'), 'error');
  16. break;
  17. case 'cancel':
  18. drupal_set_message(t('OpenID login cancelled.'));
  19. break;
  20. }
  21. drupal_goto();
  22. }
  23. /**
  24. * Menu callback; Manage OpenID identities for the specified user.
  25. */
  26. function openid_user_identities($account) {
  27. drupal_set_title(format_username($account));
  28. drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css');
  29. // Check to see if we got a response
  30. $result = openid_complete();
  31. if ($result['status'] == 'success') {
  32. $identity = $result['openid.claimed_id'];
  33. $query = db_insert('authmap')
  34. ->fields(array(
  35. 'uid' => $account->uid,
  36. 'authname' => $identity,
  37. 'module' => 'openid',
  38. ))
  39. ->execute();
  40. drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
  41. }
  42. $header = array(t('OpenID'), t('Operations'));
  43. $rows = array();
  44. $result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=:uid", array(':uid' => $account->uid));
  45. foreach ($result as $identity) {
  46. $rows[] = array(check_plain($identity->authname), l(t('Delete'), 'user/' . $account->uid . '/openid/delete/' . $identity->aid));
  47. }
  48. $build['openid_table'] = array(
  49. '#theme' => 'table',
  50. '#header' => $header,
  51. '#rows' => $rows,
  52. '#empty' => t('No OpenID identities available for this account.'),
  53. );
  54. $build['openid_user_add'] = drupal_get_form('openid_user_add');
  55. return $build;
  56. }
  57. /**
  58. * Form builder; Add an OpenID identity.
  59. *
  60. * @ingroup forms
  61. * @see openid_user_add_validate()
  62. */
  63. function openid_user_add() {
  64. $form['openid_identifier'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('OpenID'),
  67. );
  68. $form['actions'] = array('#type' => 'actions');
  69. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add an OpenID'));
  70. return $form;
  71. }
  72. function openid_user_add_validate($form, &$form_state) {
  73. // Check for existing entries.
  74. $claimed_id = openid_normalize($form_state['values']['openid_identifier']);
  75. if (db_query("SELECT authname FROM {authmap} WHERE authname = :authname", (array(':authname' => $claimed_id)))->fetchField()) {
  76. form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
  77. }
  78. }
  79. function openid_user_add_submit($form, &$form_state) {
  80. $return_to = url('user/' . arg(1) . '/openid', array('absolute' => TRUE));
  81. openid_begin($form_state['values']['openid_identifier'], $return_to);
  82. }
  83. /**
  84. * Menu callback; Delete the specified OpenID identity from the system.
  85. */
  86. function openid_user_delete_form($form, $form_state, $account, $aid = 0) {
  87. $authname = db_query("SELECT authname FROM {authmap} WHERE uid = :uid AND aid = :aid AND module = 'openid'", array(
  88. ':uid' => $account->uid,
  89. ':aid' => $aid,
  90. ))
  91. ->fetchField();
  92. 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');
  93. }
  94. function openid_user_delete_form_submit($form, &$form_state) {
  95. $query = db_delete('authmap')
  96. ->condition('uid', $form_state['build_info']['args'][0]->uid)
  97. ->condition('aid', $form_state['build_info']['args'][1])
  98. ->condition('module', 'openid')
  99. ->execute();
  100. if ($query) {
  101. drupal_set_message(t('OpenID deleted.'));
  102. }
  103. $form_state['redirect'] = 'user/' . $form_state['build_info']['args'][0]->uid . '/openid';
  104. }