Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/auth/cas/auth.php
Go to the documentation of this file.
00001 <?php
00002 
00017 if (!defined('MOODLE_INTERNAL')) {
00018     die('Direct access to this script is forbidden.');    
00019 }
00020 
00021 require_once($CFG->dirroot.'/auth/ldap/auth.php');
00022 require_once($CFG->dirroot.'/auth/cas/CAS/CAS.php');
00023 
00027 class auth_plugin_cas extends auth_plugin_ldap {
00028 
00032     function auth_plugin_cas() {
00033         $this->authtype = 'cas';
00034         $this->roleauth = 'auth_cas';
00035         $this->errorlogtag = '[AUTH CAS] ';
00036         $this->init_plugin($this->authtype);
00037     }
00038 
00039     function prevent_local_passwords() {
00040         return true;
00041     }
00042 
00052     function user_login ($username, $password) {
00053         $this->connectCAS();
00054         return phpCAS::isAuthenticated() && (trim(moodle_strtolower(phpCAS::getUser())) == $username);
00055     }
00056 
00062     function is_internal() {
00063         return false;
00064     }
00065 
00072     function can_change_password() {
00073         return false;
00074     }
00075 
00081     function loginpage_hook() {
00082         global $frm;
00083         global $CFG;
00084         global $SESSION, $OUTPUT, $PAGE;
00085 
00086         $site = get_site();
00087         $CASform = get_string('CASform', 'auth_cas');
00088         $username = optional_param('username', '', PARAM_RAW);
00089 
00090         if (!empty($username)) {
00091             if (isset($SESSION->wantsurl) && (strstr($SESSION->wantsurl, 'ticket') ||
00092                                               strstr($SESSION->wantsurl, 'NOCAS'))) {
00093                 unset($SESSION->wantsurl);
00094             }
00095             return;
00096         }
00097 
00098         // Return if CAS enabled and settings not specified yet
00099         if (empty($this->config->hostname)) {
00100             return;
00101         }
00102 
00103         // Connection to CAS server
00104         $this->connectCAS();
00105 
00106         if (phpCAS::checkAuthentication()) {
00107             $frm->username = phpCAS::getUser();
00108             $frm->password = 'passwdCas';
00109             return;
00110         }
00111 
00112         if (isset($_GET['loginguest']) && ($_GET['loginguest'] == true)) {
00113             $frm->username = 'guest';
00114             $frm->password = 'guest';
00115             return;
00116         }
00117 
00118         if ($this->config->multiauth) {
00119             $authCAS = optional_param('authCAS', '', PARAM_RAW);
00120             if ($authCAS == 'NOCAS') {
00121                 return;
00122             }
00123 
00124             // Show authentication form for multi-authentication
00125             // test pgtIou parameter for proxy mode (https connection
00126             // in background from CAS server to the php server)
00127             if ($authCAS != 'CAS' && !isset($_GET['pgtIou'])) {
00128                 $PAGE->set_url('/auth/cas/auth.php');
00129                 $PAGE->navbar->add($CASform);
00130                 $PAGE->set_title("$site->fullname: $CASform");
00131                 $PAGE->set_heading($site->fullname);
00132                 echo $OUTPUT->header();
00133                 include($CFG->dirroot.'/auth/cas/cas_form.html');
00134                 echo $OUTPUT->footer();
00135                 exit();
00136             }
00137         }
00138 
00139         // Force CAS authentication (if needed).
00140         if (!phpCAS::isAuthenticated()) {
00141             phpCAS::setLang($this->config->language);
00142             phpCAS::forceAuthentication();
00143         }
00144     }
00145 
00150     function prelogout_hook() {
00151         global $CFG;
00152 
00153         if ($this->config->logoutcas) {
00154             $backurl = $CFG->wwwroot;
00155             $this->connectCAS();
00156             phpCAS::logoutWithURL($backurl);
00157         }
00158     }
00159 
00164     function connectCAS() {
00165         global $PHPCAS_CLIENT;
00166 
00167         if (!is_object($PHPCAS_CLIENT)) {
00168             // Make sure phpCAS doesn't try to start a new PHP session when connecting to the CAS server.
00169             if ($this->config->proxycas) {
00170                 phpCAS::proxy($this->config->casversion, $this->config->hostname, (int) $this->config->port, $this->config->baseuri, false);
00171             } else {
00172                 phpCAS::client($this->config->casversion, $this->config->hostname, (int) $this->config->port, $this->config->baseuri, false);
00173             }
00174         }
00175 
00176         if($this->config->certificate_check && $this->config->certificate_path){
00177             phpCAS::setCasServerCACert($this->config->certificate_path);
00178         }else{
00179             // Don't try to validate the server SSL credentials
00180             phpCAS::setNoCasServerValidation();
00181         }
00182     }
00183 
00192     function config_form($config, $err, $user_fields) {
00193         global $CFG, $OUTPUT;
00194 
00195         if (!function_exists('ldap_connect')) { // Is php-ldap really there?
00196             echo $OUTPUT->notification(get_string('auth_ldap_noextension', 'auth_ldap'));
00197 
00198             // Don't return here, like we do in auth/ldap. We cas use CAS without LDAP.
00199             // So just warn the user (done above) and define the LDAP constants we use
00200             // in config.html, to silence the warnings.
00201             if (!defined('LDAP_DEREF_NEVER')) {
00202                 define ('LDAP_DEREF_NEVER', 0);
00203             }
00204             if (!defined('LDAP_DEREF_ALWAYS')) {
00205             define ('LDAP_DEREF_ALWAYS', 3);
00206             }
00207         }
00208 
00209         include($CFG->dirroot.'/auth/cas/config.html');
00210     }
00211 
00218     function validate_form(&$form, &$err) {
00219         $certificate_path = trim($form->certificate_path);
00220         if ($form->certificate_check && empty($certificate_path)) {
00221             $err['certificate_path'] = get_string('auth_cas_certificate_path_empty', 'auth_cas');
00222         }
00223     }
00224 
00231     function change_password_url() {
00232         return null;
00233     }
00234 
00238     function process_config($config) {
00239 
00240         // CAS settings
00241         if (!isset($config->hostname)) {
00242             $config->hostname = '';
00243         }
00244         if (!isset($config->port)) {
00245             $config->port = '';
00246         }
00247         if (!isset($config->casversion)) {
00248             $config->casversion = '';
00249         }
00250         if (!isset($config->baseuri)) {
00251             $config->baseuri = '';
00252         }
00253         if (!isset($config->language)) {
00254             $config->language = '';
00255         }
00256         if (!isset($config->proxycas)) {
00257             $config->proxycas = '';
00258         }
00259         if (!isset($config->logoutcas)) {
00260             $config->logoutcas = '';
00261         }
00262         if (!isset($config->multiauth)) {
00263             $config->multiauth = '';
00264         }
00265         if (!isset($config->certificate_check)) {
00266             $config->certificate_check = '';
00267         }
00268         if (!isset($config->certificate_path)) {
00269             $config->certificate_path = '';
00270         }
00271 
00272         // LDAP settings
00273         if (!isset($config->host_url)) {
00274             $config->host_url = '';
00275         }
00276         if (empty($config->ldapencoding)) {
00277             $config->ldapencoding = 'utf-8';
00278         }
00279         if (!isset($config->contexts)) {
00280             $config->contexts = '';
00281         }
00282         if (!isset($config->user_type)) {
00283             $config->user_type = 'default';
00284         }
00285         if (!isset($config->user_attribute)) {
00286             $config->user_attribute = '';
00287         }
00288         if (!isset($config->search_sub)) {
00289             $config->search_sub = '';
00290         }
00291         if (!isset($config->opt_deref)) {
00292             $config->opt_deref = LDAP_DEREF_NEVER;
00293         }
00294         if (!isset($config->bind_dn)) {
00295             $config->bind_dn = '';
00296         }
00297         if (!isset($config->bind_pw)) {
00298             $config->bind_pw = '';
00299         }
00300         if (!isset($config->ldap_version)) {
00301             $config->ldap_version = '3';
00302         }
00303         if (!isset($config->objectclass)) {
00304             $config->objectclass = '';
00305         }
00306         if (!isset($config->memberattribute)) {
00307             $config->memberattribute = '';
00308         }
00309 
00310         if (!isset($config->memberattribute_isdn)) {
00311             $config->memberattribute_isdn = '';
00312         }
00313         if (!isset($config->attrcreators)) {
00314             $config->attrcreators = '';
00315         }
00316         if (!isset($config->groupecreators)) {
00317             $config->groupecreators = '';
00318         }
00319         if (!isset($config->removeuser)) {
00320             $config->removeuser = AUTH_REMOVEUSER_KEEP;
00321         }
00322 
00323         // save CAS settings
00324         set_config('hostname', trim($config->hostname), $this->pluginconfig);
00325         set_config('port', trim($config->port), $this->pluginconfig);
00326         set_config('casversion', $config->casversion, $this->pluginconfig);
00327         set_config('baseuri', trim($config->baseuri), $this->pluginconfig);
00328         set_config('language', $config->language, $this->pluginconfig);
00329         set_config('proxycas', $config->proxycas, $this->pluginconfig);
00330         set_config('logoutcas', $config->logoutcas, $this->pluginconfig);
00331         set_config('multiauth', $config->multiauth, $this->pluginconfig);
00332         set_config('certificate_check', $config->certificate_check, $this->pluginconfig);
00333         set_config('certificate_path', $config->certificate_path, $this->pluginconfig);
00334 
00335         // save LDAP settings
00336         set_config('host_url', trim($config->host_url), $this->pluginconfig);
00337         set_config('ldapencoding', trim($config->ldapencoding), $this->pluginconfig);
00338         set_config('contexts', trim($config->contexts), $this->pluginconfig);
00339         set_config('user_type', moodle_strtolower(trim($config->user_type)), $this->pluginconfig);
00340         set_config('user_attribute', moodle_strtolower(trim($config->user_attribute)), $this->pluginconfig);
00341         set_config('search_sub', $config->search_sub, $this->pluginconfig);
00342         set_config('opt_deref', $config->opt_deref, $this->pluginconfig);
00343         set_config('bind_dn', trim($config->bind_dn), $this->pluginconfig);
00344         set_config('bind_pw', $config->bind_pw, $this->pluginconfig);
00345         set_config('ldap_version', $config->ldap_version, $this->pluginconfig);
00346         set_config('objectclass', trim($config->objectclass), $this->pluginconfig);
00347         set_config('memberattribute', moodle_strtolower(trim($config->memberattribute)), $this->pluginconfig);
00348         set_config('memberattribute_isdn', $config->memberattribute_isdn, $this->pluginconfig);
00349         set_config('attrcreators', trim($config->attrcreators), $this->pluginconfig);
00350         set_config('groupecreators', trim($config->groupecreators), $this->pluginconfig);
00351         set_config('removeuser', $config->removeuser, $this->pluginconfig);
00352 
00353         return true;
00354     }
00355 
00362     function iscreator($username) {
00363         if (empty($this->config->host_url) or (empty($this->config->attrcreators) && empty($this->config->groupecreators)) or empty($this->config->memberattribute)) {
00364             return false;
00365         }
00366 
00367         $textlib = textlib_get_instance();
00368         $extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
00369 
00370         // Test for group creator
00371         if (!empty($this->config->groupecreators)) {
00372             if ($this->config->memberattribute_isdn) {
00373                 if(!($userid = $this->ldap_find_userdn($ldapconnection, $extusername))) {
00374                     return false;
00375                 }
00376             } else {
00377                 $userid = $extusername;
00378             }
00379 
00380             $group_dns = explode(';', $this->config->groupecreators);
00381             if (ldap_isgroupmember($ldapconnection, $userid, $group_dns, $this->config->memberattribute)) {
00382                 return true;
00383             }
00384         }
00385 
00386         // Build filter for attrcreator
00387         if (!empty($this->config->attrcreators)) {
00388             $attrs = explode(';', $this->config->attrcreators);
00389             $filter = '(& ('.$this->config->user_attribute."=$username)(|";
00390             foreach ($attrs as $attr){
00391                 if(strpos($attr, '=')) {
00392                     $filter .= "($attr)";
00393                 } else {
00394                     $filter .= '('.$this->config->memberattribute."=$attr)";
00395                 }
00396             }
00397             $filter .= '))';
00398 
00399             // Search
00400             $result = $this->ldap_get_userlist($filter);
00401             if (count($result) != 0) {
00402                 return true;
00403             }
00404         }
00405 
00406         return false;
00407     }
00408 
00420     function get_userinfo($username) {
00421         if (empty($this->config->host_url)) {
00422             return array();
00423         }
00424         return parent::get_userinfo($username);
00425     }
00426 
00436     function sync_users($do_updates=true) {
00437         if (empty($this->config->host_url)) {
00438             error_log('[AUTH CAS] '.get_string('noldapserver', 'auth_cas'));
00439             return;
00440         }
00441         parent::sync_users($do_updates);
00442     }
00443 }
 All Data Structures Namespaces Files Functions Variables Enumerations