Skip to content

Instantly share code, notes, and snippets.

@saber13812002
Created December 1, 2020 16:53
Show Gist options
  • Save saber13812002/2f88cdb8bc75387ff5a698da7bc21ead to your computer and use it in GitHub Desktop.
Save saber13812002/2f88cdb8bc75387ff5a698da7bc21ead to your computer and use it in GitHub Desktop.
bouncer from child department
$user = Auth::user();
if ($user->can($ability, $modelOrItem)) {
return true;
}
$descendants = $user->departments()->with('descendants')->get();
foreach ($descendants as $descendant) {
if ($descendant->can($ability, $modelOrItem)) {
return true;
}
}
$unionAllChildsDepartments = self::unionOfDescendants($descendants);
foreach ($unionAllChildsDepartments as $childsDepartments) {
if ($childsDepartments->can($ability, $modelOrItem)) {
return true;
}
}
return false;
}
private static function unionOfDescendants($descendants)
{
$union = collect();
foreach ($descendants as $descendant) {
$union = $union->union($descendant->descendants);
}
return $union;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment