-<?php namespace BookStack\Auth\Access;
+<?php
+
+namespace BookStack\Auth\Access;
use BookStack\Auth\User;
use BookStack\Exceptions\JsonDebugException;
* Class LdapService
* Handles any app-specific LDAP tasks.
*/
-class LdapService extends ExternalAuthService
+class LdapService
{
-
protected $ldap;
+ protected $groupSyncService;
protected $ldapConnection;
protected $userAvatars;
protected $config;
/**
* LdapService constructor.
*/
- public function __construct(Ldap $ldap, UserAvatars $userAvatars)
+ public function __construct(Ldap $ldap, UserAvatars $userAvatars, GroupSyncService $groupSyncService)
{
$this->ldap = $ldap;
$this->userAvatars = $userAvatars;
+ $this->groupSyncService = $groupSyncService;
$this->config = config('services.ldap');
$this->enabled = config('auth.method') === 'ldap';
}
/**
* Check if groups should be synced.
- * @return bool
*/
- public function shouldSyncGroups()
+ public function shouldSyncGroups(): bool
{
return $this->enabled && $this->config['user_to_groups'] !== false;
}
/**
* 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;
+
+ return $this->getGroupsRecursive($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)
{
$userLdapGroups = $this->getUserGroups($username);
- $this->syncWithGroups($user, $userLdapGroups);
+ $this->groupSyncService->syncUserWithFoundGroups($user, $userLdapGroups, $this->config['remove_from_groups']);
}
/**