type === 'global') { return true; } if ($document->type === 'dedicated') { $userGroupIds = $user->groups()->pluck('groups.id')->toArray(); return in_array($document->group_id, $userGroupIds); } return false; } /** * 下载文档 */ public function downloadDocument(Document $document, User $user): StreamedResponse { if (!$this->validateDocumentAccess($document, $user)) { throw new \Exception('您没有权限访问此文档'); } if (!Storage::disk('local')->exists($document->file_path)) { throw new \Exception('文档不存在或已被删除'); } return Storage::disk('local')->download( $document->file_path, $document->file_name ); } /** * 记录文档下载日志 */ public function logDownload(Document $document, User $user, ?string $ipAddress = null): DownloadLog { return DownloadLog::create([ 'document_id' => $document->id, 'user_id' => $user->id, 'downloaded_at' => now(), 'ip_address' => $ipAddress ?? request()->ip(), ]); } }