133 lines
4.1 KiB
PHP
133 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Models\BannedIp;
|
|
use App\Models\Customer;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use App\Models\User;
|
|
use App\Policies\Rules\UserStaticRules;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class ParticipationPolicy
|
|
{
|
|
/**
|
|
* Determine whether the user can view any models.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @return boolean
|
|
*/
|
|
public function viewWinnerList(User $authUser): bool
|
|
{
|
|
return UserStaticRules::atLeastRole($authUser, RoleEnum::marketing);
|
|
}
|
|
|
|
/**
|
|
* Wether a customer can participate
|
|
*
|
|
* @param \App\Models\Customer $authUser
|
|
* @return boolean
|
|
*/
|
|
public function participate(Customer $authUser): bool
|
|
{
|
|
/** @var \App\Models\Settings */
|
|
$settings = app(Settings::class);
|
|
|
|
// * Participation par compte
|
|
$playerCanParticipate = true;
|
|
// ? Différent de illimité (0) ?
|
|
if ($settings->nb_participation_player) {
|
|
$playerCanParticipateAccount = (Participation::query()->whereHas(
|
|
'customer',
|
|
fn (Builder $q) => $q->where('customer_id', $authUser->id)
|
|
)->count() < $settings->nb_participation_player);
|
|
}
|
|
// * Participation par IP
|
|
// ? Différent de illimité (0) ?
|
|
if ($settings->nb_participation_ip) {
|
|
$playerCanParticipateIp = (Participation::query()->whereHas(
|
|
'customer',
|
|
fn (Builder $q) => $q->where('ip', app('clientIp'))
|
|
)->count() < $settings->nb_participation_ip);
|
|
}
|
|
$playerCanParticipate = (!empty($playerCanParticipateAccount) && !empty($playerCanParticipateIp)) ?
|
|
true :
|
|
false;
|
|
|
|
// * Deny banned IP, Check did not participate too much
|
|
return !(bool) BannedIp::getCached()
|
|
->where('ip', app('clientIp'))->count() and
|
|
$playerCanParticipate;
|
|
}
|
|
|
|
/**
|
|
* Wether a customer can access to won list page
|
|
*
|
|
* @param \App\Models\Customer|\App\Models\User $authUser
|
|
* @return boolean
|
|
*/
|
|
public function viewWonList(Customer|User $authUser): bool
|
|
{
|
|
if ($authUser instanceof User) {
|
|
// * Recettage
|
|
return true;
|
|
}
|
|
return (bool) $authUser->participations()->where(function (Builder $q) {
|
|
$q->whereHas('instantWin')->orWhereHas('discount');
|
|
})->count();
|
|
}
|
|
|
|
/**
|
|
* Can a customer submit winning proof ?
|
|
*
|
|
* @param \App\Models\Customer $authUser
|
|
* @param \App\Models\Participation $model
|
|
* @return boolean
|
|
*/
|
|
public function submitProof(Customer $authUser, Participation $model): bool
|
|
{
|
|
/** @var \App\Models\Settings */
|
|
$settings = app(Settings::class);
|
|
return $settings->winning_proof and Participation::query()
|
|
->where('prize_sent', false)
|
|
->whereHas(
|
|
'customer',
|
|
fn (Builder $q) =>
|
|
$q->where('id', $authUser->id)
|
|
)->where('id', $model->id)->whereHas('instantWin')
|
|
->where(function (Builder $q) {
|
|
$q->whereNull(
|
|
['sticker', 'ticket', 'barcode'],
|
|
'or'
|
|
);
|
|
})->exists();
|
|
}
|
|
|
|
/**
|
|
* Can a user change comment ?
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Participation $model
|
|
* @return boolean
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
|
*/
|
|
public function setComment(User $authUser, Participation $model): bool
|
|
{
|
|
// phpcs:enable
|
|
return UserStaticRules::atLeastRole($authUser, RoleEnum::marketing);
|
|
}
|
|
|
|
/**
|
|
* Can a user change prize_sent ?
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Participation $model
|
|
* @return boolean
|
|
*/
|
|
public function changePrizeSent(User $authUser, Participation $model): bool
|
|
{
|
|
return !$model->prize_sent and UserStaticRules::atLeastRole($authUser, RoleEnum::marketing);
|
|
}
|
|
}
|