web/drupal/modules/openid/openid.pages.inc
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: openid.pages.inc,v 1.5.2.1 2008/07/09 21:48:28 goba Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * User page callbacks for the openid module.
       
     7  */
       
     8 
       
     9 /**
       
    10  * Menu callback; Process an OpenID authentication.
       
    11  */
       
    12 function openid_authentication_page() {
       
    13   $result = openid_complete();
       
    14   switch ($result['status']) {
       
    15     case 'success':
       
    16       return openid_authentication($result);
       
    17     case 'failed':
       
    18       drupal_set_message(t('OpenID login failed.'), 'error');
       
    19       break;
       
    20     case 'cancel':
       
    21       drupal_set_message(t('OpenID login cancelled.'));
       
    22       break;
       
    23   }
       
    24   drupal_goto();
       
    25 }
       
    26 
       
    27 /**
       
    28  * Menu callback; Manage OpenID identities for the specified user.
       
    29  */
       
    30 function openid_user_identities($account) {
       
    31   drupal_set_title(check_plain($account->name));
       
    32   drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module');
       
    33 
       
    34   // Check to see if we got a response
       
    35   $result = openid_complete();
       
    36   if ($result['status'] == 'success') {
       
    37     $identity = $result['openid.claimed_id'];
       
    38     db_query("INSERT INTO {authmap} (uid, authname, module) VALUES (%d, '%s','openid')", $account->uid, $identity);
       
    39     drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
       
    40   }
       
    41 
       
    42   $header = array(t('OpenID'), t('Operations'));
       
    43   $rows = array();
       
    44 
       
    45   $result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=%d", $account->uid);
       
    46   while ($identity = db_fetch_object($result)) {
       
    47     $rows[] = array(check_plain($identity->authname), l(t('Delete'), 'user/'. $account->uid .'/openid/delete/'. $identity->aid));
       
    48   }
       
    49 
       
    50   $output = theme('table', $header, $rows);
       
    51   $output .= drupal_get_form('openid_user_add');
       
    52   return $output;
       
    53 }
       
    54 
       
    55 /**
       
    56  * Form builder; Add an OpenID identity.
       
    57  *
       
    58  * @ingroup forms
       
    59  * @see openid_user_add_validate()
       
    60  */
       
    61 function openid_user_add() {
       
    62   $form['openid_identifier'] = array(
       
    63     '#type' => 'textfield',
       
    64     '#title' => t('OpenID'),
       
    65   );
       
    66   $form['submit'] = array('#type' => 'submit', '#value' => t('Add an OpenID'));
       
    67   return $form;
       
    68 }
       
    69 
       
    70 function openid_user_add_validate($form, &$form_state) {
       
    71   // Check for existing entries.
       
    72   $claimed_id = _openid_normalize($form_state['values']['openid_identifier']);
       
    73   if (db_result(db_query("SELECT authname FROM {authmap} WHERE authname='%s'", $claimed_id))) {
       
    74     form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
       
    75   }
       
    76   else {
       
    77     $return_to = url('user/'. arg(1) .'/openid', array('absolute' => TRUE));
       
    78     openid_begin($form_state['values']['openid_identifier'], $return_to);
       
    79   }
       
    80 }
       
    81 
       
    82 /**
       
    83  * Present a confirmation form to delete the specified OpenID identity from the system.
       
    84  *
       
    85  * @ingroup forms
       
    86  * @see openid_user_delete_form_submit()
       
    87  */
       
    88 function openid_user_delete_form($form_state, $account, $aid = 0) {
       
    89   $authname = db_result(db_query('SELECT authname FROM {authmap} WHERE uid = %d AND aid = %d', $account->uid, $aid));
       
    90 
       
    91   $form = array();
       
    92 
       
    93   $form['uid'] = array(
       
    94     '#type' => 'value',
       
    95     '#value' => $account->uid,
       
    96   );
       
    97 
       
    98   $form['aid'] = array(
       
    99     '#type' => 'value',
       
   100     '#value' => $aid,
       
   101   );
       
   102 
       
   103   return confirm_form($form, t('Are you sure you want to delete the OpenID %authname for %user?', array('%authname' => $authname, '%user' => $account->name)), 'user/'. $account->uid .'/openid');
       
   104 }
       
   105 
       
   106 function openid_user_delete_form_submit($form, &$form_state) {
       
   107   db_query("DELETE FROM {authmap} WHERE uid = %d AND aid = %d AND module = 'openid'", $form_state['values']['uid'], $form_state['values']['aid']);
       
   108   if (db_affected_rows()) {
       
   109     drupal_set_message(t('OpenID deleted.'));
       
   110   }
       
   111   $form_state['redirect'] = 'user/'. $form_state['values']['uid'] .'/openid';
       
   112 }