Created
December 1, 2020 16:53
-
-
Save saber13812002/2f88cdb8bc75387ff5a698da7bc21ead to your computer and use it in GitHub Desktop.
bouncer from child department
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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