Ich habe das unten beschrieben Verfahren als nuSOAP Webservices realisiert. Vielleicht nützt Dir das folgende Script etwas ..
PHP-Code:
<?php
// include nuSOAP
require_once("nusoap.php");
// define LDAP settings
define("AUTH_WS_LDAP_SERVER", "ldapserverdummy");
define("AUTH_WS_LDAP_PORT", false);
define("AUTH_WS_LDAP_BASEDN", "ou=People,dc=zv,dc=mmo,dc=de,o=internet");
/**
* authLDAPCNByUID()
*
* This function will return the cn for the user
* identified by the UID
*
*/
function authLDAPCNByUID($ldapUID) {
// initialize
$ldapReturn = false;
// check if LDAP is supported by the PHP installation
if(function_exists("ldap_connect") &&
function_exists("ldap_bind") &&
function_exists("ldap_unbind") &&
function_exists("ldap_close"))
{
// trim credentials
$ldapUID = trim($ldapUID);
// check credentials
if(strlen($ldapUID)) {
// try to connect to LDAP
if(AUTH_WS_LDAP_PORT !== false) {
// connect with port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER, AUTH_WS_LDAP_PORT);
} else {
// connect without port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER);
}
if($ldapConn) {
// simple bind
$ldapBind = ldap_bind($ldapConn);
if($ldapBind) {
// set list of attributes to return
$ldapAttributesOnly = array("cn");
// search user information
$ldapSearch = ldap_search($ldapConn, AUTH_WS_LDAP_BASEDN, "(uid={$ldapUID})", $ldapAttributesOnly);
// if search was successfull
if($ldapSearch !== false) {
// get entries returned by search
$ldapEntries = ldap_get_entries($ldapConn, $ldapSearch);
// check if the entry for the user was found
if($ldapEntries['count']) {
// get attribute
if($ldapEntries[0]['cn']['count'] >= 1) {
$ldapReturn = $ldapEntries[0]['cn'][0];
} else {
// no cn defined
$ldapReturn = false;
}
} else {
// no matching entry found
$ldapReturn = false;
}
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(5, "authLDAPCNByUID", "Error while processing search (uid={$ldapUID}*) - ".ldap_error().": ".ldap_err2str(ldap_error()));
}
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(4, "authLDAPCNByUID", "Unable to bind to LDAP Server without security credentials".ldap_error().": ".ldap_err2str(ldap_error()));
}
// bind successfully done, unbind
ldap_unbind($ldapConn);
// close connection
// deprecated: ldap_close($ldapConn);
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(1, "authLDAPCNByUID", "Unable to connect to LDAP Server ".AUTH_WS_LDAP_SERVER.":".AUTH_WS_LDAP_PORT);
}
} else {
// missing credentials
$ldapReturn = new soap_fault(2, "authLDAPCNByUID", "Invalid credentials. Username and password must be givven");
}
} else {
// missing credentials
$ldapReturn = new soap_fault(3, "authLDAPCNByUID", "The server does not support access to LDAP");
}
// return
return($ldapReturn);
}
/**
* authLDAPUserDetailsByUID()
*
* This function will return the user details for the user
* identified by the UID
*/
function authLDAPUserDetailsByUID($ldapUID) {
// initialize
$ldapReturn = false;
// check if LDAP is supported by the PHP installation
if(function_exists("ldap_connect") &&
function_exists("ldap_bind") &&
function_exists("ldap_unbind") &&
function_exists("ldap_close"))
{
// trim credentials
$ldapUID = trim($ldapUID);
// check credentials
if(strlen($ldapUID)) {
// try to connect to LDAP
if(AUTH_WS_LDAP_PORT !== false) {
// connect with port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER, AUTH_WS_LDAP_PORT);
} else {
// connect without port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER);
}
if($ldapConn) {
// simple bind
$ldapBind = ldap_bind($ldapConn);
if($ldapBind) {
// set list of attributes to return
$ldapAttributesOnly = array("cn","mail","ou","l","userrole","firma", "orgkey", "corporatenetworknumber", "telephonenumber", "roomnumber", "givenname", "sn");
// search user information
$ldapSearch = ldap_search($ldapConn, AUTH_WS_LDAP_BASEDN, "(uid={$ldapUID})", $ldapAttributesOnly);
// if search was successfull
if($ldapSearch !== false) {
// get entries returned by search
$ldapEntries = ldap_get_entries($ldapConn, $ldapSearch);
//return($ldapEntries);
// initialize return struct
$ldapReturn = array();
// get all information
for($attr = 0; $attr < count($ldapAttributesOnly); $attr++) {
// initialize field with empty string
$ldapReturn[$ldapAttributesOnly[$attr]] = "";
// get attribute
if($ldapEntries[0][$ldapAttributesOnly[$attr]]['count'] >= 1) {
$ldapReturn[$ldapAttributesOnly[$attr]] = $ldapEntries[0][$ldapAttributesOnly[$attr]][0];
}
}
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(5, "authLDAPUserDetailsByUID", "Error while processing search (uid={$ldapUID}*) - ".ldap_error().": ".ldap_err2str(ldap_error()));
}
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(4, "authLDAPUserDetailsByUID", "Unable to bind to LDAP Server without security credentials".ldap_error().": ".ldap_err2str(ldap_error()));
}
// bind successfully done, unbind
ldap_unbind($ldapConn);
// close connection
// deprecated: ldap_close($ldapConn);
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(1, "authLDAPUserDetailsByUID", "Unable to connect to LDAP Server ".AUTH_WS_LDAP_SERVER.":".AUTH_WS_LDAP_PORT);
}
} else {
// missing credentials
$ldapReturn = new soap_fault(2, "authLDAPUserDetailsByUID", "Invalid credentials. Username and password must be givven");
}
} else {
// missing credentials
$ldapReturn = new soap_fault(3, "authLDAPUserDetailsByUID", "The server does not support access to LDAP");
}
// return
return($ldapReturn);
}
/**
* authLDAPBindCNPassword()
*
* This function will check the login credentials based
* on cn and password of user
*
*/
function authLDAPBindCNPassword($ldapCN, $ldapPassword) {
// initialize
$ldapReturn = false;
// check if LDAP is supported by the PHP installation
if(function_exists("ldap_connect") &&
function_exists("ldap_bind") &&
function_exists("ldap_unbind"))
{
// trim credentials
$ldapCN = trim($ldapCN);
$ldapPassword = trim($ldapPassword);
// check credentials
if(strlen($ldapCN) && strlen($ldapPassword)) {
// try to connect to LDAP
if(AUTH_WS_LDAP_PORT !== false) {
// connect with port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER, AUTH_WS_LDAP_PORT);
} else {
// connect without port
$ldapConn = ldap_connect(AUTH_WS_LDAP_SERVER);
}
if($ldapConn) {
// create rdn
$ldapRdn = "cn={$ldapCN},".AUTH_WS_LDAP_BASEDN;
// try to bind
$ldapBind = ldap_bind($ldapConn, $ldapRdn, $ldapPassword);
if($ldapBind) {
// bind successfully done, unbind
ldap_unbind($ldapConn);
$ldapReturn = true;
} else {
// unable to bind with givven credentials
$ldapReturn = false;
}
} else {
// unable to connect to LDAP server
$ldapReturn = new soap_fault(1, "authLDAPBindCNPassword", "Unable to connect to LDAP Server ".AUTH_WS_LDAP_SERVER.":".AUTH_WS_LDAP_PORT);
}
} else {
// missing credentials
$ldapReturn = new soap_fault(2, "authLDAPBindCNPassword", "Invalid credentials. Username and password must be givven");
}
} else {
// missing credentials
$ldapReturn = new soap_fault(3, "authLDAPBindCNPassword", "The server does not support access to LDAP");
}
// return
return($ldapReturn);
}
/**
* authLDAPAuthUser()
*
* This function will check the login credentials
* based on uid and password of user
*
*/
function authLDAPAuthUser($ldapUID, $ldapPassword) {
// initialize
$ldapReturn = false;
// trim credentials
$ldapUID = trim($ldapUID);
$ldapPassword = trim($ldapPassword);
// check credentials
if(strlen($ldapUID) && strlen($ldapPassword)) {
// determin CN of user
$ldapCN = authLDAPCNByUID($ldapUID);
if(is_string($ldapCN)) {
// try to bind with user credentials
$ldapReturn = authLDAPBindCNPassword($ldapCN, $ldapPassword);
} else {
// no CN for user found
$ldapReturn = false;
}
} else {
// missing credentials
$ldapReturn = new soap_fault(3, "authLDAPAuthUser", "Invalid credentials. UID and password must be givven");
}
// return
return($ldapReturn);
}
// create global SOAP server instance
$authSOAPServer = new soap_server();
// register service
$authSOAPServer->register("authLDAPCNByUID");
$authSOAPServer->register("authLDAPBindCNPassword");
$authSOAPServer->register("authLDAPAuthUser");
$authSOAPServer->register("authLDAPUserDetailsByUID");
// serve request
$authSOAPServer->service($HTTP_RAW_POST_DATA);
?>
Die Funktionen kannst Du Dir ja hinbiegen, falls Du nicht via SOAP zugreifen möchtest. Die
Prüfung der Zugangsdaten kannst Du dann mittels
PHP-Code:
<?php
if(authLDAPAuthUser("uname", "geheim") === true) {
// passt ...
}
?>
durchführen.
Unter Umständen ist noch hier und da eine Anpassung notwendig, dürfte aber als Leitfaden reichen.
Viel Erfolg
José