Permalink
Browse files

add of cas functions for login module

  • Loading branch information...
1 parent f8c330c commit 5888f3912c49819ad1df409c460ff849fc949a8c phoenixfr committed Mar 14, 2005
Showing with 85 additions and 0 deletions.
  1. +85 −0 auth/cas/lib.php
View
@@ -12,6 +12,9 @@
This module is using the LDAP Module so you need the /auth/ldap directory.
*/
+require_once($CFG->dirroot.'/config.php');
+include_once($CFG->dirroot.'/lib/cas/CAS.php');
+$cas_validate=false;
define("AUTH_METHOD", 'cas');
require_once($CFG->dirroot.'/auth/cas/commonlib.php');
@@ -69,4 +72,86 @@ function auth_user_login ($username, $password) {
return false;
}
+/**
+ * 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,(Integer)$CFG->cas_port,$CFG->cas_baseuri);
+ phpCAS::setLang($CFG->cas_language);
+ if (!phpCAS::isAuthenticated()){
+ phpCAS::authenticateIfNeeded();
+ }
+ if ($CFG->cas_create_user=="0"){
+ if (get_user_info_from_db("username", phpCAS::getUser())){
+ $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{
+ $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,(Integer)$CFG->cas_port,$CFG->cas_baseuri);
+ phpCAS::setLang($CFG->cas_language);
+ if (!phpCAS::isAuthenticated() && !$CFG->guestloginbutton){
+ phpCAS::authenticateIfNeeded();
+ }
+ if (phpCAS::isAuthenticated()){
+ if ($CFG->cas_create_user=="0"){
+ if (get_user_info_from_db("username", phpCAS::getUser())){
+ $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{
+ $user = authenticate_user_login(phpCAS::getUser(), 'cas');
+ }
+ return $user;
+ }else{
+ return;
+ }
+
+ }else{
+ return $user;
+ }
+}
+
?>

0 comments on commit 5888f39

Please sign in to comment.