diff --git a/profiles/unl_profile/unl_profile.install b/profiles/unl_profile/unl_profile.install index e432b02ec6a589ae6dd0901d1dc985443328d72f..50b2b5f89487c6bfc3ef0ea3588b5e04c0beb04a 100644 --- a/profiles/unl_profile/unl_profile.install +++ b/profiles/unl_profile/unl_profile.install @@ -232,9 +232,10 @@ EOF; 'default' => $new_prefix, // shared tables across all sites - 'filter' => $shared_prefix, - 'filter_format' => $shared_prefix, - 'wysiwyg' => $shared_prefix, + 'filter' => $shared_prefix, + 'filter_format' => $shared_prefix, + 'unl_cas_settings' => $shared_prefix, + 'wysiwyg' => $shared_prefix, ); $settings['databases'] = array( diff --git a/sites/all/modules/unl/unl_cas.admin.inc b/sites/all/modules/unl/unl_cas.admin.inc index 1a211a6cf497529639c2d0faae4d727532195245..80a5abdcc3c5e2e8cc4c07cc625746ab5c1f402f 100644 --- a/sites/all/modules/unl/unl_cas.admin.inc +++ b/sites/all/modules/unl/unl_cas.admin.inc @@ -65,4 +65,49 @@ function unl_cas_user_import_submit($form, &$form_state) { if($user) drupal_set_message('<li>User '.$name.' successfully created.</li>'); } -?> +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', + '#description' => 'ie: ldap://example.com/', + '#default_value' => unl_cas_get_setting('ldap_uri'), + '#parents' => array('ldap', 'uri'), + '#required' => TRUE, + ); + + $form['ldap']['dn'] = array( + '#type' => 'textfield', + '#title' => 'Distinguished Name (DN)', + '#description' => 'ie: uid=admin,dc=example,dc=com', + '#default_value' => unl_cas_get_setting('ldap_dn'), + '#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; +} + +function unl_cas_config_submit($form, &$form_state) { + unl_cas_set_setting('ldap_uri', $form_state['values']['ldap']['uri']); + unl_cas_set_setting('ldap_dn', $form_state['values']['ldap']['dn']); + unl_cas_set_setting('ldap_password', $form_state['values']['ldap']['password']); +} diff --git a/sites/all/modules/unl/unl_cas.info b/sites/all/modules/unl/unl_cas.info index 5c307762490072bea143ddc376dc889020a1be53..11e218a5c2745ef590ffb76c11817e4c9e4d6df3 100644 --- a/sites/all/modules/unl/unl_cas.info +++ b/sites/all/modules/unl/unl_cas.info @@ -2,5 +2,8 @@ name = UNL CAS description = Enables UNL CAS authentication/registration of users. core = 7.x +version = "7.x-1.0-20101129" +configure = admin/config/people/unl_cas + files[] = unl_cas.module files[] = unl_loader.php diff --git a/sites/all/modules/unl/unl_cas.install b/sites/all/modules/unl/unl_cas.install new file mode 100644 index 0000000000000000000000000000000000000000..20849a07382066eba58c16564a080decc9f40b72 --- /dev/null +++ b/sites/all/modules/unl/unl_cas.install @@ -0,0 +1,55 @@ +<?php + +function unl_cas_schema() { + $schema = array(); + $schema['unl_cas_settings'] = array( + 'description' => 'Settings for the UNL CAS module.', + 'fields' => array( + 'name' => array( + 'description' => 'The name of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value of the variable.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('name'), + ); + + return $schema; +} + +/** + * Updates prior to upgrading to unl module 7.x-1.0 + */ +function unl_cas_update_7100() { + $table = array( + 'description' => 'Settings for the UNL CAS module.', + 'fields' => array( + 'name' => array( + 'description' => 'The name of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value of the variable.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('name'), + ); + + db_create_table('unl_cas_settings', $table); +} diff --git a/sites/all/modules/unl/unl_cas.module b/sites/all/modules/unl/unl_cas.module index 8501c15644d58f39dee2f1d6e262afe7344e18d4..24ce33ef9eabdcf5a830500760429f2d1e5670e6 100644 --- a/sites/all/modules/unl/unl_cas.module +++ b/sites/all/modules/unl/unl_cas.module @@ -59,6 +59,16 @@ function unl_cas_menu() { 'weight' => 1, ); + $items['admin/config/people/unl_cas'] = array( + 'title' => 'UNL CAS', + 'description' => 'Configure the UNL CAS module', + 'access callback' => 'unl_cas_user_is_administrator', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('unl_cas_config'), + 'file' => 'unl_cas.admin.inc', + 'weight' => 1, + ); + return $items; } @@ -79,10 +89,8 @@ function unl_cas_validate() { if ($auth) { $username = $cas->getUsername(); - $user = user_load_by_name($username); - if (!$user) { - $user = unl_cas_import_user($username); - } + $user = unl_cas_import_user($username); + if ($GLOBALS['user']->uid != $user->uid) { $GLOBALS['user'] = $user; user_login_finalize(); @@ -161,28 +169,54 @@ function unl_cas_user_logout($account) { } function unl_cas_import_user($username) { - $xml = @file_get_contents('http://peoplefinder.unl.edu/service.php?format=xml&uid=' . $username); - if ($xml) { - $dom = new DOMDocument(); - $dom->loadXML($xml); - $firstName = $dom->getElementsByTagName('givenName')->item(0)->textContent; - $lastName = $dom->getElementsByTagName('sn')->item(0)->textContent; - $email = $dom->getElementsByTagName('mail')->item(0)->textContent; - $displayName = $dom->getElementsByTagName('displayName')->item(0)->textContent; + $user = array(); + + // First, try getting the info from LDAP. + try { + $ldap = new Unl_Ldap(unl_cas_get_setting('ldap_uri')); + $ldap->bind(unl_cas_get_setting('ldap_dn'), unl_cas_get_setting('ldap_password')); + $results = $ldap->search('dc=unl,dc=edu', 'uid=' . $username); + if (count($results) > 0) { + $result = $results[0]; + + $user['firstName'] = $result['givenname'][0]; + $user['lastName'] = $result['sn'][0]; + $user['email'] = $result['mail'][0]; + $user['displayName'] = $result['displayname'][0]; + } } - else { - $email = $username . '@unl.edu'; + catch (Exception $e) { + // don't do anything, just go on to try the people finder method + } + + // Next, if LDAP didn't work, try peoplefinder. + if (!isset($user['email'])) { + $xml = @file_get_contents('http://peoplefinder.unl.edu/service.php?format=xml&uid=' . $username); + if ($xml) { + $dom = new DOMDocument(); + $dom->loadXML($xml); + $user['firstName'] = $dom->getElementsByTagName('givenName')->item(0)->textContent; + $user['lastName'] = $dom->getElementsByTagName('sn')->item(0)->textContent; + $user['email'] = $dom->getElementsByTagName('mail')->item(0)->textContent; + $user['displayName'] = $dom->getElementsByTagName('displayName')->item(0)->textContent; + } + } + + // Finally, if peoplefinder didn't work either, just guess. + if (!isset($user['email'])) { + $user['email'] = $username . '@unl.edu'; } $userData = array( 'name' => $username, - 'mail' => $email, + 'mail' => $user['email'], 'status' => 1, 'timezone' => variable_get('date_default_timezone', @date_default_timezone_get()), ); - $user = user_save(NULL, $userData); - return $user; + $account = user_load_by_name($username); + + return user_save($account, $userData); } /** @@ -258,3 +292,40 @@ function unl_cas_preprocess_user_picture(&$variables) { $variables['user_picture'] = '<img class="profile_pic medium" src="http://planetred.unl.edu/pg/icon/unl_' . $username . '/medium" alt="' . $username . '\'s photo" />'; } + +function unl_cas_get_setting($name, $default = NULL) { + $data = db_select('unl_cas_settings', 's') + ->fields('s', array('value')) + ->condition('s.name', $name) + ->execute() + ->fetchCol(); + if (count($data) > 0) { + return unserialize($data[0]); + } + return $default; +} + +function unl_cas_set_setting($name, $value) { + $value = serialize($value); + if (unl_cas_get_setting($name, '__foobar__') == '__foobar__') { + db_insert('unl_cas_settings') + ->fields(array('name', 'value')) + ->values(array($name, $value)) + ->execute(); + } + else { + db_update('unl_cas_settings') + ->fields(array('value' => $value)) + ->condition('name', $name) + ->execute(); + } +} + +function unl_cas_user_is_administrator() +{ + $user = $GLOBALS['user']; + if ($user && in_array(variable_get('user_admin_role'), array_keys($user->roles))) { + return TRUE; + } + return FALSE; +} \ No newline at end of file