2007-08-19 09:48:33 +00:00
< ? php
// $Id$
/**
* @ file
* User page callbacks for the openid module .
*/
/**
* Menu callback ; Process an OpenID authentication .
*/
function openid_authentication_page () {
2008-01-30 22:11:22 +00:00
$result = openid_complete ();
2007-08-19 09:48:33 +00:00
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 ) {
2008-10-13 00:33:05 +00:00
drupal_set_title ( $account -> name );
2008-10-26 18:06:39 +00:00
drupal_add_css ( drupal_get_path ( 'module' , 'openid' ) . '/openid.css' );
2007-08-19 09:48:33 +00:00
// Check to see if we got a response
2008-01-30 22:11:22 +00:00
$result = openid_complete ();
2007-08-19 09:48:33 +00:00
if ( $result [ 'status' ] == 'success' ) {
2008-01-30 22:11:22 +00:00
$identity = $result [ 'openid.claimed_id' ];
2009-03-17 14:02:49 +00:00
$query = db_insert ( 'authmap' )
-> fields ( array (
'uid' => $account -> uid ,
'authname' => $identity ,
'module' => 'openid' ,
))
-> execute ();
2008-01-30 22:11:22 +00:00
drupal_set_message ( t ( 'Successfully added %identity' , array ( '%identity' => $identity )));
2007-08-19 09:48:33 +00:00
}
$header = array ( t ( 'OpenID' ), t ( 'Operations' ));
$rows = array ();
2009-03-17 14:02:49 +00:00
$result = db_query ( " SELECT * FROM { authmap} WHERE module='openid' AND uid=:uid " , array ( ':uid' => $account -> uid ));
foreach ( $result as $identity ) {
2008-11-22 10:32:42 +00:00
$rows [] = array ( check_plain ( $identity -> authname ), l ( t ( 'Delete' ), 'user/' . $account -> uid . '/openid/delete/' . $identity -> aid ));
2007-08-19 09:48:33 +00:00
}
$output = theme ( 'table' , $header , $rows );
2009-05-12 08:37:45 +00:00
$output .= drupal_render ( drupal_get_form ( 'openid_user_add' ));
2007-08-19 09:48:33 +00:00
return $output ;
}
/**
* Form builder ; Add an OpenID identity .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see openid_user_add_validate ()
2007-08-19 09:48:33 +00:00
*/
function openid_user_add () {
2008-01-30 22:11:22 +00:00
$form [ 'openid_identifier' ] = array (
2007-08-19 09:48:33 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'OpenID' ),
);
$form [ 'submit' ] = array ( '#type' => 'submit' , '#value' => t ( 'Add an OpenID' ));
return $form ;
}
function openid_user_add_validate ( $form , & $form_state ) {
// Check for existing entries.
2008-01-30 22:11:22 +00:00
$claimed_id = _openid_normalize ( $form_state [ 'values' ][ 'openid_identifier' ]);
2009-03-17 14:02:49 +00:00
if ( db_query ( " SELECT authname FROM { authmap} WHERE authname = :authname " , ( array ( ':authname' => $claimed_id ))) -> fetchField ()) {
2008-01-30 22:11:22 +00:00
form_set_error ( 'openid_identifier' , t ( 'That OpenID is already in use on this site.' ));
2007-08-19 09:48:33 +00:00
}
else {
2008-04-14 17:48:46 +00:00
$return_to = url ( 'user/' . arg ( 1 ) . '/openid' , array ( 'absolute' => TRUE ));
2008-01-30 22:11:22 +00:00
openid_begin ( $form_state [ 'values' ][ 'openid_identifier' ], $return_to );
2007-08-19 09:48:33 +00:00
}
}
/**
* Menu callback ; Delete the specified OpenID identity from the system .
*/
2008-11-22 10:32:42 +00:00
function openid_user_delete_form ( $form_state , $account , $aid = 0 ) {
2009-03-18 09:49:31 +00:00
$authname = db_query ( " SELECT authname FROM { authmap} WHERE uid = :uid AND aid = :aid AND module = 'openid' " , array (
2009-03-17 14:02:49 +00:00
':uid' => $account -> uid ,
':aid' => $aid ,
))
-> fetchField ();
2008-11-22 10:32:42 +00:00
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' );
}
function openid_user_delete_form_submit ( & $form_state , $form_values ) {
2009-03-17 14:02:49 +00:00
$query = db_delete ( 'authmap' )
2009-03-18 09:49:31 +00:00
-> condition ( 'uid' , $form_state [ '#args' ][ 0 ] -> uid )
-> condition ( 'aid' , $form_state [ '#args' ][ 1 ])
-> condition ( 'module' , 'openid' )
2009-03-17 14:02:49 +00:00
-> execute ();
if ( $query ) {
2007-08-19 09:48:33 +00:00
drupal_set_message ( t ( 'OpenID deleted.' ));
}
2009-03-14 20:13:27 +00:00
$form_state [ '#redirect' ] = 'user/' . $form_state [ '#args' ][ 0 ] -> uid . '/openid' ;
2007-08-19 09:48:33 +00:00
}