Pbearne
Pbearne

Reputation: 1165

Accessing user credentials with PHP on IIS 7

We are installing WordPress MU onto an IIS 7 sever

We need to enable single sign on against the AD.

We are happy to code the PHP to auto login / create accounts for the users etc.

What we need help with is how to we get the users Credential (username,email,name etc.) from the IIS / windows server into PHP variables so we can use them.

All advice welcomed

Upvotes: 1

Views: 1677

Answers (1)

reconbot
reconbot

Reputation: 5287

You want to use ldap. The hardest part for me was figuring out the OU my users were in and the format we needed to have our usernames ([email protected] instead of domain\username). We have them all in the default Users OU. This is taken from a cakephp app I wrote and it grabs some additional information, but it should set you along the right track. Your php of course needs to have an ldap extension compiled.

protected function findLdapUser($username, $password, $otheruser = false){
    $config = Configure::read('ldap');
    if(!$username && !$password && $otheruser){
        $username = $config['username'];
        $password = $config['password'];
    }
    if($password == ""){return false;} //prevent anonmyous bind
    if(!$otheruser){
        $otheruser = $username;
    }
    $connection = ldap_connect($config['host'], $config['port']);
    if($connection === false){      //does not detect properly depending on enviroment!
        debug("cannot connect to ldap server");
        return 0; //cannot connect!
    }
    ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, $config['version']);
    if (!@ldap_bind($connection, $username . $config['userpostfix'], $password)){
        return false;
    }

    //search for user data
    $fields = array('mail', 'name', 'telephoneNumber', 'physicalDeliveryOfficeName');
    //$filter = "sAMAccountName=" . $username;
    $filter = "userPrincipalName=" . $otheruser . $config['userpostfix'];
    $results = ldap_search($connection, "CN=USERS,".$config['basedn'], $filter, $fields);
    $info =  ldap_get_entries($connection, $results);
    if($info['count'] == 0){return false;}
    @ldap_unbind($connection);
    $return['LdapUser']['email'] = $info[0]['mail'][0];
    $return['LdapUser']['fullname'] = $info[0]['name'][0];
    //supress warnings
    @$return['LdapUser']['office'] = $info[0]['physicaldeliveryofficename'][0];
    @$return['LdapUser']['phone'] = $info[0]['telephonenumber'][0];
    return $return;
}

Upvotes: 1

Related Questions