LDAP
Posted: Tue May 20, 2008 5:08 am
Hi Steve,
Is there a LDAP authentification module for Xataface ?
Thank you
Jean
Is there a LDAP authentification module for Xataface ?
Thank you
Jean
function checkCredentials(){
$auth =& Dataface_AuthenticationTool::getInstance();
$app =& Dataface_Application::getInstance();
$creds = $auth->getCredentials();
if ( !isset($auth->conf['ldap_host']) ) $auth->conf['ldap_host'] = 'localhost';
if ( !isset($auth->conf['ldap_port']) ) $auth->conf['ldap_port'] = null;
if ( !isset($auth->conf['ldap_base']) ){
trigger_error("Please specify the LDAP basedn in the [_auth] section of the conf.ini file.", E_USER_ERROR);
}
if ( !function_exists('ldap_connect') ){
trigger_error("Please install the PHP LDAP module in order to user LDAP authentication.", E_USER_ERROR);
}
$ds = ldap_connect($auth->conf['ldap_host'], $auth->conf['ldap_port']);
if ( !$ds ) trigger_error("Failed to connect to LDAP server", E_USER_ERROR);
$filter='samaccountname='.$creds['UserName'];
if ( ldap_bind( $ds, $creds['UserName'].'@intranet', $creds['Password']) ){
return true;
}
return false;
}
function checkCredentials(){
$auth =& Dataface_AuthenticationTool::getInstance();
$app =& Dataface_Application::getInstance();
$creds = $auth->getCredentials();
if ( !isset($auth->conf['ldap_host']) ) $auth->conf['ldap_host'] = 'localhost';
if ( !isset($auth->conf['ldap_port']) ) $auth->conf['ldap_port'] = null;
if ( !isset($auth->conf['ldap_base']) ){
trigger_error("Please specify the LDAP basedn in the [_auth] section of the conf.ini file.", E_USER_ERROR);
}
if ( !function_exists('ldap_connect') ){
trigger_error("Please install the PHP LDAP module in order to user LDAP authentication.", E_USER_ERROR);
}
//echo "qui";
$ds = ldap_connect($auth->conf['ldap_host'], $auth->conf['ldap_port']);
if ( !$ds ) trigger_error("Failed to connect to LDAP server", E_USER_ERROR);
$good=false;
if ( @ldap_bind( $ds, $creds['UserName'].'@'.$auth->conf['ldap_prefix'], $creds['Password']) )
{
$result = ldap_search($ds, $auth->conf['ldap_base'], '(samaccountname='.$creds['UserName'].')');
//Create result set
$entries = ldap_get_entries($ds, $result);
//Sort and print
//echo "User count: " . $entries["count"] . "<br /><br /><b>Users:</b><br />";
//print_r ($entries);
for ($i=0; $i < $entries["count"]; $i++)
{
//echo "name:".$entries[$i]["displayname"][0]."<br />\n";
foreach ($entries[$i]['memberof'] as &$value)
{
list($attribute) = explode(',', $value);
if ($attribute == "CN=".$auth->conf['ldap_group'])
$good=true;
//echo "member:".$attribute."<br />\n";
}
}
ldap_unbind($ds);
}
return $good;
}
ldap_host = "10.10.10.10"
ldap_port = "389"
ldap_base = "ou=Dipendenti, dc=prova, dc=it"
ldap_prefix = "inmi.it"
ldap_group = "GRPInternet"
I have to use sql insert or there is a shortcut?
function checkCredentials(){
$auth =& Dataface_AuthenticationTool::getInstance();
$app =& Dataface_Application::getInstance();
$creds = $auth->getCredentials();
if ( !isset($auth->conf['ldap_host']) ) $auth->conf['ldap_host'] = 'localhost';
if ( !isset($auth->conf['ldap_port']) ) $auth->conf['ldap_port'] = null;
if ( !isset($auth->conf['ldap_base']) ){
trigger_error("Please specify the LDAP basedn in the [_auth] section of the conf.ini file.", E_USER_ERROR);
}
if ( !function_exists('ldap_connect') ){
trigger_error("Please install the PHP LDAP module in order to user LDAP authentication.", E_USER_ERROR);
}
//echo "qui";
$ds = ldap_connect($auth->conf['ldap_host'], $auth->conf['ldap_port']);
if ( !$ds ) trigger_error("Failed to connect to LDAP server", E_USER_ERROR);
$good=false;
if ( @ldap_bind( $ds, $creds['UserName'].'@'.$auth->conf['ldap_prefix'], $creds['Password']) )
{
$result = ldap_search($ds, $auth->conf['ldap_base'], '(samaccountname='.$creds['UserName'].')');
//Create result set
$entries = ldap_get_entries($ds, $result);
//Sort and print
//echo "User count: " . $entries["count"] . "<br /><br /><b>Users:</b><br />";
//print_r ($entries);
for ($i=0; $i < $entries["count"]; $i++)
{
//echo "name:".$entries[$i]["displayname"][0]."<br />\n";
//Autoinsert in usertable if user in ldap not exist
foreach ($entries[$i]['memberof'] as &$value)
{
list($attribute) = explode(',', $value);
if ($attribute == "CN=".$auth->conf['ldap_group'])
{
//echo "member:".$attribute."<br />\n";
$good=true;
$sql = "select * from ".$auth->conf['users_table']." where ".$auth->conf['username_column']."='".$creds['UserName']."'";
$res = mysql_query($sql, $app->db()) or
trigger_error("Failed to get username from usertable qry:".$sql." Err:".mysql_query(), E_USER_ERROR);
//insert new user with default role
if (mysql_num_rows($res) < 1 )
{
$sql = "insert into ".$auth->conf['users_table']." (".$auth->conf['username_column'].",ROLE) value ('".$creds['UserName']."','ADMIN')";
$res = mysql_query($sql, $app->db()) or
trigger_error("Failed to insert username into usertable qry:".$sql." Err:".mysql_query(), E_USER_ERROR);
}
break;
}
}
}
ldap_unbind($ds);
}
return $good;
}