-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mails): reduce duplicated entries on mails module
- Loading branch information
Showing
6 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015, 2016, 2017, 2018, 2019 Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Web\Http\DataTables\Scopes; | ||
|
||
use Illuminate\Support\Arr; | ||
use Yajra\DataTables\Contracts\DataTableScope; | ||
|
||
/** | ||
* Class CharacterMailScope. | ||
* | ||
* This is a specific scope for character mails table. | ||
* It will restrict returned data based on both requested character ID and granted access. | ||
* | ||
* @package Seat\Web\Http\DataTables\Scopes | ||
*/ | ||
class CharacterMailScope implements DataTableScope | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $character_ids = []; | ||
|
||
/** | ||
* CharacterMailScope constructor. | ||
* | ||
* @param int $character_id | ||
* @param array|null $character_ids | ||
*/ | ||
public function __construct(int $character_id, ?array $character_ids) | ||
{ | ||
if (is_null($character_ids)) | ||
$character_ids = []; | ||
|
||
$this->character_ids = array_merge([$character_id], $character_ids); | ||
} | ||
|
||
/** | ||
* Apply a query scope. | ||
* | ||
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query | ||
* @return mixed | ||
*/ | ||
public function apply($query) | ||
{ | ||
return $query->whereHas('recipients', function ($sub_query) { | ||
|
||
$character_ids = []; | ||
$map = Arr::get(auth()->user()->getAffiliationMap(), 'char'); | ||
|
||
// in case user is super, apply filter over all requested characters | ||
if (auth()->user()->hasSuperUser()) { | ||
$character_ids = $this->character_ids; | ||
} | ||
|
||
// otherwise, determine to which character the user has access and include them in applied filters | ||
// reject each other | ||
if (empty($character_ids)) { | ||
foreach ($this->character_ids as $character_id) { | ||
if (in_array(Arr::has($map, (int) $character_id), ['character.*', 'character.mail'])) | ||
$character_ids[] = (int) $character_id; | ||
} | ||
} | ||
|
||
return $sub_query->whereIn('recipient_id', $character_ids); | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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