Skip to content

Commit

Permalink
Update files to PSR-2 standards
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanmurphy committed Jul 2, 2018
1 parent d640cc1 commit 37aa8b0
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 331 deletions.
12 changes: 6 additions & 6 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ protected function authenticated(Request $request, Authenticatable $user)
auth()->login($user);
}

// ldap groups refresh
if (config('services.ldap.user_to_groups') !== false && $request->filled('username')) {
$ldapRepo = new LdapRepo($this->userRepo);
$ldapRepo->syncGroups($user,$request->input('username'));
}
// ldap groups refresh
if (config('services.ldap.user_to_groups') !== false && $request->filled('username')) {
$ldapRepo = new LdapRepo($this->userRepo);
$ldapRepo->syncGroups($user, $request->input('username'));
}


$path = session()->pull('url.intended', '/');
$path = session()->pull('url.intended', '/');
$path = baseUrl($path, true);
return redirect($path);
}
Expand Down
130 changes: 65 additions & 65 deletions app/Repos/LdapRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,77 @@
class LdapRepo
{

protected $ldap = null;
protected $ldapService = null;
protected $ldap = null;
protected $ldapService = null;

protected $config;
protected $config;

/**
* LdapRepo constructor.
* @param \BookStack\Repos\UserRepo $userRepo
*/
public function __construct(UserRepo $userRepo)
{
$this->config = config('services.ldap');
/**
* LdapRepo constructor.
* @param \BookStack\Repos\UserRepo $userRepo
*/
public function __construct(UserRepo $userRepo)
{
$this->config = config('services.ldap');

if (config('auth.method') !== 'ldap') {
return false;
}
if (config('auth.method') !== 'ldap') {
return false;
}

$this->ldapService = new LdapService(new Ldap);
$this->userRepo = $userRepo;
}
$this->ldapService = new LdapService(new Ldap);
$this->userRepo = $userRepo;
}

/**
* If there is no ldap connection, all methods calls to this library will return null
*/
public function __call($method, $arguments)
{
if ($this->ldap === null) {
return null;
}
/**
* If there is no ldap connection, all methods calls to this library will return null
*/
public function __call($method, $arguments)
{
if ($this->ldap === null) {
return null;
}

return call_user_func_array(array($this,$method),$arguments);
}
return call_user_func_array(array($this,$method), $arguments);
}

/**
* Sync the LDAP groups to the user roles for the current user
* @param \BookStack\User $user
* @param string $userName
* @throws \BookStack\Exceptions\NotFoundException
*/
public function syncGroups($user,$userName)
{
$userLdapGroups = $this->ldapService->getUserGroups($userName);
$userLdapGroups = $this->groupNameFilter($userLdapGroups);
// get the ids for the roles from the names
$ldapGroupsAsRoles = Role::whereIn('name',$userLdapGroups)->pluck('id');
// sync groups
if ($this->config['remove_from_groups']) {
$user->roles()->sync($ldapGroupsAsRoles);
$this->userRepo->attachDefaultRole($user);
} else {
$user->roles()->syncWithoutDetaching($ldapGroupsAsRoles);
}
/**
* Sync the LDAP groups to the user roles for the current user
* @param \BookStack\User $user
* @param string $userName
* @throws \BookStack\Exceptions\NotFoundException
*/
public function syncGroups($user, $userName)
{
$userLdapGroups = $this->ldapService->getUserGroups($userName);
$userLdapGroups = $this->groupNameFilter($userLdapGroups);
// get the ids for the roles from the names
$ldapGroupsAsRoles = Role::whereIn('name', $userLdapGroups)->pluck('id');
// sync groups
if ($this->config['remove_from_groups']) {
$user->roles()->sync($ldapGroupsAsRoles);
$this->userRepo->attachDefaultRole($user);
} else {
$user->roles()->syncWithoutDetaching($ldapGroupsAsRoles);
}

// make the user an admin?
if (in_array($this->config['admin'],$userLdapGroups)) {
$this->userRepo->attachSystemRole($user,'admin');
}
}
// make the user an admin?
if (in_array($this->config['admin'], $userLdapGroups)) {
$this->userRepo->attachSystemRole($user, 'admin');
}
}

/**
* Filter to convert the groups from ldap to the format of the roles name on BookStack
* Spaces replaced with -, all lowercase letters
* @param array $groups
* @return array
*/
private function groupNameFilter($groups)
{
$return = [];
foreach ($groups as $groupName) {
$return[] = str_replace(' ', '-', strtolower($groupName));
}
return $return;
}
}
/**
* Filter to convert the groups from ldap to the format of the roles name on BookStack
* Spaces replaced with -, all lowercase letters
* @param array $groups
* @return array
*/
private function groupNameFilter($groups)
{
$return = [];
foreach ($groups as $groupName) {
$return[] = str_replace(' ', '-', strtolower($groupName));
}
return $return;
}
}
Loading

0 comments on commit 37aa8b0

Please sign in to comment.