1 <?php namespace BookStack\Repos;
3 use BookStack\Services\LdapService;
9 protected $ldap = null;
10 protected $ldapService = null;
15 * LdapRepo constructor.
16 * @param \BookStack\Repos\UserRepo $userRepo
17 * @param LdapService $ldapService
19 public function __construct(UserRepo $userRepo, LdapService $ldapService)
21 $this->config = config('services.ldap');
23 if (config('auth.method') !== 'ldap') {
27 $this->ldapService = $ldapService;
28 $this->userRepo = $userRepo;
32 * If there is no ldap connection, all methods calls to this library will return null
34 public function __call($method, $arguments)
36 if ($this->ldap === null) {
40 return call_user_func_array(array($this,$method), $arguments);
44 * Sync the LDAP groups to the user roles for the current user
45 * @param \BookStack\User $user
46 * @param string $userName
47 * @throws \BookStack\Exceptions\NotFoundException
49 public function syncGroups($user, $userName)
51 $userLdapGroups = $this->ldapService->getUserGroups($userName);
52 $userLdapGroups = $this->groupNameFilter($userLdapGroups);
53 // get the ids for the roles from the names
54 $ldapGroupsAsRoles = Role::whereIn('name', $userLdapGroups)->pluck('id');
56 if ($this->config['remove_from_groups']) {
57 $user->roles()->sync($ldapGroupsAsRoles);
58 $this->userRepo->attachDefaultRole($user);
60 $user->roles()->syncWithoutDetaching($ldapGroupsAsRoles);
63 // make the user an admin?
64 if (in_array($this->config['admin'], $userLdapGroups)) {
65 $this->userRepo->attachSystemRole($user, 'admin');
70 * Filter to convert the groups from ldap to the format of the roles name on BookStack
71 * Spaces replaced with -, all lowercase letters
72 * @param array $groups
75 private function groupNameFilter($groups)
78 foreach ($groups as $groupName) {
79 $return[] = str_replace(' ', '-', strtolower($groupName));