Skip to content

Commit

Permalink
Merge pull request #914 from owlchester/character-race-filters
Browse files Browse the repository at this point in the history
Fixed filters no filtering properly on is_private
  • Loading branch information
ilestis authored Jul 11, 2024
2 parents f71ad8e + 990f7c0 commit eec9a97
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/Cleanup/CleanupTrashedCampaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle()
Campaign::observe(CampaignObserver::class);

$count = $this->service->purgeDeleted();
$log = 'Deleted ' . $count . ' trashed campaigns.';
$log = 'Deleted ' . $count . ' trashed campaigns.';
$this->info($log);
$this->log($log);

Expand Down
36 changes: 18 additions & 18 deletions app/Http/Controllers/Entity/MoveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ public function move(MoveEntityRequest $request, Campaign $campaign, Entity $ent
}

$copied = $request->filled('copy');
// try {
$this->service
->entity($entity)
->campaign($campaign)
->user($request->user())
->to($request->get('campaign'))
->copy($copied)
->validate()
->process()
;
// try {
$this->service
->entity($entity)
->campaign($campaign)
->user($request->user())
->to($request->get('campaign'))
->copy($copied)
->validate()
->process()
;

return redirect()
->route($entity->pluralType() . '.index', $campaign)
->with('success_raw', __('entities/move.success' . ($copied ? '_copy' : null), ['name' => $entity->name, 'campaign' => $this->service->target()->name]));
// } catch (TranslatableException $ex) {
// return redirect()
// ->to($entity->url())
// ->with('error', __($ex->getMessage(), ['name' => $entity->name]));
// }
return redirect()
->route($entity->pluralType() . '.index', $campaign)
->with('success_raw', __('entities/move.success' . ($copied ? '_copy' : null), ['name' => $entity->name, 'campaign' => $this->service->target()->name]));
// } catch (TranslatableException $ex) {
// return redirect()
// ->to($entity->url())
// ->with('error', __($ex->getMessage(), ['name' => $entity->name]));
// }
}
}
6 changes: 3 additions & 3 deletions app/Http/Requests/StoreCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function rules()
'show_birthdays' => 'boolean',
'template_id' => 'nullable',
'format' => ['nullable', new CalendarFormat(), 'string', 'max:20'],
// 'moon_offset' => [
// '*' => new CalendarMoonOffset()
// ],
// 'moon_offset' => [
// '*' => new CalendarMoonOffset()
// ],
];

if (request()->has('quick-creator')) {
Expand Down
20 changes: 16 additions & 4 deletions app/Models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,22 @@ public function scopeMember(Builder $query, string|null $value, FilterOption $fi
if (!empty($value)) {
return $query;
}
return $query
$query
->select($this->getTable() . '.*')
->leftJoin('organisation_member as memb', function ($join) {
$join->on('memb.character_id', '=', $this->getTable() . '.id');
})
->where('memb.organisation_id', null);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query;
} elseif ($filter === FilterOption::EXCLUDE) {
return $query
->whereRaw('(select count(*) from organisation_member as memb where memb.character_id = ' .
$this->getTable() . '.id and memb.organisation_id in (' . (int) $value . ')) = 0');
$this->getTable() . '.id and memb.character_id = ' . ((int) $value) . ' and ' . $this->subPrivacy('memb.is_private') . ') = 0');
}

$ids = [$value];
Expand All @@ -199,12 +205,18 @@ public function scopeMember(Builder $query, string|null $value, FilterOption $fi
$ids = [...$model->descendants->pluck('id')->toArray(), $model->id];
}
}
return $query
$query
->select($this->getTable() . '.*')
->leftJoin('organisation_member as memb', function ($join) {
$join->on('memb.character_id', '=', $this->getTable() . '.id');
})
->whereIn('memb.organisation_id', $ids)->distinct();
->whereIn('memb.organisation_id', $ids);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query->distinct();
}

/**
Expand Down
39 changes: 34 additions & 5 deletions app/Models/Concerns/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,15 @@ protected function filterRace(Builder $query, string $value = null): void
{
$ids = [$value];
if ($this->filterOption('exclude')) {
$query->whereRaw('(select count(*) from character_race as cr where cr.character_id = ' .
$this->getTable() . '.id and cr.race_id = ' . ((int) $value) . ') = 0');
if (auth()->check() && auth()->user()->isAdmin()) {
$query->whereRaw('(select count(*) from character_race as cr where cr.character_id = ' .
$this->getTable() . '.id and cr.race_id = ' . ((int) $value) . ') = 0');
} else {
$query->whereRaw('(select count(*) from character_race as cr where cr.character_id = ' .
$this->getTable() . '.id and cr.race_id = ' . ((int) $value) . ' and cr.is_private = 0) = 0');
}
return;

} elseif ($this->filterOption('children')) {
/** @var Race|null $race */
$race = Race::find($value);
Expand All @@ -505,7 +511,12 @@ protected function filterRace(Builder $query, string $value = null): void
->select($this->getTable() . '.*')
->leftJoin('character_race as cr', function ($join) {
$join->on('cr.character_id', '=', $this->getTable() . '.id');
})->whereIn('cr.race_id', $ids)->distinct();
})->whereIn('cr.race_id', $ids);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('cr.is_private', false);
}
$query->distinct();
}

