| 1 | package com.edtech.service; | |
| 2 | ||
| 3 | import com.edtech.model.AcaoAuditoria; | |
| 4 | import com.edtech.model.AuditLog; | |
| 5 | import com.edtech.repository.AuditLogRepository; | |
| 6 | import jakarta.servlet.http.HttpServletRequest; | |
| 7 | import java.util.UUID; | |
| 8 | import org.slf4j.Logger; | |
| 9 | import org.slf4j.LoggerFactory; | |
| 10 | import org.springframework.stereotype.Service; | |
| 11 | import org.springframework.web.context.request.RequestContextHolder; | |
| 12 | import org.springframework.web.context.request.ServletRequestAttributes; | |
| 13 | ||
| 14 | /** Servico responsavel por registrar logs de auditoria no sistema. */ | |
| 15 | @Service | |
| 16 | public class AuditLogService { | |
| 17 | private static final String DOCUMENT_RESOURCE_TYPE = "Document"; | |
| 18 | private static final Logger log = LoggerFactory.getLogger(AuditLogService.class); | |
| 19 | private final AuditLogRepository auditLogRepository; | |
| 20 | ||
| 21 | /** | |
| 22 | * Construtor para injecao de dependencias. | |
| 23 | * | |
| 24 | * @param auditLogRepository Repositorio de logs de auditoria. | |
| 25 | */ | |
| 26 | public AuditLogService(AuditLogRepository auditLogRepository) { | |
| 27 | this.auditLogRepository = auditLogRepository; | |
| 28 | } | |
| 29 | ||
| 30 | private String getClientIp() { | |
| 31 | try { | |
| 32 | ServletRequestAttributes attributes = | |
| 33 | (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |
| 34 |
1
1. getClientIp : negated conditional → KILLED |
if (attributes != null) { |
| 35 | HttpServletRequest request = attributes.getRequest(); | |
| 36 | String forwardedFor = request.getHeader("X-Forwarded-For"); | |
| 37 |
2
1. getClientIp : negated conditional → KILLED 2. getClientIp : negated conditional → KILLED |
if (forwardedFor != null && !forwardedFor.isEmpty()) { |
| 38 |
1
1. getClientIp : replaced return value with "" for com/edtech/service/AuditLogService::getClientIp → KILLED |
return forwardedFor.split(",")[0].trim(); |
| 39 | } | |
| 40 |
1
1. getClientIp : replaced return value with "" for com/edtech/service/AuditLogService::getClientIp → KILLED |
return request.getRemoteAddr(); |
| 41 | } | |
| 42 | } catch (Exception ignored) { | |
| 43 | log.debug("Erro ao extrair o IP do cliente", ignored); | |
| 44 | } | |
| 45 |
1
1. getClientIp : replaced return value with "" for com/edtech/service/AuditLogService::getClientIp → KILLED |
return "UNKNOWN"; |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Registra uma acao de auditoria detalhada. | |
| 50 | * | |
| 51 | * @param action Acao realizada. | |
| 52 | * @param userId ID do usuario. | |
| 53 | * @param resourceType Tipo do recurso. | |
| 54 | * @param resourceId ID do recurso. | |
| 55 | * @param ip IP do cliente. | |
| 56 | * @param details Detalhes adicionais. | |
| 57 | * @return Log de auditoria salvo. | |
| 58 | */ | |
| 59 | public AuditLog log( | |
| 60 | AcaoAuditoria action, | |
| 61 | UUID userId, | |
| 62 | String resourceType, | |
| 63 | UUID resourceId, | |
| 64 | String ip, | |
| 65 | String details) { | |
| 66 | try { | |
| 67 |
1
1. log : negated conditional → KILLED |
String clientIp = ip != null ? ip : getClientIp(); |
| 68 | AuditLog auditLog = | |
| 69 | new AuditLog( | |
| 70 | java.util.UUID.fromString("00000000-0000-0000-0000-000000000001"), | |
| 71 | userId, | |
| 72 | action, | |
| 73 | resourceType, | |
| 74 | resourceId, | |
| 75 | clientIp, | |
| 76 | details); | |
| 77 |
1
1. log : replaced return value with null for com/edtech/service/AuditLogService::log → KILLED |
return auditLogRepository.save(auditLog); |
| 78 | } catch (Exception e) { | |
| 79 | log.error("Erro em salvar o log da auditoria: {}", e.getMessage(), e); | |
| 80 | return null; | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Registra uma acao de auditoria simples, sem vinculo direto com um recurso especifico. | |
| 86 | * | |
| 87 | * @param userId ID do usuario. | |
| 88 | * @param action Acao realizada. | |
| 89 | * @param details Detalhes adicionais. | |
| 90 | * @return Log de auditoria salvo. | |
| 91 | */ | |
| 92 | public AuditLog logAction(UUID userId, AcaoAuditoria action, String details) { | |
| 93 |
1
1. logAction : replaced return value with null for com/edtech/service/AuditLogService::logAction → KILLED |
return log(action, userId, null, null, null, details); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Registra uma acao de auditoria relacionada a um documento especifico. | |
| 98 | * | |
| 99 | * @param userId ID do usuario que executou a acao. | |
| 100 | * @param action Acao realizada. | |
| 101 | * @param documentId ID do documento afetado. | |
| 102 | * @param details Detalhes adicionais da acao. | |
| 103 | * @return Log de auditoria salvo. | |
| 104 | */ | |
| 105 | public AuditLog logDocumentAction( | |
| 106 | UUID userId, AcaoAuditoria action, UUID documentId, String details) { | |
| 107 |
1
1. logDocumentAction : replaced return value with null for com/edtech/service/AuditLogService::logDocumentAction → NO_COVERAGE |
return log(action, userId, DOCUMENT_RESOURCE_TYPE, documentId, null, details); |
| 108 | } | |
| 109 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 37 |
1.1 2.2 |
|
| 38 |
1.1 |
|
| 40 |
1.1 |
|
| 45 |
1.1 |
|
| 67 |
1.1 |
|
| 77 |
1.1 |
|
| 93 |
1.1 |
|
| 107 |
1.1 |