diff --git a/sites/all/modules/unl/unl_cas/unl_cas.admin.inc b/sites/all/modules/unl/unl_cas/unl_cas.admin.inc index 80a5abdcc3c5e2e8cc4c07cc625746ab5c1f402f..fa227163556837e526c017cc0717023e87240c04 100644 --- a/sites/all/modules/unl/unl_cas/unl_cas.admin.inc +++ b/sites/all/modules/unl/unl_cas/unl_cas.admin.inc @@ -1,78 +1,95 @@ <?php function unl_cas_user_import($form, &$form_state) { - $form['root']['#type'] = 'fieldset'; - $form['root']['#title'] = 'Import from peoplefinder.'; - $form['account']['name'] = array(); - $form['account']['name']['#type'] = 'textfield'; - $form['account']['name']['#title'] = 'Search String'; - $form['account']['name']['#maxlength'] = 60; - $form['account']['name']['#description'] = 'The name or username of the person to search for.'; -# $form['account']['name']['#required'] = 1; - $form['account']['name']['#attributes'] = array(); - $form['account']['name']['#attributes']['class'] = array(); - $form['account']['name']['#attributes']['class'][0] = 'username'; - $form['account']['name']['#default_value'] = ''; - $form['account']['name']['#access'] = 1; - $form['account']['name']['#weight'] = -10; - $form['submit']['#type'] = 'submit'; - $form['submit']['#value'] = 'Search'; - $form['submit']['#submit'] = array('unl_cas_user_import_search'); -# $form['submit']['#validate'] = array('unl_cas_user_validate'); - - if(isset($form_state['searchstring'])){ - $matches=array(); - $search=$form_state['searchstring']; - // TODO Use peoplefinder for now, use LDAP query once this app is granted access - $results = explode('</person>', file_get_contents('http://peoplefinder.unl.edu/service.php?q='.urlencode($search).'&format=xml&method=getLikeMatches')); - foreach($results as $result) { - $displayName=$uid=$affiliation=$mail=array(); - preg_match('/displayName>([^<]*)/', $result, $displayName); - preg_match('/<uid>([^<]*)/', $result, $uid); - preg_match('/<mail>([^<]*)/', $result, $mail); - preg_match('/<eduPersonPrimaryAffiliation>([^<]*)/', $result, $affiliation); - // TODO Need to handle incomplete results better - if(sizeof($uid)>1) { - $matches[$uid[1].'_'.(sizeof($mail)>1?$mail[1]:'')]=$displayName[1].' ('.$affiliation[1].') ('.$uid[1].')'; - } - } - $form['account']['radio']=array(); - $form['account']['radio']['#type'] = 'radios'; - $form['account']['radio']['#title'] = sizeof($matches).' Records Found'; -# $form['account']['radio']['#required'] = 1; - $form['account']['radio']['#options'] = $matches; - - $form['submit']['#value'] = 'Search Again'; - $form['submit2']['#type'] = 'submit'; - $form['submit2']['#value'] = 'Add Selected User'; - $form['submit2']['#submit'] = array('unl_cas_user_import_submit'); - } + + $form['root'] = array( + '#type' => 'fieldset', + '#title' => 'Import from UNL Directory.', + ); + + $form['root']['account'] = array(); + + $form['root']['account']['name'] = array( + '#type' => 'textfield', + '#title' => 'Search String', + '#description' => 'The name or username of the person to search for.', +# '#required' => TRUE, + ); + + $form['root']['submit'] = array( + '#type' => 'submit', + '#value' => 'Search', + '#submit' => array('unl_cas_user_import_search'), +# '#validate' => array('unl_cas_user_validate'), + ); + + if (isset($form_state['values']['name'])) { + $matches = array(); + $search = $form_state['values']['name']; + // TODO Use directory for now, use LDAP query once this app is granted access + $results = json_decode(file_get_contents('http://directory.unl.edu/service.php?q='.urlencode($search).'&format=json&method=getLikeMatches')); + foreach ($results as $result) { + $displayName = $result->displayName->{0}; + $uid = $result->uid; + $mail = $result->mail; + $affiliations = array(); + foreach ($result->eduPersonPrimaryAffiliation as $affiliation) { + $affiliations[] = $affiliation; + } + $affiliations = implode(', ', $affiliations); + // TODO Need to handle incomplete results better + if ($uid) { + $matches[$uid] = "$displayName ($affiliations) ($uid)"; + } + } + + $form['root']['account']['username'] = array( + '#type' => 'radios', + '#title' => sizeof($matches).' Records Found', +# '#required' => TRUE, + '#options' => $matches, + ); + + $form['root']['submit']['#value'] = 'Search Again'; + + $form['root']['submit2'] = array( + '#type' => 'submit', + '#value' => 'Add Selected User', + '#submit' => array('unl_cas_user_import_submit'), + ); + } return $form; } function unl_cas_user_import_search($form, &$form_state) { - // if only one result is returned should we instead create the user? - $form_state['searchstring']=$form['account']['name']['#value']; - $form_state['rebuild']=TRUE; + // if only one result is returned should we instead create the user? + $form_state['rebuild'] = TRUE; } function unl_cas_user_import_submit($form, &$form_state) { - list($name, $mail) = explode('_', $form_state['complete form']['account']['radio']['#value']); - $userData = array( - 'name' => $name, 'mail' => $mail, 'status' => 1 - ); - $user = user_save(NULL, $userData); - if($user) drupal_set_message('<li>User '.$name.' successfully created.</li>'); + if (!$form_state['values']['username']) { + drupal_set_message('Please select a user.', 'error'); + $form_state['rebuild'] = TRUE; + return; + } + + $user = unl_cas_import_user($form_state['values']['username']); + + if ($user) { + drupal_set_message('User ' . $form_state['values']['username'] . ' successfully created.'); + } else { + drupal_set_message('An error occured importing the user.', 'error'); + } } function unl_cas_config($form, &$form_state) { - + $form['ldap'] = array( '#tree' => TRUE, '#type' => 'fieldset', '#title' => 'LDAP Settings', ); - + $form['ldap']['uri'] = array( '#type' => 'textfield', '#title' => 'URI', @@ -81,7 +98,7 @@ function unl_cas_config($form, &$form_state) { '#parents' => array('ldap', 'uri'), '#required' => TRUE, ); - + $form['ldap']['dn'] = array( '#type' => 'textfield', '#title' => 'Distinguished Name (DN)', @@ -90,19 +107,19 @@ function unl_cas_config($form, &$form_state) { '#parents' => array('ldap', 'dn'), '#required' => TRUE, ); - + $form['ldap']['password'] = array( '#type' => 'password', '#title' => 'Password', '#parents' => array('ldap', 'password'), '#required' => TRUE, ); - + $form['submit'] = array( '#type' => 'submit', '#value' => 'Update', ); - + return $form; } diff --git a/sites/all/modules/unl/unl_cas/unl_cas.info b/sites/all/modules/unl/unl_cas/unl_cas.info index 5e8bea2da3526fb43c3a4716825ba3d899db0989..24b8e1a790a07d70e4853844fcceb6294c57574b 100644 --- a/sites/all/modules/unl/unl_cas/unl_cas.info +++ b/sites/all/modules/unl/unl_cas/unl_cas.info @@ -1,4 +1,3 @@ -; $Id$ name = UNL CAS description = Enables CAS authentication/registration of users through login.unl.edu. package = UNL diff --git a/sites/all/modules/unl/unl_cas/unl_cas.module b/sites/all/modules/unl/unl_cas/unl_cas.module index 673f5910227b08bfd0cebc76cc77d56c811a599f..3ec9e5323918b80be3f73b2a2fa7f5fa50166c26 100644 --- a/sites/all/modules/unl/unl_cas/unl_cas.module +++ b/sites/all/modules/unl/unl_cas/unl_cas.module @@ -7,12 +7,12 @@ function unl_cas_enable() { } function unl_cas_init() { - + // If no one is claiming to be logged in while no one is actually logged in, we don't need CAS. if (!array_key_exists('unl_sso', $_COOKIE) && user_is_anonymous()) { return; } - + // The current request is to the validation URL, we don't want to redirect while a login is pending. if (request_path() == 'user/cas') { return; @@ -31,8 +31,8 @@ function unl_cas_init() { */ function unl_cas_get_adapter() { unl_load_zend_framework(); - - // Start the session because if drupal doesn't then Zend_Session will. + + // Start the session because if drupal doesn't then Zend_Session will. drupal_session_start(); static $adapter; if (!$adapter) { @@ -55,8 +55,8 @@ function unl_cas_menu() { ); $items['admin/people/import'] = array( - 'title' => 'Import User from People Finder', - 'description' => 'Import a user from UNL People Finder', + 'title' => 'Import User from UNL Directory', + 'description' => 'Import a user from the UNL Directory', 'access arguments' => array('administer users'), 'page callback' => 'drupal_get_form', 'page arguments' => array('unl_cas_user_import'), @@ -193,10 +193,10 @@ function unl_cas_import_user($username) { } } catch (Exception $e) { - // don't do anything, just go on to try the people finder method + // don't do anything, just go on to try the peoplefinder method } - // Next, if LDAP didn't work, try peoplefinder. + // Next, if LDAP didn't work, try peoplefinder/directory service. if (!isset($user['email'])) { $xml = @file_get_contents('http://directory.unl.edu/service.php?format=xml&uid=' . $username); if ($xml) {