middleware('auth:sanctum'); } /** * 사용자 소유권을 확인합니다. * * @param int $userId 확인할 사용자 ID * @return bool */ protected function isOwner(int $userId): bool { return $this->getCurrentUser()?->id === $userId; } /** * 사용자가 특정 리소스에 접근할 수 있는지 확인합니다. * * @param mixed $resource 접근하려는 리소스 * @return bool */ protected function canAccessResource($resource): bool { // 기본적으로 자신의 리소스만 접근 가능 if (method_exists($resource, 'user_id')) { return $this->getCurrentUser()?->id === $resource->user_id; } return true; } /** * 사용자 활동을 기록합니다. * * @param string $action 수행한 작업 * @param array $data 관련 데이터 * @return void */ protected function logUserActivity(string $action, array $data = []): void { // TODO: 사용자 활동 로그 시스템 구현 Log::info("User Activity: {$action}", [ 'user_id' => $this->getCurrentUser()?->uuid, 'data' => $data, 'timestamp' => now(), ]); } /** * 리소스 소유권을 확인하고, 소유자가 아니면 Forbidden 응답을 반환합니다. * * @param mixed $resource 확인할 리소스 * @param string $messageKey 오류 메시지 키 * @return JsonResponse|null 소유자이면 null, 아니면 Forbidden 응답 */ protected function checkOwnership($resource, string $messageKey = 'common.forbidden') { if (! $this->canAccessResource($resource)) { return $this->forbidden($messageKey); } return null; } /** * API 사용량을 기록합니다. * * @param string $endpoint 엔드포인트 * @param array $data 관련 데이터 * @return void */ protected function logApiUsage(string $endpoint, array $data = []): void { // TODO: API 사용량 통계 시스템 구현 // // PublicBaseController 와 같은 이유로 디버그 모드에서만 기록한다. // 통계 시스템이 서기 전까지 요청마다 로그 파일에 줄을 쌓지 않는다. if (! config('app.debug')) { return; } Log::debug("Auth API Usage: {$endpoint}", [ 'user_id' => $this->getCurrentUser()?->uuid, 'ip' => request()->ip(), 'user_agent' => request()->userAgent(), 'data' => $data, 'timestamp' => now(), ]); } }