130 lines
3.5 KiB
PHP
130 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Role;
|
|
use App\Enums\TicketStatus;
|
|
use App\Models\Ticket;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
use Illuminate\Auth\Access\Response;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class TicketPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
/**
|
|
* Determine whether the user can view any models.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return mixed
|
|
*/
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
|
public function viewAny(User $user)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can view the model.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return mixed
|
|
*/
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
|
public function view(User $user, Ticket $ticket)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can create models.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return mixed
|
|
*/
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
|
public function create(User $user)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can update the model.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return mixed
|
|
*/
|
|
public function update(User $user, Ticket $ticket)
|
|
{
|
|
return $user->isAdmin() or $user->sites->contains($ticket->site_id);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can delete the model.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return mixed
|
|
*/
|
|
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
|
public function delete(User $user, Ticket $ticket)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can restore the model.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return mixed
|
|
*/
|
|
public function restore(User $user, Ticket $ticket)
|
|
{
|
|
return $user->isAdmin() or $user->sites->contains($ticket->site_id);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can permanently delete the model.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return mixed
|
|
*/
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
|
public function forceDelete(User $user, Ticket $ticket)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function changeStatus(User $user)
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
public function suscribeToTicket(User $user, Ticket $ticket)
|
|
{
|
|
return !$ticket->status->equals(TicketStatus::archived());
|
|
}
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
public function createMessage(User $user, Ticket $ticket)
|
|
{
|
|
//can create if
|
|
//role == 1
|
|
// OR site_id IN user->sites
|
|
|
|
if (!($user->isAdmin() or $user->sites->contains($ticket->site_id))) {
|
|
return Response::deny(trans('Vous ne pouvez modifier un ticket qui ne vous appartient pas.'));
|
|
}
|
|
return $ticket->status->equals(TicketStatus::archived()) ?
|
|
Response::deny(trans('Vous ne pouvez ajouter un message à un ticket qui est archivé')) : Response::allow();
|
|
}
|
|
}
|