Files
fluent-chat/app/Policies/ConversationPolicy.php
2026-05-01 00:42:01 +03:30

82 lines
1.9 KiB
PHP

<?php
namespace App\Policies;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\User;
class ConversationPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return true;
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Conversation $conversation): bool
{
return $this->participates($user, $conversation);
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return true;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Conversation $conversation): bool
{
return $conversation->participants()
->where('user_id', $user->id)
->where('role', ConversationParticipant::RoleAdmin)
->exists();
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Conversation $conversation): bool
{
return $conversation->created_by_id === $user->id;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Conversation $conversation): bool
{
return false;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Conversation $conversation): bool
{
return false;
}
public function sendMessage(User $user, Conversation $conversation): bool
{
return $this->participates($user, $conversation);
}
private function participates(User $user, Conversation $conversation): bool
{
return $conversation->participants()
->where('user_id', $user->id)
->exists();
}
}