Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-staab committed Jun 25, 2024
1 parent 06bc994 commit 4567288
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Api/Committees.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private function prepareData(callable $filter = null) : array
foreach ($committeeDns as $committeeDn){
$committees->add(Committee::find($committeeDn));
}
// returns array of committees like "stura" => "Studierendenrat"
// FIXME: has issues with all() -> not distinguishable in multi realm setup
return $committees
->keyBy(function ($item){
// change key
Expand Down
20 changes: 13 additions & 7 deletions app/Http/Controllers/Api/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ class Groups extends Controller
{
public function all(Request $request)
{
$committees = $this->prepareData(function (string $dn){
// no filter applied
$groups = $this->prepareData(function (string $dn){
return true;
});
return response()->json($committees);
return response()->json($groups);
}

public function fromCommunity(Request $request, string $community_uid)
{
$committees = $this->prepareData(function (string $dn) use ($community_uid){
// only one specific community as filter
$groups = $this->prepareData(function (string $dn) use ($community_uid){
return str_contains($dn, "ou=Committees,ou=$community_uid");
});
return response()->json($committees);
return response()->json($groups);
}

/**
* Returns all Group memberships (not roles) as array, can be filtered
* @param callable $filter the filter which should be applied to the collected result
* @return array the fetched groups
*/
private function prepareData(callable $filter) : array
{
/** @var User $ldapUser */
Expand All @@ -41,14 +48,13 @@ private function prepareData(callable $filter) : array
}
$groups = $groupsQuery->get();

$groupDns = $groups->map(function ($item){
// returns the (filtered) group DNs as array
return $groups->map(function ($item){
return $item->getDn();
})->reject(function (string $dn){
// throw out the committee roles. only memberships and permissions inside
return str_contains($dn, 'ou=Committees');
})->filter($filter)
->toArray();

return $groupDns;
}
}

0 comments on commit 4567288

Please sign in to comment.