openid.pages.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. );
  53. $build['openid_user_add'] = drupal_get_form('openid_user_add');
  54. return $build;
  55. }
  56. /**
  57. * Form builder; Add an OpenID identity.
  58. *
  59. * @ingroup forms
  60. * @see openid_user_add_validate()
  61. */
  62. function openid_user_add() {
  63. $form['openid_identifier'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('OpenID'),
  66. );
  67. $form['actions'] = array('#type' => 'actions');
  68. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add an OpenID'));
  69. return $form;
  70. }
  71. function openid_user_add_validate($form, &$form_state) {
  72. // Check for existing entries.
  73. $claimed_id = openid_normalize($form_state['values']['openid_identifier']);
  74. if (db_query("SELECT authname FROM {authmap} WHERE authname = :authname", (array(':authname' => $claimed_id)))->fetchField()) {
  75. form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
  76. }
  77. }
  78. function openid_user_add_submit($form, &$form_state) {
  79. $return_to = url('user/' . arg(1) . '/openid', array('absolute' => TRUE));
  80. openid_begin($form_state['values']['openid_identifier'], $return_to);
  81. }
  82. /**
  83. * Menu callback; Delete the specified OpenID identity from the system.
  84. */
  85. function openid_user_delete_form($form, $form_state, $account, $aid = 0) {
  86. $authname = db_query("SELECT authname FROM {authmap} WHERE uid = :uid AND aid = :aid AND module = 'openid'", array(
  87. ':uid' => $account->uid,
  88. ':aid' => $aid,
  89. ))
  90. ->fetchField();
  91. 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');
  92. }
  93. function openid_user_delete_form_submit($form, &$form_state) {
  94. $query = db_delete('authmap')
  95. ->condition('uid', $form_state['build_info']['args'][0]->uid)
  96. ->condition('aid', $form_state['build_info']['args'][1])
  97. ->condition('module', 'openid')
  98. ->execute();
  99. if ($query) {
  100. drupal_set_message(t('OpenID deleted.'));
  101. }
  102. $form_state['redirect'] = 'user/' . $form_state['build_info']['args'][0]->uid . '/openid';
  103. }