|
2 | 2 |
|
3 | 3 | namespace App\Models; |
4 | 4 |
|
| 5 | +use App\Helpers\ApiUtilsFacade as ApiUtils2; |
5 | 6 | use App\User; |
6 | 7 | use Carbon\Carbon; |
7 | 8 | use Illuminate\Database\Eloquent\Model; |
@@ -42,9 +43,113 @@ class TimelineItem extends Model |
42 | 43 | 'updated_at' => 'datetime', |
43 | 44 | ]; |
44 | 45 |
|
45 | | - public static function createNote(int $ownedBy, string $body, string $subject = ''): TimelineItem |
| 46 | + public static function createAlert(User $user, Scan $scan, Alert $alert): TimelineItem |
46 | 47 | { |
47 | | - return self::createItem($ownedBy, 'note', Carbon::now(), 0, [ |
| 48 | + $asset = $alert->asset(); |
| 49 | + $port = $alert->port(); |
| 50 | + |
| 51 | + if (empty($alert->title)) { |
| 52 | + $title = ''; |
| 53 | + } else { |
| 54 | + $result = ApiUtils2::translate($alert->title, 'fr'); |
| 55 | + if ($result['error'] !== false) { |
| 56 | + $title = $alert->title; |
| 57 | + } else { |
| 58 | + $title = $result['response']; |
| 59 | + } |
| 60 | + } |
| 61 | + if (empty($alert->vulnerability)) { |
| 62 | + $vulnerability = ''; |
| 63 | + } else { |
| 64 | + $result = ApiUtils2::translate($alert->vulnerability, 'fr'); |
| 65 | + if ($result['error'] !== false) { |
| 66 | + $vulnerability = $alert->vulnerability; |
| 67 | + } else { |
| 68 | + $vulnerability = $result['response']; |
| 69 | + } |
| 70 | + } |
| 71 | + if (empty($alert->remediation)) { |
| 72 | + $remediation = ''; |
| 73 | + } else { |
| 74 | + $result = ApiUtils2::translate($alert->remediation, 'fr'); |
| 75 | + if ($result['error'] !== false) { |
| 76 | + $remediation = $alert->remediation; |
| 77 | + } else { |
| 78 | + $remediation = $result['response']; |
| 79 | + } |
| 80 | + } |
| 81 | + return self::createItem($user->id, 'alert', Carbon::now(), 0, [ |
| 82 | + |
| 83 | + // Ids |
| 84 | + 'tenant_id' => $user->tenant_id, |
| 85 | + 'asset_id' => $asset->id, |
| 86 | + 'scan_id' => $scan->id, |
| 87 | + 'port_id' => $port->id, |
| 88 | + 'alert_id' => $alert->id, |
| 89 | + |
| 90 | + // Asset |
| 91 | + 'asset_name' => $asset->asset, |
| 92 | + 'asset_type' => $asset->type->value, |
| 93 | + 'asset_tld' => $asset->tld() ?? '', |
| 94 | + 'asset_tags' => json_encode($asset->tags()->get()->pluck('tag')->unique()->sort()->values()->toArray()), |
| 95 | + 'asset_ip' => $port->ip, |
| 96 | + |
| 97 | + // Port |
| 98 | + 'port_number' => $port->port, |
| 99 | + 'port_protocol' => $port->protocol, |
| 100 | + 'port_tags' => json_encode($port->tags()->get()->pluck('tag')->unique()->sort()->values()->toArray()), |
| 101 | + 'port_service' => $port->service ?? '', |
| 102 | + 'port_product' => $port->product ?? '', |
| 103 | + |
| 104 | + // Hosting provider |
| 105 | + 'hosting_service_description' => $port->hosting_service_description ?? '', |
| 106 | + 'hosting_service_registry' => $port->hosting_service_registry ?? '', |
| 107 | + 'hosting_service_asn' => $port->hosting_service_asn ?? '', |
| 108 | + 'hosting_service_cidr' => $port->hosting_service_cidr ?? '', |
| 109 | + 'hosting_service_country_code' => $port->hosting_service_country_code ?? '', |
| 110 | + 'hosting_service_date' => $port->hosting_service_date ?? '', |
| 111 | + |
| 112 | + // Vulnerability |
| 113 | + 'vuln_type' => $alert->type, |
| 114 | + 'vuln_vulnerability_en' => $alert->vulnerability ?? '', |
| 115 | + 'vuln_vulnerability_fr' => $vulnerability, |
| 116 | + 'vuln_remediation_en' => $alert->remediation ?? '', |
| 117 | + 'vuln_remediation_fr' => $remediation, |
| 118 | + 'vuln_level' => $alert->level ?? '', |
| 119 | + 'vuln_uid' => $alert->uid ?? '', |
| 120 | + 'vuln_cve_id' => $alert->cve_id ?? '', |
| 121 | + 'vuln_cve_cvss' => $alert->cve_cvss ?? '', |
| 122 | + 'vuln_cve_vendor' => $alert->cve_vendor ?? '', |
| 123 | + 'vuln_cve_product' => $alert->cve_product ?? '', |
| 124 | + 'vuln_title_en' => $alert->title ?? '', |
| 125 | + 'vuln_title_fr' => $title, |
| 126 | + |
| 127 | + // Misc. |
| 128 | + 'country' => $port->country ?? '', |
| 129 | + 'ssl' => $port->ssl ?? false, |
| 130 | + ]); |
| 131 | + } |
| 132 | + |
| 133 | + public static function fetchAlerts(?int $ownedBy = null, ?Carbon $createdAtOrAfter = null, ?Carbon $createdAtOrBefore = null, ?int $flags = null, array $ands = []): \Illuminate\Support\Collection |
| 134 | + { |
| 135 | + return self::fetchItems($ownedBy, 'alert', $createdAtOrAfter, $createdAtOrBefore, $flags, $ands); |
| 136 | + } |
| 137 | + |
| 138 | + public static function deleteAlerts(int $ownedBy, string $asset): void |
| 139 | + { |
| 140 | + TimelineItem::fetchAlerts($ownedBy, null, null, 0, [ |
| 141 | + [['asset_name', '=', $asset]], |
| 142 | + ])->each(function (TimelineItem $item) { |
| 143 | + DB::transaction(function () use ($item) { |
| 144 | + $item->facts()->delete(); |
| 145 | + $item->delete(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + public static function createNote(User $user, string $body, string $subject = ''): TimelineItem |
| 151 | + { |
| 152 | + return self::createItem($user->id, 'note', Carbon::now(), 0, [ |
48 | 153 | 'body' => Str::limit(trim($body), 1000 - 3, '...'), |
49 | 154 | 'subject' => Str::limit(trim($subject), 1000 - 3, '...'), |
50 | 155 | ]); |
|
0 commit comments