95 lines
3.4 KiB
PHP
95 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\ExistingBusiness;
|
|
use App\Models\User;
|
|
|
|
class ExistingBusinessPolicy
|
|
{
|
|
public function create(User $user): bool
|
|
{
|
|
return $user->hasVerifiedEmail();
|
|
}
|
|
|
|
public function update(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return $this->owns($user, $business) && in_array($business->status, ['draft', 'needs_information'], true);
|
|
}
|
|
|
|
public function view(User $user, ExistingBusiness $business): bool
|
|
{
|
|
if ($this->owns($user, $business) || $this->review($user, $business) || $this->triage($user, $business)
|
|
|| $this->approveLevelOne($user, $business) || $this->approveLevelTwo($user, $business) || $this->publish($user, $business)) {
|
|
return true;
|
|
}
|
|
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& in_array($business->status, ['rejected', 'published', 'suspended', 'cancelled', 'archived'], true)
|
|
&& $this->reviewQueue($user);
|
|
}
|
|
|
|
public function delete(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return $this->update($user, $business);
|
|
}
|
|
|
|
public function owns(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return $user->hasVerifiedEmail() && $business->owner_user_id === $user->id && ! $business->trashed();
|
|
}
|
|
|
|
public function review(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& in_array($business->status, ['triage', 'in_review', 'needs_information'], true)
|
|
&& $this->designated($user, 'reviewers');
|
|
}
|
|
|
|
public function triage(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& in_array($business->status, ['submitted', 'triage'], true)
|
|
&& $this->designated($user, 'triagers');
|
|
}
|
|
|
|
public function approveLevelOne(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& $business->status === 'approval_l1'
|
|
&& $this->designated($user, 'approvers_l1');
|
|
}
|
|
|
|
public function approveLevelTwo(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& $business->status === 'approval_l2'
|
|
&& $this->designated($user, 'approvers_l2');
|
|
}
|
|
|
|
public function publish(User $user, ExistingBusiness $business): bool
|
|
{
|
|
return ! $business->trashed() && $business->owner_user_id !== $user->id
|
|
&& $business->status === 'approved'
|
|
&& $this->designated($user, 'publishers');
|
|
}
|
|
|
|
public function reviewQueue(User $user): bool
|
|
{
|
|
foreach (['reviewers', 'triagers', 'approvers_l1', 'approvers_l2', 'publishers'] as $designation) {
|
|
if ($this->designated($user, $designation)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function designated(User $user, string $designation): bool
|
|
{
|
|
return $user->hasVerifiedEmail() && $user->can('manage-portal')
|
|
&& (bool) $user->two_factor_confirmed_at && (bool) $user->two_factor_secret
|
|
&& in_array(strtolower($user->email), config('existing-business.'.$designation, []), true);
|
|
}
|
|
}
|