Initial project
This commit is contained in:
81
app/Policies/ConversationPolicy.php
Normal file
81
app/Policies/ConversationPolicy.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user