35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\Simulation;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SimulationPolicy
|
|
{
|
|
public function view(User $user, Simulation $simulation): bool
|
|
{
|
|
if ($simulation->organization_id === null && $simulation->owner_user_id === $user->id && $user->hasVerifiedEmail()) {
|
|
return true;
|
|
}
|
|
|
|
return $simulation->organization_id !== null && $user->hasVerifiedEmail()
|
|
&& DB::table('memberships')->where('user_id', $user->id)
|
|
->where('organization_id', $simulation->organization_id)->whereNull('deleted_at')->exists();
|
|
}
|
|
|
|
public function update(User $user, Simulation $simulation): bool
|
|
{
|
|
if (! $user->hasVerifiedEmail() || ! in_array($simulation->status, ['queued', 'completed', 'failed'], true)) {
|
|
return false;
|
|
}
|
|
if ($simulation->organization_id === null) {
|
|
return $simulation->owner_user_id === $user->id;
|
|
}
|
|
|
|
return DB::table('memberships')->where('user_id', $user->id)->where('organization_id', $simulation->organization_id)
|
|
->whereNull('deleted_at')->whereIn('role', ['admin', 'manager', 'officer'])->exists();
|
|
}
|
|
}
|