moodle/auth/cas/lib.php

169 lines
5.0 KiB
PHP
Raw Normal View History

2005-03-09 12:36:24 +00:00
<?PHP
// $Id$
2005-03-16 10:40:39 +00:00
// author: romuald Lorthioir
2005-03-09 12:36:24 +00:00
//CHANGELOG:
2005-03-16 10:40:39 +00:00
//16/03/2005 Use of LDAP Module
2005-03-09 12:36:24 +00:00
//05.02.2005 Added CAS module
/* README!
CAS Module
This Module can be turn ON/OFF on admin screen.
2005-03-13 10:14:01 +00:00
The /login/index.php module is intercepted and replace with the login.php.
2005-03-16 10:40:39 +00:00
This Module is using the /auth/cas/index_form.html.
2005-03-09 12:36:24 +00:00
This module is using the LDAP Module so you need the /auth/ldap directory.
2005-03-16 10:40:39 +00:00
You can see /auth/ldap/lib.php for the other functions.
2005-03-09 12:36:24 +00:00
*/
defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
2005-03-09 12:36:24 +00:00
2005-03-16 10:40:39 +00:00
define('AUTH_LDAP_NAME', 'cas'); // for ldap module
2005-03-14 13:01:22 +00:00
require_once($CFG->dirroot.'/config.php');
2005-03-16 10:40:39 +00:00
require_once($CFG->dirroot.'/auth/ldap/lib.php');
require_once($CFG->dirroot.'/lib/cas/CAS.php');
2005-03-14 13:01:22 +00:00
$cas_validate=false;
2005-03-09 12:36:24 +00:00
2005-03-16 10:40:39 +00:00
/**
* replace the ldap auth_user_login function
* authenticates user againt CAS with ldap
* Returns true if the username and password work
* and false if they don't
*
* @param string $username
* @param string $password
*
*/
function cas_ldap_auth_user_login ($username, $password) {
2005-03-09 12:36:24 +00:00
/// Returns true if the username and password work
/// and false if they don't
global $CFG;
if (!$username or !$password) { // Don't allow blank usernames or passwords
return false;
}
2005-03-16 10:40:39 +00:00
if ($CFG->auth == "cas" && !empty($CFG->cas_enabled)){ //cas specific
2005-03-09 12:36:24 +00:00
if ($CFG->cas_create_user=="0"){
if (record_exists('user', 'username', $username)){
2005-03-09 12:36:24 +00:00
return true;
}else{
return false;
}
}else{
return true;
}
}
$ldap_connection = auth_ldap_connect();
if ($ldap_connection) {
$ldap_user_dn = auth_ldap_find_userdn($ldap_connection, $username);
//if ldap_user_dn is empty, user does not exist
if(!$ldap_user_dn){
ldap_close($ldap_connection);
return false;
}
// Try to bind with current username and password
$ldap_login = ldap_bind($ldap_connection, $ldap_user_dn, $password);
ldap_close($ldap_connection);
if ($ldap_login) {
2005-03-16 10:40:39 +00:00
if ($CFG->cas_create_user=="0"){ //cas specific
if (record_exists('user', 'username', $username)){
2005-03-09 12:36:24 +00:00
return true;
}else{
return false;
}
}else{
return true;
}
}
} else {
ldap_close($ldap_connection);
error("LDAP part of CAS-module cannot connect to server: $CFG->ldap_host_url");
}
return false;
}
2005-03-14 13:01:22 +00:00
/**
* authenticates user against CAS from screen login
* the user doesn't have a CAS Ticket yet.
*
* Returns an object user if the username and password work
* and nothing if they don't
*
* @param string $username
* @param string $password
*
*/
function cas_authenticate_user_login ($username, $password) {
global $CFG;
$cas_validate=true;
phpCAS::client($CFG->cas_version,$CFG->cas_hostname,(int)$CFG->cas_port,$CFG->cas_baseuri);
2005-03-14 13:01:22 +00:00
phpCAS::setLang($CFG->cas_language);
phpCAS::forceAuthentication();
2005-03-14 13:01:22 +00:00
if ($CFG->cas_create_user=="0"){
if (record_exists('user', 'username', phpCAS::getUser())) {
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
2005-03-14 13:01:22 +00:00
}else{
//login as guest if CAS but not Moodle and not automatic creation
if ($CFG->guestloginbutton){
$user = authenticate_user_login('guest', 'guest');
}else{
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
}
}
}else{
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
}
return $user;
}
/**
* authenticates user against CAS when first call of Moodle
* if already in CAS (cookie with the CAS ticket), don't have to log again (SSO)
*
* Returns an object user if the username and password work
* and nothing if they don't
*
* @param object $user
*
*/
function cas_automatic_authenticate ($user="") {
global $CFG;
if (!$cas_validate){
$cas_validate=true;
phpCAS::client($CFG->cas_version,$CFG->cas_hostname,(int)$CFG->cas_port,$CFG->cas_baseuri);
2005-03-14 13:01:22 +00:00
phpCAS::setLang($CFG->cas_language);
$cas_user_exist=phpCAS::checkAuthentication();
if (!$cas_user_exist && !$CFG->guestloginbutton){
$cas_user_exist=phpCAS::forceAuthentication();
2005-03-14 13:01:22 +00:00
}
if ($cas_user_exist){
2005-03-14 13:01:22 +00:00
if ($CFG->cas_create_user=="0"){
if (record_exists('user', 'username', phpCAS::getUser())) {
2005-03-14 13:01:22 +00:00
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
}else{
//login as guest if CAS but not Moodle and not automatic creation
if ($CFG->guestloginbutton){
$user = authenticate_user_login('guest', 'guest');
}else{
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
}
}
}else{
2005-03-16 10:40:39 +00:00
2005-03-14 13:01:22 +00:00
$user = authenticate_user_login(phpCAS::getUser(), 'cas');
}
return $user;
}else{
return;
}
}else{
return $user;
}
}
?>