Skip to content

Commit

Permalink
refactor(acl): switch to gates
Browse files Browse the repository at this point in the history
  • Loading branch information
warlof committed Aug 16, 2020
1 parent 062bffb commit 3312c75
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/Http/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getFaded()
*/
public function getDetail($operation_id)
{
if (auth()->user()->has('calendar.view', false)) {
if (auth()->user()->can('calendar.view', false)) {
$op = Operation::with('tags')->find($operation_id);
return view('calendar::operation.modals/details.content', compact('op'));
}
Expand Down
14 changes: 8 additions & 6 deletions src/Http/Controllers/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Seat\Kassie\Calendar\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Eveapi\Models\Sde\InvType;
use Seat\Kassie\Calendar\Models\Pap;
use Seat\Web\Http\Controllers\Controller;
Expand All @@ -20,23 +21,24 @@
class CharacterController extends Controller
{
/**
* @param $character_id
* @param \Seat\Eveapi\Models\Character\CharacterInfo $character
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function paps($character_id)
public function paps(CharacterInfo $character)
{
$today = carbon();

$monthlyPaps = Pap::where('character_id', $character_id)
$monthlyPaps = Pap::where('character_id', $character->character_id)
->select('character_id', 'year', 'month', DB::raw('sum(value) as qty'))
->groupBy('character_id', 'year', 'month')
->get();

$shipTypePaps = InvType::rightJoin('invGroups', 'invGroups.groupID', '=', 'invTypes.groupID')
->leftJoin('kassie_calendar_paps', 'ship_type_id', '=', 'typeID')
->where('categoryID', 6)
->where(function($query) use ($character_id) {
$query->where('character_id', $character_id)
->where(function($query) use ($character) {
$query->where('character_id', $character->character_id)
->orWhere('character_id', null);
})
->select('invGroups.groupID', 'categoryID', 'groupName', DB::raw('sum(value) as qty'))
Expand Down Expand Up @@ -66,6 +68,6 @@ public function paps($character_id)
->get();

return view('calendar::character.paps', compact('monthlyPaps', 'shipTypePaps',
'weeklyRanking', 'monthlyRanking', 'yearlyRanking', 'character_id'));
'weeklyRanking', 'monthlyRanking', 'yearlyRanking', 'character'));
}
}
22 changes: 12 additions & 10 deletions src/Http/Controllers/CorporationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Seat\Kassie\Calendar\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Seat\Eveapi\Models\Corporation\CorporationInfo;
use Seat\Kassie\Calendar\Models\Pap;
use Seat\Web\Http\Controllers\Controller;

Expand All @@ -19,16 +20,17 @@
class CorporationController extends Controller
{
/**
* @param int $corporation_id
* @param \Seat\Eveapi\Models\Corporation\CorporationInfo $corporation
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getPaps(int $corporation_id)
public function getPaps(CorporationInfo $corporation)
{
$today = carbon();

$weeklyRanking = Pap::with('character', 'character.affiliation')
->whereHas('character.affiliation', function ($query) use ($corporation_id) {
$query->where('corporation_id', $corporation_id);
->whereHas('character.affiliation', function ($query) use ($corporation) {
$query->where('corporation_id', $corporation->corporation_id);
})
->where('week', $today->weekOfMonth)
->where('month', $today->month)
Expand All @@ -40,8 +42,8 @@ public function getPaps(int $corporation_id)
->get();

$monthlyRanking = Pap::with('character', 'character.affiliation')
->whereHas('character.affiliation', function ($query) use ($corporation_id) {
$query->where('corporation_id', $corporation_id);
->whereHas('character.affiliation', function ($query) use ($corporation) {
$query->where('corporation_id', $corporation->corporation_id);
})
->where('month', $today->month)
->where('year', $today->year)
Expand All @@ -52,8 +54,8 @@ public function getPaps(int $corporation_id)
->get();

$yearlyRanking = Pap::with('character', 'character.affiliation')
->whereHas('character.affiliation', function ($query) use ($corporation_id) {
$query->where('corporation_id', $corporation_id);
->whereHas('character.affiliation', function ($query) use ($corporation) {
$query->where('corporation_id', $corporation->corporation_id);
})
->where('year', $today->year)
->select('character_id')
Expand All @@ -62,7 +64,7 @@ public function getPaps(int $corporation_id)
->orderBy('qty', 'desc')
->get();

return view('calendar::corporation.paps', compact('weeklyRanking', 'monthlyRanking', 'yearlyRanking'));
return view('calendar::corporation.paps', compact('weeklyRanking', 'monthlyRanking', 'yearlyRanking', 'corporation'));
}

/**
Expand Down Expand Up @@ -140,7 +142,7 @@ public function getYearPapsStats(int $corporation_id)
public function getMonthlyStackedPapsStats(int $corporation_id)
{
$year = is_null(request()->query('year')) ? carbon()->year : intval(request()->query('year'));
$month = is_null(request()->query('month')) ? carbon()->year : intval(request()->query('month'));
$month = is_null(request()->query('month')) ? carbon()->month : intval(request()->query('month'));
$grouped = request()->query('grouped') ?: false;

$paps = Pap::select('ci.character_id', 'cto.operation_id', 'analytics', 'value')
Expand Down
16 changes: 8 additions & 8 deletions src/Http/Controllers/OperationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class OperationController extends Controller
* OperationController constructor.
*/
public function __construct() {
$this->middleware('bouncer:calendar.view')->only('index');
$this->middleware('bouncer:calendar.create')->only('store');
$this->middleware('can:calendar.view')->only('index');
$this->middleware('can:calendar.create')->only('store');
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ public function update(Request $request)
$operation = Operation::find($request->operation_id);
$tags = array();

if (auth()->user()->has('calendar.update_all', false) || $operation->user->id == auth()->user()->id) {
if (auth()->user()->can('calendar.update_all', false) || $operation->user->id == auth()->user()->id) {

foreach ($request->toArray() as $name => $value) {
if (empty($value))
Expand Down Expand Up @@ -181,7 +181,7 @@ public function update(Request $request)
public function delete(Request $request)
{
$operation = Operation::find($request->operation_id);
if (auth()->user()->has('calendar.delete_all', false) || $operation->user->id == auth()->user()->id) {
if (auth()->user()->can('calendar.delete_all', false) || $operation->user->id == auth()->user()->id) {
if ($operation != null) {

if (! $operation->isUserGranted(auth()->user()))
Expand All @@ -204,7 +204,7 @@ public function delete(Request $request)
public function close(Request $request)
{
$operation = Operation::find($request->operation_id);
if (auth()->user()->has('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {
if (auth()->user()->can('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {

if ($operation != null) {
$operation->end_at = Carbon::now('UTC');
Expand All @@ -225,7 +225,7 @@ public function close(Request $request)
public function cancel(Request $request)
{
$operation = Operation::find($request->operation_id);
if (auth()->user()->has('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {
if (auth()->user()->can('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {
if ($operation != null) {

$operation->timestamps = false;
Expand All @@ -250,7 +250,7 @@ public function cancel(Request $request)
public function activate(Request $request)
{
$operation = Operation::find($request->operation_id);
if (auth()->user()->has('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {
if (auth()->user()->can('calendar.close_all', false) || $operation->user->id == auth()->user()->id) {
if ($operation != null) {
$operation->timestamps = false;
$operation->is_cancelled = false;
Expand Down Expand Up @@ -306,7 +306,7 @@ public function subscribe(Request $request)
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function find($operation_id) {
if (auth()->user()->has('calendar.view', false)) {
if (auth()->user()->can('calendar.view', false)) {
$operation = Operation::find($operation_id)->load('tags');

if (! $operation->isUserGranted(auth()->user()))
Expand Down
24 changes: 12 additions & 12 deletions src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
'prefix' => 'character',
], function() {

Route::get('/{character_id}/paps', [
Route::get('/{character}/paps', [
'as' => 'character.view.paps',
'uses' => 'CharacterController@paps',
'middleware' => 'characterbouncer:kassie_calendar_paps',
'middleware' => 'can:character.kassie_calendar_paps,character',
]);

});
Expand All @@ -20,29 +20,29 @@
'prefix' => 'corporation',
], function() {

Route::get('/{corporation_id}/paps', [
Route::get('/{corporation}/paps', [
'as' => 'corporation.view.paps',
'uses' => 'CorporationController@getPaps',
'middleware' => 'corporationbouncer:kassie_calendar_paps',
'middleware' => 'can:corporation.kassie_calendar_paps,corporation',
]);

Route::get('/{corporation_id}/paps/json/year', [
Route::get('/{corporation}/paps/json/year', [
'as' => 'corporation.ajax.paps.year',
'uses' => 'CorporationController@getYearPapsStats',
'middleware' => 'corporationbouncer:kassie_calendar_paps',
'middleware' => 'can:corporation.kassie_calendar_paps,corporation',
]);

Route::get('/{corporation_id}/paps/json/stacked', [
Route::get('/{corporation}/paps/json/stacked', [
'as' => 'corporation.ajax.paps.stacked',
'uses' => 'CorporationController@getMonthlyStackedPapsStats',
'middleware' => 'corporationbouncer:kassie_calendar_paps',
'middleware' => 'can:corporation.kassie_calendar_paps,corporation',
]);

});

Route::group([
'namespace' => 'Seat\Kassie\Calendar\Http\Controllers',
'middleware' => ['web', 'auth', 'locale', 'bouncer:calendar.view'],
'middleware' => ['web', 'auth', 'locale', 'can:calendar.view'],
'prefix' => 'calendar'
], function () {

Expand Down Expand Up @@ -83,7 +83,7 @@
Route::post('/', [
'as' => 'operation.store',
'uses' => 'OperationController@store',
'middleware' => 'bouncer:calendar.create'
'middleware' => 'can:calendar.create'
]);

Route::post('update', [
Expand Down Expand Up @@ -129,7 +129,7 @@

Route::group([
'prefix' => 'setting',
'middleware' => 'bouncer:calendar.setup'
'middleware' => 'can:calendar.setup'
], function() {

Route::get('/', [
Expand Down Expand Up @@ -166,7 +166,7 @@
Route::get('show/{id}', [
'as' => 'tags.show',
'uses' => 'TagController@get',
'middleware' => 'bouncer:calendar.setup',
'middleware' => 'can:calendar.setup',
]);

Route::post('update', [
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getIsFleetCommanderAttribute()
if ($this->fc_character_id == null)
return false;

return in_array($this->fc_character_id, auth()->user()->associatedCharacterIds()->toArray());
return in_array($this->fc_character_id, auth()->user()->associatedCharacterIds());
}

/**
Expand Down
26 changes: 13 additions & 13 deletions src/resources/views/character/paps.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
</tr>
@endforelse
</tbody>
@if(! $weeklyRanking->where('character_id', $character_id)->isEmpty())
@if(! $weeklyRanking->where('character_id', $character->character_id)->isEmpty())
<tfoot class="hidden">
<tr>
<td>{{ $weeklyRanking->where('character_id', $character_id)->keys()->first() + 1 }}.</td>
<td>{{ $weeklyRanking->where('character_id', $character->character_id)->keys()->first() + 1 }}.</td>
<td>
@include('web::partials.character', ['character' => $weeklyRanking->where('character_id', $character_id)->first()->character])
@include('web::partials.character', ['character' => $weeklyRanking->where('character_id', $character->character_id)->first()->character])
</td>
<td>{{ $weeklyRanking->where('character_id', $character_id)->first()->qty }}</td>
<td>{{ $weeklyRanking->where('character_id', $character->character_id)->first()->qty }}</td>
</tr>
</tfoot>
@endif
Expand Down Expand Up @@ -84,14 +84,14 @@
</tr>
@endforelse
</tbody>
@if(! $monthlyRanking->where('character_id', $character_id)->isEmpty())
@if(! $monthlyRanking->where('character_id', $character->character_id)->isEmpty())
<tfoot class="hidden">
<tr>
<td>{{ $monthlyRanking->where('character_id', $character_id)->keys()->first() + 1 }}.</td>
<td>{{ $monthlyRanking->where('character_id', $character->character_id)->keys()->first() + 1 }}.</td>
<td>
@include('web::partials.character', ['character' => $monthlyRanking->where('character_id', $character_id)->first()->character])
@include('web::partials.character', ['character' => $monthlyRanking->where('character_id', $character->character_id)->first()->character])
</td>
<td>{{ $monthlyRanking->where('character_id', $character_id)->first()->qty }}</td>
<td>{{ $monthlyRanking->where('character_id', $character->character_id)->first()->qty }}</td>
</tr>
</tfoot>
@endif
Expand Down Expand Up @@ -122,14 +122,14 @@
</tr>
@endforelse
</tbody>
@if(! $yearlyRanking->where('character_id', $character_id)->isEmpty())
@if(! $yearlyRanking->where('character_id', $character->character_id)->isEmpty())
<tfoot class="hidden">
<tr>
<td>{{ $yearlyRanking->where('character_id', $character_id)->keys()->first() + 1 }}.</td>
<td>{{ $yearlyRanking->where('character_id', $character->character_id)->keys()->first() + 1 }}.</td>
<td>
@include('web::partials.character', ['character' => $yearlyRanking->where('character_id', $character_id)->first()->character])
@include('web::partials.character', ['character' => $yearlyRanking->where('character_id', $character->character_id)->first()->character])
</td>
<td>{{ $yearlyRanking->where('character_id', $character_id)->first()->qty }}</td>
<td>{{ $yearlyRanking->where('character_id', $character->character_id)->first()->qty }}</td>
</tr>
</tfoot>
@endif
Expand Down Expand Up @@ -237,7 +237,7 @@
var found = false;
var children = $(this).find('tr');
children.each(function(){
if ($(this).attr('data-attr') == {{ $character_id }}) {
if ($(this).attr('data-attr') == {{ $character->character_id }}) {
$(this).addClass('bg-' + getActiveThemeColor() + '-gradient');
found = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/resources/views/corporation/paps.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
yearChartParameters.find('button').on('click', function(){
$.ajax({
url: '{{ route('corporation.ajax.paps.year', request()->route('corporation_id')) }}',
url: '{{ route('corporation.ajax.paps.year', request()->route('corporation')) }}',
data: {
year: yearChartParameters.find('input[type="text"]').val(),
grouped: yearChartParameters.find('input[type="checkbox"]').is(':checked') ? 1 : 0
Expand Down Expand Up @@ -299,7 +299,7 @@
monthChartParameters.find('button').on('click', function(){
$.ajax({
url: '{{ route('corporation.ajax.paps.stacked', request()->route('corporation_id')) }}',
url: '{{ route('corporation.ajax.paps.stacked', request()->route('corporation')) }}',
data: {
year: monthChartParameters.find('input[name="year"]').val(),
month: monthChartParameters.find('select[name="month"]').val(),
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/operation/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@section('full')

@if(auth()->user()->has('calendar.create', false))
@if(auth()->user()->can('calendar.create', false))
<div class="row margin-bottom">
<div class="col-md-offset-8 col-md-4">
<div class="pull-right">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
@if(! $op->is_cancelled)
@include('calendar::operation.partials.actions.subscribe')
@endif
@if(auth()->user()->has('calendar.update_all', false) || $op->user->id == auth()->user()->id)
@if(auth()->user()->can('calendar.update_all', false) || $op->user->id == auth()->user()->id)
@include('calendar::operation.partials.actions.edit')
@endif
@endif

@if(carbon()->now()->gt($op->start_at) && in_array($op->end_at, [null, carbon()->now()]))
@if(auth()->user()->has('calendar.close_all', false) || $op->user->id == auth()->user()->id)
@if(auth()->user()->can('calendar.close_all', false) || $op->user->id == auth()->user()->id)
@include('calendar::operation.partials.actions.close')
@endif
@endif

@if(auth()->user()->has('calendar.cancel_all', false) || $op->user->id == auth()->user()->id)
@if(auth()->user()->can('calendar.cancel_all', false) || $op->user->id == auth()->user()->id)
@if($op->is_cancelled)
@include('calendar::operation.partials.actions.enable')
@else
Expand All @@ -25,6 +25,6 @@
@endif
@endif

@if(auth()->user()->has('calendar.delete_all', false) || $op->user->id == auth()->user()->id)
@if(auth()->user()->can('calendar.delete_all', false) || $op->user->id == auth()->user()->id)
@include('calendar::operation.partials.actions.destroy')
@endif

0 comments on commit 3312c75

Please sign in to comment.