'value', '#value' => $account); $form['list'] = array( '#weight' => -1, '#type' => 'textarea', '#title' => t('List of OpenIDs'), '#required' => TRUE, '#description' => t('Add a list of OpenIDs for this account, one per line.'), '#element_validate' => array('openidadmin_list_form_validate'), ); $form['submit']['#value'] = t('Add'); $form['submit']['#submit'] = array('openidadmin_list_form_submit'); } } /** * Form callback. List validation */ function openidadmin_list_form_validate($element, &$form_state) { foreach (openidadmin_normalize_list($element['#value']) as $claimed_id) { // Check for existing entries. if ($uid = db_result(db_query("SELECT uid FROM {authmap} WHERE authname='%s'", $claimed_id))) { $account = user_load(array('uid' => $uid)); form_set_error('list', t('The OpenID %identity is already in use on this site for the user !name.', array('%identity' => $claimed_id, '!name' => theme('username', $account)))); } } } /** * Form callback. List submission */ function openidadmin_list_form_submit($form, &$form_state) { $account = $form_state['values']['account']; $add_list = openidadmin_normalize_list($form_state['values']['list']); foreach ($add_list as $claimed_id) { db_query("INSERT INTO {authmap}(uid, authname, module) VALUES(%d, '%s', 'openid')", $account->uid, $claimed_id); } drupal_set_message(format_plural(count($add_list), 'One OpenID has been added.', '@count OpenIDs have been added.')); return "user/$account->uid/openid"; } /** * Helper function. Convert text area into normalized list removing duplicates */ function openidadmin_normalize_list($text) { module_load_include('inc', 'openid'); $list = explode("\n", $text); $normalized = array(); foreach ($list as $identity) { if ($identity = trim($identity)) { $claimed_id = _openid_normalize($identity); // This will take care of duplicates $normalized[$claimed_id] = $claimed_id; } } return $normalized; }