diff --git a/src/Repositories/Character/Mail.php b/src/Repositories/Character/Mail.php index 91418711..68408222 100644 --- a/src/Repositories/Character/Mail.php +++ b/src/Repositories/Character/Mail.php @@ -22,6 +22,7 @@ namespace Seat\Services\Repositories\Character; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Seat\Eveapi\Models\Mail\MailHeader; @@ -127,21 +128,22 @@ public function getCharacterMailTimeline(int $message_id = null) // Get the User for permissions and affiliation // checks $user = auth()->user(); - $messages = MailHeader::with('recipients', 'body'); + $messages = MailHeader::with('body', 'recipients', 'recipients.entity', 'sender'); // If a user is not a super user, only return their own mail and those // which they are affiliated to to receive. if (! $user->hasSuperUser()) { - $messages = $messages->where(function ($query) use ($user) { + $messages = $messages->whereHas('recipients', function ($sub_query) { + // retrieve authenticated user permissions map + $character_map = collect(Arr::get(auth()->user()->getAffiliationMap(), 'char')); - // If the user has any affiliations and can - // list those characters, add them - if ($user->has('character.mail', false)) - $query = $query->whereIn('character_id', array_keys($user->getAffiliationMap()['char'])); + // collect only character which has either the requested permission or wildcard + $characters_ids = $character_map->filter(function ($permissions, $key) { + return in_array('character.*', $permissions) || in_array('character.mail', $permissions); + })->keys(); - // Add mail owned by *this* character - $query->orWhere('character_id', $user->id); + $sub_query->whereIn('recipient_id', $characters_ids); }); } @@ -150,7 +152,7 @@ public function getCharacterMailTimeline(int $message_id = null) return $messages->where('mail_id', $message_id) ->first(); - return $messages->select('mail_id', 'subject', 'from', 'timestamp', 'labels', 'is_read') + return $messages->select('mail_id', 'subject', 'from', 'timestamp') ->orderBy('timestamp', 'desc') ->distinct() ->paginate(25); diff --git a/src/Search/Search.php b/src/Search/Search.php index e49cdc47..813dffb1 100644 --- a/src/Search/Search.php +++ b/src/Search/Search.php @@ -22,6 +22,7 @@ namespace Seat\Services\Search; +use Illuminate\Support\Arr; use Seat\Eveapi\Models\Assets\CharacterAsset; use Seat\Eveapi\Models\Character\CharacterSkill; use Seat\Eveapi\Models\Mail\MailHeader; @@ -64,34 +65,22 @@ public function doSearchCharacterMail() // checks $user = auth()->user(); - $messages = MailHeader::with('body', 'recipients', 'sender', 'character') - ->select('timestamp', 'from', 'subject', 'mail_headers.mail_id', 'character_id'); + $messages = MailHeader::with('body', 'recipients', 'recipients.entity', 'sender') + ->select('timestamp', 'from', 'subject', 'mail_headers.mail_id'); // If the user is a super user, return all if (! $user->hasSuperUser()) { - $messages = $messages->where(function ($query) use ($user) { + $messages = $messages->whereHas('recipients', function ($sub_query) { + // retrieve authenticated user permissions map + $character_map = collect(Arr::get(auth()->user()->getAffiliationMap(), 'char')); - // If the user has any affiliations and can - // list those characters, add them - // also include all attached characters + // collect only character which has either the requested permission or wildcard + $characters_ids = $character_map->filter(function ($permissions, $key) { + return in_array('character.*', $permissions) || in_array('character.mail', $permissions); + })->keys(); - $map = $user->getAffiliationMap(); - $character_maps = []; - - foreach ($map['char'] as $character_id => $permissions) { - if (in_array('character.*', $permissions)) - $character_maps[] = $character_id; - if (in_array('character.mail', $permissions)) - $character_maps[] = $character_id; - } - - $query = $query->orWhereIn('character_id', $character_maps) - ->orWhereIn('from', $character_maps); - - $query = $query->orWhereHas('recipients', function ($sub_query) use ($character_maps) { - $sub_query->whereIn('recipient_id', $character_maps); - }); + $sub_query->whereIn('recipient_id', $characters_ids); }); }