Initial project
This commit is contained in:
33
app/Actions/Fortify/CreateNewUser.php
Normal file
33
app/Actions/Fortify/CreateNewUser.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Fortify;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use App\Concerns\ProfileValidationRules;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Laravel\Fortify\Contracts\CreatesNewUsers;
|
||||
|
||||
class CreateNewUser implements CreatesNewUsers
|
||||
{
|
||||
use PasswordValidationRules, ProfileValidationRules;
|
||||
|
||||
/**
|
||||
* Validate and create a newly registered user.
|
||||
*
|
||||
* @param array<string, string> $input
|
||||
*/
|
||||
public function create(array $input): User
|
||||
{
|
||||
Validator::make($input, [
|
||||
...$this->profileRules(),
|
||||
'password' => $this->passwordRules(),
|
||||
])->validate();
|
||||
|
||||
return User::create([
|
||||
'name' => $input['name'],
|
||||
'email' => $input['email'],
|
||||
'password' => $input['password'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
29
app/Actions/Fortify/ResetUserPassword.php
Normal file
29
app/Actions/Fortify/ResetUserPassword.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Fortify;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Laravel\Fortify\Contracts\ResetsUserPasswords;
|
||||
|
||||
class ResetUserPassword implements ResetsUserPasswords
|
||||
{
|
||||
use PasswordValidationRules;
|
||||
|
||||
/**
|
||||
* Validate and reset the user's forgotten password.
|
||||
*
|
||||
* @param array<string, string> $input
|
||||
*/
|
||||
public function reset(User $user, array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'password' => $this->passwordRules(),
|
||||
])->validate();
|
||||
|
||||
$user->forceFill([
|
||||
'password' => $input['password'],
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
29
app/Concerns/PasswordValidationRules.php
Normal file
29
app/Concerns/PasswordValidationRules.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Concerns;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
trait PasswordValidationRules
|
||||
{
|
||||
/**
|
||||
* Get the validation rules used to validate passwords.
|
||||
*
|
||||
* @return array<int, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
protected function passwordRules(): array
|
||||
{
|
||||
return ['required', 'string', Password::default(), 'confirmed'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules used to validate the current password.
|
||||
*
|
||||
* @return array<int, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
protected function currentPasswordRules(): array
|
||||
{
|
||||
return ['required', 'string', 'current_password'];
|
||||
}
|
||||
}
|
||||
51
app/Concerns/ProfileValidationRules.php
Normal file
51
app/Concerns/ProfileValidationRules.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Concerns;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
trait ProfileValidationRules
|
||||
{
|
||||
/**
|
||||
* Get the validation rules used to validate user profiles.
|
||||
*
|
||||
* @return array<string, array<int, ValidationRule|array<mixed>|string>>
|
||||
*/
|
||||
protected function profileRules(?int $userId = null): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->nameRules(),
|
||||
'email' => $this->emailRules($userId),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules used to validate user names.
|
||||
*
|
||||
* @return array<int, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
protected function nameRules(): array
|
||||
{
|
||||
return ['required', 'string', 'max:255'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules used to validate user emails.
|
||||
*
|
||||
* @return array<int, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
protected function emailRules(?int $userId = null): array
|
||||
{
|
||||
return [
|
||||
'required',
|
||||
'string',
|
||||
'email',
|
||||
'max:255',
|
||||
$userId === null
|
||||
? Rule::unique(User::class)
|
||||
: Rule::unique(User::class)->ignore($userId),
|
||||
];
|
||||
}
|
||||
}
|
||||
8
app/Http/Controllers/Controller.php
Normal file
8
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
22
app/Livewire/Actions/Logout.php
Normal file
22
app/Livewire/Actions/Logout.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class Logout
|
||||
{
|
||||
/**
|
||||
* Log the current user out of the application.
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
Auth::guard('web')->logout();
|
||||
|
||||
Session::invalidate();
|
||||
Session::regenerateToken();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
||||
50
app/Livewire/Chat/ChatPage.php
Normal file
50
app/Livewire/Chat/ChatPage.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Chat')]
|
||||
class ChatPage extends Component
|
||||
{
|
||||
public ?int $selectedConversationId = null;
|
||||
|
||||
public bool $detailsPanelOpen = true;
|
||||
|
||||
#[On('conversation-selected')]
|
||||
public function selectConversation(int $conversationId): void
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
$this->selectedConversationId = $conversation->id;
|
||||
$this->detailsPanelOpen = true;
|
||||
}
|
||||
|
||||
#[On('conversation-closed')]
|
||||
public function clearConversation(): void
|
||||
{
|
||||
$this->selectedConversationId = null;
|
||||
$this->detailsPanelOpen = false;
|
||||
}
|
||||
|
||||
#[On('conversation-details-toggled')]
|
||||
public function toggleDetailsPanel(): void
|
||||
{
|
||||
$this->detailsPanelOpen = ! $this->detailsPanelOpen;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.chat-page');
|
||||
}
|
||||
}
|
||||
66
app/Livewire/Chat/ConversationDetailsPanel.php
Normal file
66
app/Livewire/Chat/ConversationDetailsPanel.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationDetailsPanel extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'joined_at'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->withCount('messages')
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return $this->conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function initials(): string
|
||||
{
|
||||
return collect(explode(' ', $this->title()))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-details-panel');
|
||||
}
|
||||
}
|
||||
103
app/Livewire/Chat/ConversationHeader.php
Normal file
103
app/Livewire/Chat/ConversationHeader.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationHeader extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return $this->conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->otherParticipant()?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function subtitle(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return trans_choice(':count member|:count members', $this->conversation->participants->count(), [
|
||||
'count' => $this->conversation->participants->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->isOnline() ? __('Online') : __('Recently active');
|
||||
}
|
||||
|
||||
public function initials(): string
|
||||
{
|
||||
if (! $this->conversation->isGroup()) {
|
||||
return $this->otherParticipant()?->initials() ?? 'DC';
|
||||
}
|
||||
|
||||
return collect(explode(' ', $this->title()))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function isOnline(): bool
|
||||
{
|
||||
$participant = $this->otherParticipant();
|
||||
|
||||
return $participant instanceof User && $participant->id % 3 !== 0;
|
||||
}
|
||||
|
||||
public function closeConversation(): void
|
||||
{
|
||||
$this->dispatch('conversation-closed');
|
||||
}
|
||||
|
||||
public function toggleDetails(): void
|
||||
{
|
||||
$this->dispatch('conversation-details-toggled');
|
||||
}
|
||||
|
||||
private function otherParticipant(): ?User
|
||||
{
|
||||
return $this->conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-header');
|
||||
}
|
||||
}
|
||||
166
app/Livewire/Chat/ConversationList.php
Normal file
166
app/Livewire/Chat/ConversationList.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationList extends Component
|
||||
{
|
||||
public ?int $selectedConversationId = null;
|
||||
|
||||
public string $search = '';
|
||||
|
||||
#[On('message-created')]
|
||||
public function refreshConversations(): void
|
||||
{
|
||||
unset($this->conversations);
|
||||
}
|
||||
|
||||
public function selectConversation(int $conversationId): void
|
||||
{
|
||||
$this->dispatch('conversation-selected', conversationId: $conversationId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Conversation>
|
||||
*/
|
||||
#[Computed]
|
||||
public function conversations(): Collection
|
||||
{
|
||||
$user = Auth::user();
|
||||
$search = trim($this->search);
|
||||
|
||||
return Conversation::query()
|
||||
->select(['id', 'created_by_id', 'type', 'name', 'description', 'created_at', 'updated_at'])
|
||||
->forUser($user)
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
|
||||
->with('user:id,name,email'),
|
||||
'latestMessage' => fn ($query) => $query
|
||||
->select(['messages.id', 'messages.conversation_id', 'messages.user_id', 'messages.body', 'messages.created_at']),
|
||||
'latestMessage.sender:id,name,email',
|
||||
])
|
||||
->withMax('messages', 'created_at')
|
||||
->withCount([
|
||||
'messages as unread_messages_count' => fn (Builder $messages) => $messages
|
||||
->where('user_id', '!=', $user->id)
|
||||
->whereExists(fn ($participants) => $participants
|
||||
->selectRaw('1')
|
||||
->from('conversation_participants')
|
||||
->whereColumn('conversation_participants.conversation_id', 'messages.conversation_id')
|
||||
->where('conversation_participants.user_id', $user->id)
|
||||
->where(fn ($readState) => $readState
|
||||
->whereNull('conversation_participants.last_read_at')
|
||||
->orWhereColumn('messages.created_at', '>', 'conversation_participants.last_read_at'))),
|
||||
])
|
||||
->when($search !== '', fn (Builder $query) => $query->where(function (Builder $query) use ($search, $user): void {
|
||||
$query
|
||||
->where('name', 'like', "%{$search}%")
|
||||
->orWhereHas('participants.user', fn (Builder $users) => $users
|
||||
->where('users.id', '!=', $user->id)
|
||||
->where(fn (Builder $users) => $users
|
||||
->where('name', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%")));
|
||||
}))
|
||||
->orderByDesc('messages_max_created_at')
|
||||
->orderByDesc('updated_at')
|
||||
->limit(40)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function titleFor(Conversation $conversation): string
|
||||
{
|
||||
if ($conversation->isGroup()) {
|
||||
return $conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->otherParticipant($conversation)?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function initialsFor(Conversation $conversation): string
|
||||
{
|
||||
if ($conversation->isGroup()) {
|
||||
return collect(explode(' ', $this->titleFor($conversation)))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
return $this->otherParticipant($conversation)?->initials() ?? 'DC';
|
||||
}
|
||||
|
||||
public function previewFor(Conversation $conversation): string
|
||||
{
|
||||
if (! $conversation->latestMessage) {
|
||||
return __('No messages yet');
|
||||
}
|
||||
|
||||
$prefix = $conversation->latestMessage->user_id === Auth::id()
|
||||
? __('You: ')
|
||||
: ($conversation->isGroup() ? $conversation->latestMessage->sender?->name.': ' : '');
|
||||
|
||||
return str($prefix.$conversation->latestMessage->body)
|
||||
->squish()
|
||||
->limit(86)
|
||||
->toString();
|
||||
}
|
||||
|
||||
public function timeFor(?CarbonInterface $timestamp): string
|
||||
{
|
||||
if (! $timestamp) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($timestamp->isToday()) {
|
||||
return $timestamp->format('H:i');
|
||||
}
|
||||
|
||||
if ($timestamp->isCurrentYear()) {
|
||||
return $timestamp->format('M j');
|
||||
}
|
||||
|
||||
return $timestamp->format('M j, Y');
|
||||
}
|
||||
|
||||
public function participantSummaryFor(Conversation $conversation): string
|
||||
{
|
||||
if (! $conversation->isGroup()) {
|
||||
return $this->isOnline($conversation) ? __('Online') : __('Recently active');
|
||||
}
|
||||
|
||||
return trans_choice(':count member|:count members', $conversation->participants->count(), [
|
||||
'count' => $conversation->participants->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function isOnline(Conversation $conversation): bool
|
||||
{
|
||||
$participant = $this->otherParticipant($conversation);
|
||||
|
||||
return $participant instanceof User && $participant->id % 3 !== 0;
|
||||
}
|
||||
|
||||
private function otherParticipant(Conversation $conversation): ?User
|
||||
{
|
||||
return $conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-list');
|
||||
}
|
||||
}
|
||||
123
app/Livewire/Chat/ConversationView.php
Normal file
123
app/Livewire/Chat/ConversationView.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationView extends Component
|
||||
{
|
||||
public int $conversationId;
|
||||
|
||||
public int $messageLimit = 40;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
|
||||
$this->authorizeConversation();
|
||||
$this->markAsRead();
|
||||
}
|
||||
|
||||
public function loadEarlier(): void
|
||||
{
|
||||
$this->messageLimit += 25;
|
||||
}
|
||||
|
||||
#[On('message-created')]
|
||||
public function refreshMessages(int $conversationId): void
|
||||
{
|
||||
if ($conversationId !== $this->conversationId) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->messages, $this->hasMoreMessages);
|
||||
|
||||
$this->markAsRead();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Message>
|
||||
*/
|
||||
#[Computed]
|
||||
public function messages(): Collection
|
||||
{
|
||||
return Message::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->with('sender:id,name,email')
|
||||
->latest()
|
||||
->limit($this->messageLimit)
|
||||
->get()
|
||||
->reverse()
|
||||
->values();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function hasMoreMessages(): bool
|
||||
{
|
||||
return Message::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->count() > $this->messageLimit;
|
||||
}
|
||||
|
||||
public function dateLabel(CarbonInterface $timestamp): string
|
||||
{
|
||||
if ($timestamp->isToday()) {
|
||||
return __('Today');
|
||||
}
|
||||
|
||||
if ($timestamp->isYesterday()) {
|
||||
return __('Yesterday');
|
||||
}
|
||||
|
||||
return $timestamp->format('F j, Y');
|
||||
}
|
||||
|
||||
private function authorizeConversation(): void
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
}
|
||||
|
||||
private function markAsRead(): void
|
||||
{
|
||||
ConversationParticipant::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->where('user_id', Auth::id())
|
||||
->update(['last_read_at' => now()]);
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-view');
|
||||
}
|
||||
}
|
||||
77
app/Livewire/Chat/MessageComposer.php
Normal file
77
app/Livewire/Chat/MessageComposer.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class MessageComposer extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
|
||||
$this->conversation();
|
||||
}
|
||||
|
||||
public function sendMessage(): void
|
||||
{
|
||||
$this->body = trim($this->body);
|
||||
|
||||
$validated = $this->validate([
|
||||
'body' => ['required', 'string', 'max:4000'],
|
||||
], [
|
||||
'body.required' => __('Write a message before sending.'),
|
||||
]);
|
||||
|
||||
$conversation = $this->conversation();
|
||||
|
||||
Gate::authorize('sendMessage', $conversation);
|
||||
|
||||
$message = Message::query()->create([
|
||||
'conversation_id' => $conversation->id,
|
||||
'user_id' => Auth::id(),
|
||||
'type' => Message::TypeText,
|
||||
'body' => $validated['body'],
|
||||
]);
|
||||
|
||||
$conversation->touch();
|
||||
|
||||
ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', Auth::id())
|
||||
->update(['last_read_at' => now()]);
|
||||
|
||||
$this->reset('body');
|
||||
$this->resetValidation();
|
||||
|
||||
$this->dispatch('message-created', conversationId: $conversation->id, messageId: $message->id);
|
||||
}
|
||||
|
||||
private function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('sendMessage', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.message-composer');
|
||||
}
|
||||
}
|
||||
12
app/Livewire/Settings/Appearance.php
Normal file
12
app/Livewire/Settings/Appearance.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Appearance settings')]
|
||||
class Appearance extends Component
|
||||
{
|
||||
//
|
||||
}
|
||||
29
app/Livewire/Settings/DeleteUserForm.php
Normal file
29
app/Livewire/Settings/DeleteUserForm.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use App\Livewire\Actions\Logout;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class DeleteUserForm extends Component
|
||||
{
|
||||
use PasswordValidationRules;
|
||||
|
||||
public string $password = '';
|
||||
|
||||
/**
|
||||
* Delete the currently authenticated user.
|
||||
*/
|
||||
public function deleteUser(Logout $logout): void
|
||||
{
|
||||
$this->validate([
|
||||
'password' => $this->currentPasswordRules(),
|
||||
]);
|
||||
|
||||
tap(Auth::user(), $logout(...))->delete();
|
||||
|
||||
$this->redirect('/', navigate: true);
|
||||
}
|
||||
}
|
||||
81
app/Livewire/Settings/Profile.php
Normal file
81
app/Livewire/Settings/Profile.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\ProfileValidationRules;
|
||||
use Flux\Flux;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Profile settings')]
|
||||
class Profile extends Component
|
||||
{
|
||||
use ProfileValidationRules;
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public string $email = '';
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->name = Auth::user()->name;
|
||||
$this->email = Auth::user()->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the profile information for the currently authenticated user.
|
||||
*/
|
||||
public function updateProfileInformation(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$validated = $this->validate($this->profileRules($user->id));
|
||||
|
||||
$user->fill($validated);
|
||||
|
||||
if ($user->isDirty('email')) {
|
||||
$user->email_verified_at = null;
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
Flux::toast(variant: 'success', text: __('Profile updated.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an email verification notification to the current user.
|
||||
*/
|
||||
public function resendVerificationNotification(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->hasVerifiedEmail()) {
|
||||
$this->redirectIntended(default: route('dashboard', absolute: false));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$user->sendEmailVerificationNotification();
|
||||
|
||||
Flux::toast(text: __('A new verification link has been sent to your email address.'));
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function hasUnverifiedEmail(): bool
|
||||
{
|
||||
return Auth::user() instanceof MustVerifyEmail && ! Auth::user()->hasVerifiedEmail();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function showDeleteUser(): bool
|
||||
{
|
||||
return ! Auth::user() instanceof MustVerifyEmail
|
||||
|| (Auth::user() instanceof MustVerifyEmail && Auth::user()->hasVerifiedEmail());
|
||||
}
|
||||
}
|
||||
227
app/Livewire/Settings/Security.php
Normal file
227
app/Livewire/Settings/Security.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use Exception;
|
||||
use Flux\Flux;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Features;
|
||||
use Laravel\Fortify\Fortify;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Security settings')]
|
||||
class Security extends Component
|
||||
{
|
||||
use PasswordValidationRules;
|
||||
|
||||
public string $current_password = '';
|
||||
|
||||
public string $password = '';
|
||||
|
||||
public string $password_confirmation = '';
|
||||
|
||||
#[Locked]
|
||||
public bool $canManageTwoFactor;
|
||||
|
||||
#[Locked]
|
||||
public bool $twoFactorEnabled;
|
||||
|
||||
#[Locked]
|
||||
public bool $requiresConfirmation;
|
||||
|
||||
#[Locked]
|
||||
public string $qrCodeSvg = '';
|
||||
|
||||
#[Locked]
|
||||
public string $manualSetupKey = '';
|
||||
|
||||
public bool $showModal = false;
|
||||
|
||||
public bool $showVerificationStep = false;
|
||||
|
||||
#[Validate('required|string|size:6', onUpdate: false)]
|
||||
public string $code = '';
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void
|
||||
{
|
||||
$this->canManageTwoFactor = Features::canManageTwoFactorAuthentication();
|
||||
|
||||
if ($this->canManageTwoFactor) {
|
||||
if (Fortify::confirmsTwoFactorAuthentication() && is_null(auth()->user()->two_factor_confirmed_at)) {
|
||||
$disableTwoFactorAuthentication(auth()->user());
|
||||
}
|
||||
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
$this->requiresConfirmation = Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the password for the currently authenticated user.
|
||||
*/
|
||||
public function updatePassword(): void
|
||||
{
|
||||
try {
|
||||
$validated = $this->validate([
|
||||
'current_password' => $this->currentPasswordRules(),
|
||||
'password' => $this->passwordRules(),
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Auth::user()->update([
|
||||
'password' => $validated['password'],
|
||||
]);
|
||||
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
Flux::toast(variant: 'success', text: __('Password updated.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable two-factor authentication for the user.
|
||||
*/
|
||||
public function enable(EnableTwoFactorAuthentication $enableTwoFactorAuthentication): void
|
||||
{
|
||||
$enableTwoFactorAuthentication(auth()->user());
|
||||
|
||||
if (! $this->requiresConfirmation) {
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
}
|
||||
|
||||
$this->loadSetupData();
|
||||
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the two-factor authentication setup data for the user.
|
||||
*/
|
||||
private function loadSetupData(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
try {
|
||||
$this->qrCodeSvg = $user?->twoFactorQrCodeSvg();
|
||||
$this->manualSetupKey = decrypt($user->two_factor_secret);
|
||||
} catch (Exception) {
|
||||
$this->addError('setupData', 'Failed to fetch setup data.');
|
||||
|
||||
$this->reset('qrCodeSvg', 'manualSetupKey');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the two-factor verification step if necessary.
|
||||
*/
|
||||
public function showVerificationIfNecessary(): void
|
||||
{
|
||||
if ($this->requiresConfirmation) {
|
||||
$this->showVerificationStep = true;
|
||||
|
||||
$this->resetErrorBag();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->closeModal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm two-factor authentication for the user.
|
||||
*/
|
||||
public function confirmTwoFactor(ConfirmTwoFactorAuthentication $confirmTwoFactorAuthentication): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$confirmTwoFactorAuthentication(auth()->user(), $this->code);
|
||||
|
||||
$this->closeModal();
|
||||
|
||||
$this->twoFactorEnabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset two-factor verification state.
|
||||
*/
|
||||
public function resetVerification(): void
|
||||
{
|
||||
$this->reset('code', 'showVerificationStep');
|
||||
|
||||
$this->resetErrorBag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable two-factor authentication for the user.
|
||||
*/
|
||||
public function disable(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void
|
||||
{
|
||||
$disableTwoFactorAuthentication(auth()->user());
|
||||
|
||||
$this->twoFactorEnabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the two-factor authentication modal.
|
||||
*/
|
||||
public function closeModal(): void
|
||||
{
|
||||
$this->reset(
|
||||
'code',
|
||||
'manualSetupKey',
|
||||
'qrCodeSvg',
|
||||
'showModal',
|
||||
'showVerificationStep',
|
||||
);
|
||||
|
||||
$this->resetErrorBag();
|
||||
|
||||
if (! $this->requiresConfirmation) {
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current modal configuration state.
|
||||
*/
|
||||
#[Computed]
|
||||
public function modalConfig(): array
|
||||
{
|
||||
if ($this->twoFactorEnabled) {
|
||||
return [
|
||||
'title' => __('Two-factor authentication enabled'),
|
||||
'description' => __('Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app.'),
|
||||
'buttonText' => __('Close'),
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->showVerificationStep) {
|
||||
return [
|
||||
'title' => __('Verify authentication code'),
|
||||
'description' => __('Enter the 6-digit code from your authenticator app.'),
|
||||
'buttonText' => __('Continue'),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => __('Enable two-factor authentication'),
|
||||
'description' => __('To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app.'),
|
||||
'buttonText' => __('Continue'),
|
||||
];
|
||||
}
|
||||
}
|
||||
50
app/Livewire/Settings/TwoFactor/RecoveryCodes.php
Normal file
50
app/Livewire/Settings/TwoFactor/RecoveryCodes.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings\TwoFactor;
|
||||
|
||||
use Exception;
|
||||
use Laravel\Fortify\Actions\GenerateNewRecoveryCodes;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class RecoveryCodes extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public array $recoveryCodes = [];
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->loadRecoveryCodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new recovery codes for the user.
|
||||
*/
|
||||
public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generateNewRecoveryCodes): void
|
||||
{
|
||||
$generateNewRecoveryCodes(auth()->user());
|
||||
|
||||
$this->loadRecoveryCodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the recovery codes for the user.
|
||||
*/
|
||||
private function loadRecoveryCodes(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->hasEnabledTwoFactorAuthentication() && $user->two_factor_recovery_codes) {
|
||||
try {
|
||||
$this->recoveryCodes = json_decode(decrypt($user->two_factor_recovery_codes), true);
|
||||
} catch (Exception) {
|
||||
$this->addError('recoveryCodes', 'Failed to load recovery codes');
|
||||
|
||||
$this->recoveryCodes = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
app/Models/Conversation.php
Normal file
83
app/Models/Conversation.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\ConversationFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['created_by_id', 'type', 'name', 'description'])]
|
||||
class Conversation extends Model
|
||||
{
|
||||
/** @use HasFactory<ConversationFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public const string TypeDirect = 'direct';
|
||||
|
||||
public const string TypeGroup = 'group';
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<ConversationParticipant, $this>
|
||||
*/
|
||||
public function participants(): HasMany
|
||||
{
|
||||
return $this->hasMany(ConversationParticipant::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<User, $this>
|
||||
*/
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'conversation_participants')
|
||||
->withPivot(['role', 'joined_at', 'last_read_at', 'muted_until'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Message, $this>
|
||||
*/
|
||||
public function messages(): HasMany
|
||||
{
|
||||
return $this->hasMany(Message::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasOne<Message, $this>
|
||||
*/
|
||||
public function latestMessage(): HasOne
|
||||
{
|
||||
return $this->hasOne(Message::class)->latestOfMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Conversation> $query
|
||||
* @return Builder<Conversation>
|
||||
*/
|
||||
public function scopeForUser(Builder $query, User|int $user): Builder
|
||||
{
|
||||
$userId = $user instanceof User ? $user->id : $user;
|
||||
|
||||
return $query->whereHas('participants', fn (Builder $participants) => $participants
|
||||
->where('user_id', $userId));
|
||||
}
|
||||
|
||||
public function isGroup(): bool
|
||||
{
|
||||
return $this->type === self::TypeGroup;
|
||||
}
|
||||
}
|
||||
48
app/Models/ConversationParticipant.php
Normal file
48
app/Models/ConversationParticipant.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\ConversationParticipantFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['conversation_id', 'user_id', 'role', 'joined_at', 'last_read_at', 'muted_until'])]
|
||||
class ConversationParticipant extends Model
|
||||
{
|
||||
/** @use HasFactory<ConversationParticipantFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public const string RoleAdmin = 'admin';
|
||||
|
||||
public const string RoleMember = 'member';
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'joined_at' => 'datetime',
|
||||
'last_read_at' => 'datetime',
|
||||
'muted_until' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Conversation, $this>
|
||||
*/
|
||||
public function conversation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Conversation::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
53
app/Models/Message.php
Normal file
53
app/Models/Message.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\MessageFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['conversation_id', 'user_id', 'type', 'body', 'metadata', 'edited_at'])]
|
||||
class Message extends Model
|
||||
{
|
||||
/** @use HasFactory<MessageFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public const string TypeText = 'text';
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'metadata' => 'array',
|
||||
'edited_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Conversation, $this>
|
||||
*/
|
||||
public function conversation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Conversation::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function sender(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
74
app/Models/User.php
Normal file
74
app/Models/User.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, TwoFactorAuthenticatable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<ConversationParticipant, $this>
|
||||
*/
|
||||
public function conversationParticipants(): HasMany
|
||||
{
|
||||
return $this->hasMany(ConversationParticipant::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Conversation, $this>
|
||||
*/
|
||||
public function conversations(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Conversation::class, 'conversation_participants')
|
||||
->withPivot(['role', 'joined_at', 'last_read_at', 'muted_until'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Message, $this>
|
||||
*/
|
||||
public function messages(): HasMany
|
||||
{
|
||||
return $this->hasMany(Message::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's initials
|
||||
*/
|
||||
public function initials(): string
|
||||
{
|
||||
return Str::of($this->name)
|
||||
->explode(' ')
|
||||
->take(2)
|
||||
->map(fn ($word) => Str::substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
50
app/Providers/AppServiceProvider.php
Normal file
50
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->configureDefaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure default behaviors for production-ready applications.
|
||||
*/
|
||||
protected function configureDefaults(): void
|
||||
{
|
||||
Date::use(CarbonImmutable::class);
|
||||
|
||||
DB::prohibitDestructiveCommands(
|
||||
app()->isProduction(),
|
||||
);
|
||||
|
||||
Password::defaults(fn (): ?Password => app()->isProduction()
|
||||
? Password::min(12)
|
||||
->mixedCase()
|
||||
->letters()
|
||||
->numbers()
|
||||
->symbols()
|
||||
->uncompromised()
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
72
app/Providers/FortifyServiceProvider.php
Normal file
72
app/Providers/FortifyServiceProvider.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Actions\Fortify\CreateNewUser;
|
||||
use App\Actions\Fortify\ResetUserPassword;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Fortify\Fortify;
|
||||
|
||||
class FortifyServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->configureActions();
|
||||
$this->configureViews();
|
||||
$this->configureRateLimiting();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure Fortify actions.
|
||||
*/
|
||||
private function configureActions(): void
|
||||
{
|
||||
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
|
||||
Fortify::createUsersUsing(CreateNewUser::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure Fortify views.
|
||||
*/
|
||||
private function configureViews(): void
|
||||
{
|
||||
Fortify::loginView(fn () => view('livewire.auth.login'));
|
||||
Fortify::verifyEmailView(fn () => view('livewire.auth.verify-email'));
|
||||
Fortify::twoFactorChallengeView(fn () => view('livewire.auth.two-factor-challenge'));
|
||||
Fortify::confirmPasswordView(fn () => view('livewire.auth.confirm-password'));
|
||||
Fortify::registerView(fn () => view('livewire.auth.register'));
|
||||
Fortify::resetPasswordView(fn () => view('livewire.auth.reset-password'));
|
||||
Fortify::requestPasswordResetLinkView(fn () => view('livewire.auth.forgot-password'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure rate limiting.
|
||||
*/
|
||||
private function configureRateLimiting(): void
|
||||
{
|
||||
RateLimiter::for('two-factor', function (Request $request) {
|
||||
return Limit::perMinute(5)->by($request->session()->get('login.id'));
|
||||
});
|
||||
|
||||
RateLimiter::for('login', function (Request $request) {
|
||||
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
|
||||
|
||||
return Limit::perMinute(5)->by($throttleKey);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user