jledhead wrote:this doesn't work for me with active directory. I suspect its because I don't allow anon lookups. I have tried passing bind username and pass but can't get it to work. any ideas?
function login($user, $pass)
{
if (!$user)
{
return false;
}
// ---------------- Start of LDAP authentication code ----------------
$auth_type="ldap"; // Possible values: ldap | mysql
$ldap_server="od.blah.org";
$base_dn="dc=od,dc=blah,dc=org";
if ($auth_type == "ldap"){
if($connect=@ldap_connect($ldap_server)){ // if connected to ldap server
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
// bind to ldap connection
if(($bind=@ldap_bind($connect)) == false){
print "bind:__FAILED__<br>\n";
return false;
}
// search for user
if (($res_id = ldap_search( $connect, $base_dn, "uid=$user")) == false) {
print "failure: search in LDAP-tree failed<br>";
return false;
}
// verify if there is only one entry of this user:
if (ldap_count_entries($connect, $res_id) > 1) {
print "failure: user $user found more than once<br>\n";
return false;
}
elseif (ldap_count_entries($connect, $res_id) == 1){
if (( $entry_id = ldap_first_entry($connect, $res_id))== false) {
print "failur: entry of searchresult couln't be fetched<br>\n";
return false;
}
if (( $user_dn = ldap_get_dn($connect, $entry_id)) == false) {
print "failure: user-dn coulnd't be fetched<br>\n";
return false;
}
/* Authentifizierung des User */
if (($link_id = ldap_bind($connect, $user_dn, $pass)) == false) {
print "failure: username, password didn't match: $user_dn<br>\n";
return false;
}
// verify if user is already registered at database:
$sel0 = mysql_query("SELECT ID,pass FROM user WHERE name = '$user'");
$chk = mysql_fetch_array($sel0);
// if user already exists, just keep the password updated:
if ($chk["ID"] != "")
{
if ($chk["pass"] != $pass)
{
$this->admin_editpass($chk["ID"], $pass, $pass);
}
}
// if user isn't registered at database yet, add the user right now:
else
{
$newid = $this->add($user, /*$email*/"", 0, $pass, /*$admin*/1, /*$sysloc*/"");
}
unset($chk);
// Now the database is updated the system can try the normal database auth
}
@ldap_close($connect);
}
}
// ---------------- End of LDAP authentication code ------------------
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$pass = sha1($pass);
print $user;
$sel1 = mysql_query("SELECT ID,name,locale,lastlogin FROM user WHERE name = '$user' AND pass = '$pass'");
$chk = mysql_fetch_array($sel1);
print $chk["ID"];
if ($chk["ID"] != "")
{
$rolesobj = new roles();
$now = time();
$_SESSION["userpermissions"] = $rolesobj->getUserRole($chk["ID"]);
$_SESSION['userid'] = $chk['ID'];
$_SESSION['username'] = stripslashes($chk['name']);
$_SESSION['adminstate'] = $chk['admin'];
$_SESSION['lastlogin'] = $now;
$_SESSION['userlocale'] = $chk['locale'];
session_register('userid');
session_register('username');
session_register('adminstate');
session_register('lastlogin');
session_register('userlocale');
$userid = $_SESSION['userid'];
$seid = session_id();
$staylogged = getArrayVal($_POST, 'staylogged');
if ($staylogged == 1)
{
setcookie("PHPSESSID", "$seid", time() + 14 * 24 * 3600);
}
$upd1 = mysql_query("UPDATE user SET lastlogin = '$now' WHERE ID = $userid");
return true;
}
else
{
return false;
}
}
fry wrote:
It seems that the mysql insert isn't working into the user table. I have looked at the user table and my ldap user isn't in there after authenticating.
fry wrote:I can't seem to get this to work.
function ldap_login($user, $pass)
<div class="row">
<label for="stay"><span>LDAP</span></label>
<input type = "checkbox" name = "ldap" id="ldap" value = "1" />
</div>
$ldap = getArrayVal($_POST,"ldap");
if ($user->login($username, $pass))
{
$loc = $url . "index.php?mode=login";
header("Location: $loc");
}
if($ldap)
{
$normal_login = $user->ldap_login($username, $pass)
}
else
{
$normal_login = $user->login($username, $pass)
}
if ($normal_login)
{
$loc = $url . "index.php?mode=login";
header("Location: $loc");
}
Loki wrote:Great Job. Thanks for sharing. Hope LDAP for Active Directory come soon.
Greetings
Peter
($res_id = ldap_search( $connect, $base_dn, "mailNickName=$user")
($res_id = ldap_search( $connect, $base_dn, "samaccountname=$user")
$ldapbinduser = "domain\username"; //put username here
Users browsing this forum: Google [Bot]