306 lines
7.6 KiB
PHP
306 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Enums\Tickets\TicketTypeEnum;
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Models\Ticket;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
|
|
class TicketPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
/**
|
|
* Determine if the user can view any tickets.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function viewAny()
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can view a given ticket.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function view()
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can create a ticket.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function create()
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can update a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function update(User $user, Ticket $ticket)
|
|
{
|
|
return $user->sites->contains($ticket->site_id)
|
|
|| $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can change the status of a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function changeStatus(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can link ticket .
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function linkTicket(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can change the internal status of a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function changeInternalStatus(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can delete a given ticket.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function delete()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can force delete a given ticket.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function forceDelete()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can restore a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function restore(User $user, Ticket $ticket)
|
|
{
|
|
return $user->sites->contains($ticket->site_id)
|
|
|| $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can suscribe to a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
*/
|
|
public function suscribeToTicket(User $user, Ticket $ticket)
|
|
{
|
|
// phpcs:enable
|
|
return !$ticket->status->equals(TicketStatusEnum::archived);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can create a message for a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
*/
|
|
public function createMessage(User $user, Ticket $ticket)
|
|
{
|
|
// phpcs:enable
|
|
return !$ticket->status->equals(TicketStatusEnum::archived);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can create a private message for a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function createPrivateMessage(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can update a message for a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function updateMessage(User $user, Ticket $ticket)
|
|
{
|
|
return !$ticket->status->equals(TicketStatusEnum::archived)
|
|
&& $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can reopen a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function reopen(User $user)
|
|
{
|
|
return $user->role->equals(RoleEnum::client);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can assign a given ticket to a user.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function assignTo(User $user, Ticket $ticket)
|
|
{
|
|
return $user->isAdmin()
|
|
&& !$ticket->status->equals(TicketStatusEnum::archived)
|
|
&& !$ticket->status->equals(TicketStatusEnum::production);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can assign tickets.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function assignTickets(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can assign labels to a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @return boolean
|
|
*/
|
|
public function assignLabels(User $user)
|
|
{
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can estimate a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function estimate(User $user, Ticket $ticket)
|
|
{
|
|
if (!($brand = $ticket->site->brand)) {
|
|
return false;
|
|
}
|
|
return $user->isAdmin()
|
|
&& (bool)$brand->accounts()->count()
|
|
&& !$ticket->estimation()->exists()
|
|
&& !(bool)$ticket->is_internal
|
|
&& !$ticket->status->equals(TicketStatusEnum::archived);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can validate the estimation of a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function validateEstimate(User $user, Ticket $ticket)
|
|
{
|
|
if (!($brand = $ticket->site->brand)) {
|
|
return false;
|
|
}
|
|
return ($user->role->equals(RoleEnum::client)
|
|
|| $user->role->equals(RoleEnum::developper))
|
|
&& (bool)$brand->accounts()->count()
|
|
&& $ticket->estimation()->exists()
|
|
&& !(bool)$ticket->is_internal
|
|
&& !$ticket->status->equals(TicketStatusEnum::archived);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can cancel the estimation of a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function cancelEstimate(User $user, Ticket $ticket)
|
|
{
|
|
return $user->role->equals(RoleEnum::developper)
|
|
&& $ticket->estimation()->exists();
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can create a gitlab issue from a given ticket.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Ticket $ticket
|
|
* @return boolean
|
|
*/
|
|
public function createGitlabIssue(User $user, Ticket $ticket)
|
|
{
|
|
return $user->role->equals(RoleEnum::developper)
|
|
&& (
|
|
$ticket->type->equals(TicketTypeEnum::bug)
|
|
|| $ticket->type->equals(TicketTypeEnum::evolution)
|
|
|| $ticket->type->equals(TicketTypeEnum::newsletter)
|
|
)
|
|
&& !\is_null($ticket->site->gitlab_project_id)
|
|
&& \is_null($ticket->gitlab_issue);
|
|
}
|
|
|
|
/**
|
|
* Determine if the user can changeOrder
|
|
*
|
|
* @param User $user
|
|
* @return boolean
|
|
* @throws Error The error.
|
|
*/
|
|
public function changeOrder(User $user)
|
|
{
|
|
return $user->role->equals(RoleEnum::developper);
|
|
}
|
|
}
|