Hallo alle miteinander
Leider habe ich nirgends etwas zum Thema "zusätzliche Felder" und LDAP gefunden. Die Anmeldung funktioniert super, die Einträge in die Datenbank werden auch getätigt. Jedoch sollen nicht alle, die im LDAP stehen, Zugang zum Bugtracker erhalten.
Im Moment wird erfolgreich abgefragt "uid=bar,ou=users,dc=ats". Es wird jedoch ein weiterer Wert aus der UID benötigt, der referenziert in welcher Gruppe sich der User befindet. Also idRole, welche unterhalb und in Bezug zu "ou=groups,dc=ats" stehen. Es soll also automatisch nur eine bestimmte Nutzergruppe Zugang haben.
Die Frage ist nun, wie stelle ich das an? Ich habe mich im Moment schon durch diversen Quälcode von Mantis gewuselt und bin bisher keinen Schritt weiter gekommen. Den Vergleich, ob der User Zugang hat oder nicht, würde ich im PHP mit verankern. Es sei denn es gibt eine elegantere Lösung, über die ich noch nicht gestolpert bin.
Wäre genial wenn mir jemand auf die Sprünge helfen könnte.
Viele Grüße
zusätzliche Felder vom LDAP auslesen? (gelöst)
Moderators: Developer, Contributor
zusätzliche Felder vom LDAP auslesen? (gelöst)
Last edited by Wutze on 11 Mar 2011, 06:51, edited 1 time in total.
Re: zusätzliche Felder vom LDAP auslesen?
Ich habe eine Lösung gefunden. Möglich das diese nicht so ganz den Konventionen des Systems entspricht, jedoch funktioniert sie tadellos. (Es sei denn es gibt eine elegantere Lösung, dann her damit ;o))
In der ldap_api in der Funktion ldap_authenticate_by_username habe ich einige Zusätze eingebaut. Die vollständige Funktion mit der Markierung wo sich der Hack befindet.
Vielleicht hilft es ja dem ein oder anderen ;o)
Viele Grüße
In der ldap_api in der Funktion ldap_authenticate_by_username habe ich einige Zusätze eingebaut. Die vollständige Funktion mit der Markierung wo sich der Hack befindet.
Vielleicht hilft es ja dem ein oder anderen ;o)
Viele Grüße
Code: Select all
/**
* Authenticates an user via LDAP given the username and password.
*
* @param string $p_username The user name.
* @param string $p_password The password.
* @return true: authenticated, false: failed to authenticate.
*/
function ldap_authenticate_by_username( $p_username, $p_password ) {
if ( ldap_simulation_is_enabled() ) {
log_event( LOG_LDAP, "Authenticating via LDAP simulation" );
$t_authenticated = ldap_simulation_authenticate_by_username( $p_username, $p_password );
} else {
$c_username = ldap_escape_string( $p_username );
$t_ldap_organization = config_get( 'ldap_organization' );
$t_ldap_root_dn = config_get( 'ldap_root_dn' );
$t_ldap_uid_field = config_get( 'ldap_uid_field', 'uid' );
$t_search_filter = "(&$t_ldap_organization($t_ldap_uid_field=$c_username))";
$t_search_attrs = array(
$t_ldap_uid_field,
'dn',
);
# Bind
log_event( LOG_LDAP, "Binding to LDAP server" );
$t_ds = ldap_connect_bind();
if ( $t_ds === false ) {
log_event( LOG_LDAP, "ldap_connect_bind() returned false." );
return null;
}
# Search for the user id
log_event( LOG_LDAP, "Searching for $t_search_filter" );
$t_sr = ldap_search( $t_ds, $t_ldap_root_dn, $t_search_filter, $t_search_attrs );
if ( $t_sr === false ) {
ldap_unbind( $t_ds );
log_event( LOG_LDAP, "ldap_search() returned false." );
return null;
}
$t_info = ldap_get_entries( $t_ds, $t_sr );
if ( $t_info === false ) {
log_event( LOG_LDAP, "ldap_get_entries() returned false." );
return null;
}
$t_authenticated = false;
if ( $t_info ) {
# Try to authenticate to each until we get a match
for ( $i = 0; $i < $t_info['count']; $i++ ) {
$t_dn = $t_info[$i]['dn'];
# Attempt to bind with the DN and password
if ( @ldap_bind( $t_ds, $t_dn, $p_password ) ) {
$t_authenticated = true;
break;
}
}
}
ldap_free_result( $t_sr );
ldap_unbind( $t_ds );
}
# If user authenticated successfully then update the local DB with information
# from LDAP. This will allow us to use the local data after login without
# having to go back to LDAP. This will also allow fallback to DB if LDAP is down.
if ( $t_authenticated ) {
$t_user_id = user_get_id_by_name( $p_username );
if ( false !== $t_user_id ) {
user_set_field( $t_user_id, 'password', md5( $p_password ) );
if ( ON == config_get( 'use_ldap_realname' ) ) {
$t_realname = ldap_realname( $t_user_id );
user_set_field( $t_user_id, 'realname', $t_realname );
}
##################################################################################################
/** Hack -- Zusatz zur Abfrage nach der Rolle im System
* Es sollen sich nur berechtigte Gruppen einloggen dürfen!
* Addition -- only authorized groups logged in.
* by Wutze - 11.03.2011
*/
$test = ldap_get_field_from_username($p_username,'ldrole');
if ( $test === 'teacher' || $test === 'admin' || $test === 'schulleitung') {
echo "alles OK";
}else{
$t_redirect_url = 'login_page.php?return=' . $t_return .
'&error=1&username=' . urlencode( $p_username );
if ( HTTP_AUTH == config_get( 'login_method' ) ) {
auth_http_prompt();
exit;
}
print_header_redirect( $t_redirect_url );
}
##################################################################################################
if ( ON == config_get( 'use_ldap_email' ) ) {
$t_email = ldap_email_from_username( $p_username );
user_set_field( $t_user_id, 'email', $t_email );
}
}
}
return $t_authenticated;
}