Skip to content

Commit

Permalink
Refactor to comply with requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AW3i committed May 17, 2017
1 parent d5cf66d commit acd6c66
Showing 1 changed file with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
}
*
* PHP version 5
*
Expand Down Expand Up @@ -224,14 +223,15 @@ public function login()
throw new LoginException(sprintf('Failed to create principal: %s', $e->getMessage()));
}
}
$ldap_connection = $this->ldapConnect();
if ($ldap_connection) {

$ldapConnection = $this->ldapConnect();
if ($ldapConnection) {
// Replace the placeholder with the actual username of the user
$this->baseFilter = preg_replace('/\{0\}/', "$name", $this->baseFilter);

$search = ldap_search($ldap_connection, $this->baseDN, $this->baseFilter);
$entry = ldap_first_entry($ldap_connection, $search);
$userDN = ldap_get_dn($ldap_connection, $entry);
$search = ldap_search($ldapConnection, $this->baseDN, $this->baseFilter);
$entry = ldap_first_entry($ldapConnection, $search);
$userDN = ldap_get_dn($ldapConnection, $entry);

if (!(isset($userDN))) {
throw new LoginException(sprintf('User not found in LDAP directory'));
Expand All @@ -240,8 +240,8 @@ public function login()
throw new LoginException(sprintf('Couldn\'t connect to LDAP server'));
}

//Bind the authenticating user to the LDAP directory
$bind = ldap_bind($ldap_connection, $userDN, $password);
// bind the authenticating user to the LDAP directory
$bind = ldap_bind($ldapConnection, $userDN, $password);
if ($bind === false) {
throw new LoginException(sprintf('Username or password wrong'));
}
Expand Down Expand Up @@ -299,6 +299,10 @@ protected function addRole($groupName, $name)
try {
$group->addMember($this->createIdentity(new String($name)));
} catch (\Exception $e) {
$application
->getNamingDirectory()
->search(NamingDirectoryKeys::SYSTEM_LOGGER)
->error($e->__toString());
}
}

Expand Down Expand Up @@ -340,27 +344,27 @@ protected function getIdentity()
protected function ldapConnect()
{

$ldap_connection = ldap_connect($this->ldapUrl, $this->ldapPort);
$ldapConnection = ldap_connect($this->ldapUrl, $this->ldapPort);

if ($ldap_connection) {
if ($ldapConnection) {
if ($this->ldapStartTls === 'true') {
ldap_start_tls($ldap_connection);
ldap_start_tls($ldapConnection);
}
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);

//anonymous login
if ($this->allowEmptyPasswords === 'true') {
$bind = ldap_bind($ldap_connection);
$bind = ldap_bind($ldapConnection);
} else {
$bind = ldap_bind($ldap_connection, $this->bindDN, $this->bindCredential);
$bind = ldap_bind($ldapConnection, $this->bindDN, $this->bindCredential);
}
if (!$bind) {
throw new LoginException('Bind to server failed');
}
} else {
return false;
}
return $ldap_connection;
return $ldapConnection;
}

/**
Expand All @@ -378,17 +382,26 @@ protected function rolesSearch($user, $userDN)
}

$groupName = Util::DEFAULT_GROUP_NAME;
$ldap_connection = $this->ldapConnect();
$ldapConnection = $this->ldapConnect();

// replace the {0} placeholder with the username of the user
$this->roleFilter = preg_replace("/\{0\}/", "$user", $this->roleFilter);
// replace the {1} placeholder with the distiniguished name of the user
$this->roleFilter = preg_replace("/\{1\}/", "$userDN", $this->roleFilter);
$search = ldap_search($ldap_connection, $this->rolesDN, $this->roleFilter);
$entry = ldap_first_entry($ldap_connection, $search);

// search for the roles using the roleFilter and get the first entry
$search = ldap_search($ldapConnection, $this->rolesDN, $this->roleFilter);
$entry = ldap_first_entry($ldapConnection, $search);

do {
$dn = ldap_get_dn($ldap_connection, $entry);
// get the distinguished name of the entry and extract the common names out of it
$dn = ldap_get_dn($ldapConnection, $entry);
$roleArray = $this->extractCNFromDN($dn);
// add every returned CN to the roles
foreach ($roleArray as $role) {
$this->addRole($groupName, $role);
}
} while ($entry = ldap_next_entry($ldap_connection, $entry));
// continue as long as there are entries still left from the search
} while ($entry = ldap_next_entry($ldapConnection, $entry));
}
}

0 comments on commit acd6c66

Please sign in to comment.