98 lines
2.3 KiB
PHP
98 lines
2.3 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);
|
|
}
|
|
|
|
public function addMembers(User $user, Conversation $conversation): bool
|
|
{
|
|
if (! $this->participates($user, $conversation)) {
|
|
return false;
|
|
}
|
|
|
|
if (! $conversation->isGroup()) {
|
|
return true;
|
|
}
|
|
|
|
return $conversation->participants()
|
|
->where('user_id', $user->id)
|
|
->where('role', ConversationParticipant::RoleAdmin)
|
|
->exists();
|
|
}
|
|
|
|
private function participates(User $user, Conversation $conversation): bool
|
|
{
|
|
return $conversation->participants()
|
|
->where('user_id', $user->id)
|
|
->exists();
|
|
}
|
|
}
|