/**
Expand All @@ -516,8 +527,10 @@ protected function filterFamily(Builder $query, string $value = null): void
$ids = [$value];
if ($this->filterOption('exclude')) {
$query->whereRaw('(select count(*) from character_family as cf where cf.character_id = ' .
$this->getTable() . '.id and cf.family_id = ' . ((int) $value) . ') = 0');
$this->getTable() . '.id and cf.family_id = ' . ((int) $value)
. ' and ' . $this->subPrivacy('cf.is_private') . ') = 0');
return;

} elseif ($this->filterOption('children')) {
/** @var Family|null $family */
$family = Family::find($value);
Expand All @@ -531,7 +544,13 @@ protected function filterFamily(Builder $query, string $value = null): void
->select($this->getTable() . '.*')
->leftJoin('character_family as cf', function ($join) {
$join->on('cf.character_id', '=', $this->getTable() . '.id');
})->whereIn('cf.family_id', $ids)->distinct();
})->whereIn('cf.family_id', $ids);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('cf.is_private', false);
}

$query->distinct();
}

/**
Expand Down Expand Up @@ -677,4 +696,14 @@ protected function explicitFilters(): array
}
return [];
}

protected function subPrivacy(string $field): string|null
{
// Campaign admins don't have private data hidden from them
if (auth()->check() && auth()->user()->isAdmin()) {
return null;
}

return $field . ' = 0';
}
}
23 changes: 18 additions & 5 deletions app/Models/Family.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,38 @@ public function scopeMember(Builder $query, string|null $value, FilterOption $fi
if (!empty($value)) {
return $query;
}
return $query
$query
->select($this->getTable() . '.*')
->leftJoin('character_family as memb', function ($join) {
$join->on('memb.family_id', '=', $this->getTable() . '.id');
})
->where('memb.character_id', null);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query;
} elseif ($filter === FilterOption::EXCLUDE) {
return $query
->whereRaw('(select count(*) from character_family as memb where memb.family_id = ' .
$this->getTable() . '.id and memb.character_id in (' . (int) $value . ')) = 0');
$this->getTable() . '.id and memb.family_id = ' . ((int) $value) . ' and ' . $this->subPrivacy('memb.is_private') . ') = 0');
}

$ids = [$value];
return $query
$query
->select($this->getTable() . '.*')
->leftJoin('character_family as memb', function ($join) {
$join->on('memb.family_id', '=', $this->getTable() . '.id');
})
->whereIn('memb.character_id', $ids)->distinct();
->whereIn('memb.character_id', $ids);


if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query->distinct();
}

/**
Expand All @@ -176,7 +189,7 @@ public function datagridSelectFields(): array
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function location()
{
Expand Down
21 changes: 17 additions & 4 deletions app/Models/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,37 @@ public function scopeMember(Builder $query, string|null $value, FilterOption $fi
if (!empty($value)) {
return $query;
}
return $query
$query
->select($this->getTable() . '.*')
->leftJoin('organisation_member as memb', function ($join) {
$join->on('memb.organisation_id', '=', $this->getTable() . '.id');
})
->where('memb.character_id', null);
if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query;

} elseif ($filter === FilterOption::EXCLUDE) {
return $query
->whereRaw('(select count(*) from organisation_member as memb where memb.organisation_id = ' .
$this->getTable() . '.id and memb.character_id in (' . (int) $value . ')) = 0');
$this->getTable() . '.id and memb.character_id = ' . ((int) $value) . ' and ' . $this->subPrivacy('memb.is_private') . ') = 0');
}
$ids = [$value];
return $query

$query
->select($this->getTable() . '.*')
->leftJoin('organisation_member as memb', function ($join) {
$join->on('memb.organisation_id', '=', $this->getTable() . '.id');
})
->whereIn('memb.character_id', $ids)->distinct();
->whereIn('memb.character_id', $ids);

if (auth()->guest() || !auth()->user()->isAdmin()) {
$query->where('memb.is_private', 0);
}

return $query->distinct();
}

/**
Expand Down

0 comments on commit eec9a97

Please sign in to comment.