-<?php namespace BookStack\Auth\Access;
+<?php
+
+namespace BookStack\Auth\Access;
use BookStack\Auth\User;
use BookStack\Exceptions\JsonDebugException;
*/
class LdapService extends ExternalAuthService
{
-
protected $ldap;
protected $ldapConnection;
protected $userAvatars;
/**
* Check if groups should be synced.
+ *
* @return bool
*/
public function shouldSyncGroups()
/**
* Search for attributes for a specific user on the ldap.
+ *
* @throws LdapException
*/
private function getUserWithAttributes(string $userName, array $attributes): ?array
/**
* Get the details of a user from LDAP using the given username.
* User found via configurable user filter.
+ *
* @throws LdapException
*/
public function getUserDetails(string $userName): ?array
$userCn = $this->getUserResponseProperty($user, 'cn', null);
$formatted = [
- 'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
- 'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
- 'dn' => $user['dn'],
+ 'uid' => $this->getUserResponseProperty($user, $idAttr, $user['dn']),
+ 'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
+ 'dn' => $user['dn'],
'email' => $this->getUserResponseProperty($user, $emailAttr, null),
'avatar'=> $thumbnailAttr ? $this->getUserResponseProperty($user, $thumbnailAttr, null) : null,
];
if ($this->config['dump_user_details']) {
throw new JsonDebugException([
- 'details_from_ldap' => $user,
+ 'details_from_ldap' => $user,
'details_bookstack_parsed' => $formatted,
]);
}
/**
* Check if the given credentials are valid for the given user.
+ *
* @throws LdapException
*/
public function validateUserCredentials(?array $ldapUserDetails, string $password): bool
}
$ldapConnection = $this->getConnection();
+
try {
$ldapBind = $this->ldap->bind($ldapConnection, $ldapUserDetails['dn'], $password);
} catch (ErrorException $e) {
/**
* Bind the system user to the LDAP connection using the given credentials
* otherwise anonymous access is attempted.
+ *
* @param $connection
+ *
* @throws LdapException
*/
protected function bindSystemUser($connection)
/**
* Get the connection to the LDAP server.
* Creates a new connection if one does not exist.
- * @return resource
+ *
* @throws LdapException
+ *
+ * @return resource
*/
protected function getConnection()
{
}
$this->ldapConnection = $ldapConnection;
+
return $this->ldapConnection;
}
// Otherwise, extract the port out
$hostName = $serverNameParts[0];
$ldapPort = (count($serverNameParts) > 1) ? intval($serverNameParts[1]) : 389;
+
return ['host' => $hostName, 'port' => $ldapPort];
}
$newKey = '${' . $key . '}';
$newAttrs[$newKey] = $this->ldap->escape($attrText);
}
+
return strtr($filterString, $newAttrs);
}
/**
* Get the groups a user is a part of on ldap.
+ *
* @throws LdapException
*/
public function getUserGroups(string $userName): array
$userGroups = $this->groupFilter($user);
$userGroups = $this->getGroupsRecursive($userGroups, []);
+
return $userGroups;
}
/**
* Get the parent groups of an array of groups.
+ *
* @throws LdapException
*/
private function getGroupsRecursive(array $groupsArray, array $checked): array
/**
* Get the parent groups of a single group.
+ *
* @throws LdapException
*/
private function getGroupGroups(string $groupName): array
$count = 0;
if (isset($userGroupSearchResponse[$groupsAttr]['count'])) {
- $count = (int)$userGroupSearchResponse[$groupsAttr]['count'];
+ $count = (int) $userGroupSearchResponse[$groupsAttr]['count'];
}
for ($i = 0; $i < $count; $i++) {
/**
* Sync the LDAP groups to the user roles for the current user.
+ *
* @throws LdapException
*/
public function syncGroups(User $user, string $username)