@php /* | What this account is allowed to do. | | Off by default and the roles decide, exactly as before. Switched on and | the ticks below are the whole answer — not additions to the role, the | answer — which is what lets two receptionists be allowed different | things without either of them needing a role of their own. | | Prefilled from the roles when the account has no list yet, so turning it | on starts from what the account can do today rather than from nothing. | Somebody trimming three permissions off a receptionist should not have to | tick the other twenty-two first. */ $hasOwnSet = old('permissions_overridden', ($user ?? null)?->hasOwnPermissionSet() ?? false); $roleGranted = collect($selectedRoles) ->map(fn (string $name) => $availableRoles->firstWhere('name', $name)) ->filter() ->flatMap(fn ($role) => $role->permissions->pluck('name')) ->unique() ->values() ->all(); $ticked = old('permissions', $hasOwnSet && isset($user) && $user->hasOwnPermissionSet() ? $user->permissions->pluck('name')->all() : $roleGranted); @endphp