validateGuestbookPost($user, $request); if ($validation !== null) { return $validation; } $user->profileGuestbook()->create([ 'user_id' => Auth::id(), 'message' => $request->input('message'), ]); return back()->with('success', __('Your message has been posted.')); } public function destroy(User $user, WebsiteUserGuestbook $guestbook): RedirectResponse { /** @var \App\Models\User|null $currentUser */ $currentUser = Auth::user(); if ($currentUser === null) { return back()->withErrors([ 'message' => __('You must be logged in.'), ]); } if ($guestbook->user_id !== $currentUser->id && $guestbook->profile_id !== $user->id && $currentUser->rank < (int) setting('min_staff_rank')) { return back()->withErrors([ 'message' => __('Do do not have permission to delete this message'), ]); } $guestbook->delete(); return back()->with('success', __('Your message has been deleted.')); } private function validateGuestbookPost(User $user, GuestbookFormRequest $request): ?RedirectResponse { /** @var \App\Models\User|null $currentUser */ $currentUser = $request->user(); if ($currentUser === null) { return $this->redirectWithError(__('You must be logged in.')); } if ($user->id === $currentUser->id) { return $this->redirectWithError(__('You cannot post a message on your own profile.')); } $maxAllowedPostCount = in_array((string) setting('max_guestbook_posts_per_profile'), ['', '0'], true) ? 3 : (int) setting('max_guestbook_posts_per_profile'); if ($user->profileGuestbook()->where('user_id', $currentUser->id)->count() >= $maxAllowedPostCount) { return $this->redirectWithError(__('You have already posted :count messages on this profile.', ['count' => $maxAllowedPostCount])); } return null; } private function redirectWithError(string $message): RedirectResponse { return back()->withErrors(['message' => $message]); } }