From 889f263f9c6249846bfda8ac91c24548405ef418 Mon Sep 17 00:00:00 2001 From: Thomas BELOT Date: Fri, 5 Jun 2015 15:36:24 +0200 Subject: [PATCH 1/2] Added a way to customize user's realname leaving the username untouched --- CASAuth.php | 6 ++++-- CASAuthSettings.php.template | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CASAuth.php b/CASAuth.php index 1f2f91f..5d99e90 100644 --- a/CASAuth.php +++ b/CASAuth.php @@ -110,7 +110,6 @@ function casLogin($user, &$result) { // Get username $username = casNameLookup(phpCAS::getUser()); - $email = casEmailLookup(phpCAS::getUser()); // If we are restricting users AND the user is not in // the allowed users list, lets block the login @@ -127,9 +126,12 @@ function casLogin($user, &$result) { // Create a new account if the user does not exists if ($u->getID() == 0 && $CASAuth["CreateAccounts"]) { + //Get email and realname + $realname = casRealNameLookup(phpCAS::getUser()); + $email = casEmailLookup(phpCAS::getUser()); // Create the user $u->addToDatabase(); - $u->setRealName($username); + $u->setRealName($realname); $u->setEmail($email); // PwdSecret is used to salt the username, which is // then used to create an md5 hash which becomes the diff --git a/CASAuthSettings.php.template b/CASAuthSettings.php.template index 0d7246e..57ee1e4 100644 --- a/CASAuthSettings.php.template +++ b/CASAuthSettings.php.template @@ -95,6 +95,14 @@ function casNameLookup($username) { return $username; } +# If you dont like the uid that CAS returns (ie. it returns a number) you can +# modify the routine below to return a customized real name instead. +# +# Default: Returns the username, untouched +function casRealNameLookup($username) { + return $username; +} + # If your users aren't all on the same email domain you can # modify the routine below to return their email address # From 013993d39483d2d77f1c55b21d08bfec1401204c Mon Sep 17 00:00:00 2001 From: Thomas BELOT Date: Fri, 5 Jun 2015 17:08:41 +0200 Subject: [PATCH 2/2] Exemple of implementations of casRealNameLookup and casEmailLookup with mysqli as source --- CASAuthSettings.php.template | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/CASAuthSettings.php.template b/CASAuthSettings.php.template index 57ee1e4..6d1cb33 100644 --- a/CASAuthSettings.php.template +++ b/CASAuthSettings.php.template @@ -112,3 +112,60 @@ function casEmailLookup($username) { return $username."@".$CASAuth["EmailDomain"]; } +/* +#Below is a couple of implementations of casRealNameLookup and casEmailLookup +#that will fetch user details in a separate DB based on the CAS username + +global $casUserData; +$casUserData=array(); + +#Queries the DB and caches the result in memory +function casFetchUserData($username) { + global $casUserData; + if(isset($casUserData[$username])) + return $casUserData[$username]; + ## Please customize connection params to your needs + $dbh = mysqli_connect('host', 'user', 'pass', 'base') + or die("unable to contact CAS user database"); + if(!$dbh || $dbh->connect_error) + die("CAS database error : $dbh->connect_errno: $dbh->connect_error"); + ## Please customize query to your needs keeping aliases + $SQL = 'SELECT x as realname, y as email FROM table WHERE userid=?'; + $stmt=$dbh->prepare($SQL) + or die("unable to prepare CAS user database request"); + $stmt->bind_param("s", $username); + $stmt->execute() + or die("unable to execute CAS user database request"); + $stmt->bind_result($realname,$email); + $stmt->fetch() + or die("unable to fetch assoc CAS user database request"); + $data = array( + "realname"=>$realname, + "email"=>$email, + ); + $stmt->close(); + $dbh->close(); + $casUserData[$username] = $data; + return $data; +} + +# If you dont like the uid that CAS returns (ie. it returns a number) you can +# modify the routine below to return a customized username instead. +# +# Default: Returns the username, untouched +function casNameLookup($username) { + return $username; +} + +# Returns the "realname" field of the query +function casRealNameLookup($username) { + $tmp=casFetchUserData($username); + return $tmp['realname']; +} + +# Returns the "email" field of the query +function casEmailLookup($username) { + $tmp=casFetchUserData($username); + return $tmp['email']; +} +