Jump to content

Recommended Posts

Posted

I've developed a basic web based app that connects to our Active Directory through PHP, using a login system.

 

It authenticates against AD and logs the user into a basic page that echos their username.

 

What I really want instead of echoing their username is to fetch either their AD first name and surname or their Display Name.

 

This is the code that I've got to authenticate against AD.

 

// Initialize session
session_start();

function authenticate($user, $password) {
   // Active Directory server
   $ldap_host = "...";

   // Active Directory DN
   $ldap_dn = "OU=...,DC=...,DC=...";

   // Active Directory user group
   $ldap_user_group = "...";

   // Active Directory manager group
   $ldap_manager_group = "...";

   // Domain, for purposes of constructing $user
   $ldap_usr_dom = "@...";

// connect to active directory
$ldap = ldap_connect($ldap_host);


// verify user and password
if($bind = @ldap_bind($ldap, $user . $ldap_usr_dom, $password)) {
   // valid
   // check presence in groups
   $filter = "(sAMAccountName=" . $user . ")";
   $attr = array("memberof");
   $result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server");
   $entries = ldap_get_entries($ldap, $result);
   ldap_unbind($ldap);


   // check groups
   foreach($entries[0]['memberof'] as $grps) {
       // is manager, break loop
       if (strpos($grps, $ldap_manager_group)) { $access = 2; break; }


       // is user
       if (strpos($grps, $ldap_user_group)) $access = 1;
   }


   if ($access != 0) {
       // establish session variables
       $_SESSION['user'] = $user;
       $_SESSION['access'] = $access;
       return true;
   } else {
       // user has no rights
       return false;
   }


} else {
   // invalid name or password
   return false;
}
}
?>

 

Any help would be greatly appreciated! :)

 

Cheers,

 

Nick

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...