Tweaks to authentication system. Database method now allows other
[moodle.git] / auth / db / lib.php
index 5cee2d6..45ed5cf 100644 (file)
@@ -1,17 +1,9 @@
 <?PHP  // $Id$
        // Authentication by looking up an external database table
 
 <?PHP  // $Id$
        // Authentication by looking up an external database table
 
-// This code is completely untested so far - I'm just jotting down ideas ...
+// This code is completely untested so far - IT NEEDS TESTERS!
 // Looks like it should work though ...
 
 // Looks like it should work though ...
 
-$CFG->authdbhost   = "localhost";
-$CFG->authdbtype   = "mysql";     // (postgresql, etc)
-$CFG->authdbname   = "authtest";
-$CFG->authdbtable  = "users";
-$CFG->authdbuser   = "user";
-$CFG->authdbpass   = "pass";
-$CFG->authdbfielduser   = "user";
-$CFG->authdbfieldpass   = "pass";
 
 function auth_user_login ($username, $password) {
 // Returns true if the username and password work
 
 function auth_user_login ($username, $password) {
 // Returns true if the username and password work
@@ -19,14 +11,14 @@ function auth_user_login ($username, $password) {
 
     global $CFG;
 
 
     global $CFG;
 
-    ADOLoadCode($CFG->authdbtype);          
+    ADOLoadCode($CFG->auth_dbtype);          
     $authdb = &ADONewConnection();         
     $authdb = &ADONewConnection();         
-    $authdb->PConnect($CFG->authdbhost,$CFG->authdbuser,$CFG->authdbpass,$CFG->authdbname); 
+    $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname); 
 
 
 
 
-    $rs = $authdb->Execute("SELECT * FROM $CFG->authdbtable 
-                            WHERE $CFG->authdbfielduser = '$username' 
-                              AND $CFG->authdbfieldpass = '$password' ");
+    $rs = $authdb->Execute("SELECT * FROM $CFG->auth_dbtable 
+                            WHERE $CFG->auth_dbfielduser = '$username' 
+                              AND $CFG->auth_dbfieldpass = '$password' ");
     if (!$rs) {
         notify("Could not connect to the specified authentication database...");
         return false;
     if (!$rs) {
         notify("Could not connect to the specified authentication database...");
         return false;
@@ -40,4 +32,35 @@ function auth_user_login ($username, $password) {
 }
 
 
 }
 
 
+function auth_get_userinfo($username){
+// Reads any other information for a user from external database,
+// then returns it in an array
+
+    global $CFG;
+    $config = (array) $CFG;
+
+    ADOLoadCode($CFG->auth_dbtype);          
+    $authdb = &ADONewConnection();         
+    $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname); 
+
+    $fields = array("firstname", "lastname", "email", "phone1", "phone2", 
+                    "department", "address", "city", "country", "description", 
+                    "idnumber", "lang");
+
+    $result = array();
+
+    foreach ($fields as $field) {
+        if ($config["auth_user_$field"]) {
+            if ($rs = $authdb->Execute("SELECT ".$config["auth_user_$field"]." FROM $CFG->auth_dbtable
+                                        WHERE $CFG->auth_dbfielduser = '$username'")) {
+                if ( $rs->RecordCount() == 1 ) {
+                    $result["$field"] = $rs->fields[$config["auth_user_$field"]];
+                }
+            }
+        }
+    }
+
+    return $result;
+}
+
 ?>
 ?>