/ app / Models / TOE.php
TOE.php
  1  <?php
  2  
  3  namespace App\Models;
  4  
  5  use Database\Factories\TOEFactory;
  6  use Illuminate\Database\Eloquent\Factories\HasFactory;
  7  use Illuminate\Database\Eloquent\Model;
  8  use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  9  use Illuminate\Database\Eloquent\SoftDeletes;
 10  use Illuminate\Support\Collection;
 11  use Spatie\Activitylog\LogOptions;
 12  use Spatie\Activitylog\Traits\LogsActivity;
 13  use App\Models\Role;
 14  use Illuminate\Support\Facades\Storage;
 15  use App\Models\User;
 16  
 17  class TOE extends Model
 18  {
 19      use HasFactory, LogsActivity, SoftDeletes;
 20  
 21      protected $with = ['company', 'country'];
 22  
 23      protected $table = 'toes';
 24  
 25      protected $fillable = [
 26          'description',
 27          'summary',
 28          'name',
 29          'company_id',
 30          'version',
 31          'alias',
 32          'country_id',
 33          'is_active',
 34          'lab_technical_presentation',
 35          'physical_architecture',
 36          'logical_architecture',
 37          'cyber_security_info_url',
 38          'guidance_url',
 39          'period_security_support',
 40          'provider_contact_vulnerability_url',
 41          'vulnerability_notifications',
 42          'publicly_disclosed_url',
 43          'evaluation_target',
 44          'evaluation_scope',
 45          'commercial_name',
 46          'sectorial_dimensions',
 47          'usage_dimension',
 48          'technological_dimensions',
 49          'spdx_path',
 50          'oracle_prodcode',
 51          'is_certified',
 52          'development_address',
 53          'product_system',
 54          'created_by',
 55      ];
 56  
 57      protected $casts = [
 58          'sectorial_dimensions' => 'array',
 59          'technological_dimensions' => 'array',
 60          'usage_dimension' => 'array',
 61      ];
 62  
 63      protected static function newFactory(): TOEFactory
 64      {
 65          return TOEFactory::new();
 66      }
 67  
 68      public function dossiers()
 69      {
 70          return $this->hasMany(Dossier::class, 'toe_id');
 71      }
 72  
 73      public function dossier()
 74      {
 75          return $this->hasOne(Dossier::class, 'toe_id')->ofMany();
 76      }
 77  
 78      public function company()
 79      {
 80          return $this->belongsTo(Entity::class);
 81      }
 82  
 83      public function country()
 84      {
 85          return $this->belongsTo(Country::class);
 86      }
 87  
 88      public function st()
 89      {
 90          return $this->belongsTo(Document::class, 'st_document_id');
 91      }
 92  
 93      public function stLite() //30% menos de grasa
 94      {
 95          return $this->belongsTo(Document::class, 'st_lite_document_id');
 96      }
 97  
 98      public function etr()
 99      {
100          return $this->belongsTo(Document::class, 'etr_document_id');
101      }
102  
103      public function types()
104      {
105          return $this->belongsToMany(TOEType::class, 'toe_has_types');
106      }
107  
108      // CertificationRequirements
109      public function requirements()
110      {
111          return $this->hasMany(TOERequirement::class, 'toe_id');
112      }
113  
114      public function useEnvelopeHipoteses()
115      {
116          return $this->hasMany(UseEnvelopeHipotesis::class, 'toe_id');
117      }
118  
119      public function securityPolicies()
120      {
121          return $this->hasMany(SecurityPolicy::class, 'toe_id');
122      }
123  
124      public function functionalityEnvelopes()
125      {
126          return $this->hasMany(FunctionalityEnvelope::class, 'toe_id');
127      }
128  
129      public function noCoverDangers()
130      {
131          return $this->hasMany(NoCoverDanger::class, 'toe_id');
132      }
133  
134      // Log
135  
136      public function getActivitylogOptions(): LogOptions
137      {
138          return LogOptions::defaults()
139              ->logOnly([
140                  'description',
141                  'summary',
142                  'lab_technical_presentation',
143                  'physical_architecture',
144                  'logical_architecture',
145                  'name',
146                  'company_id',
147                  'company.name',
148                  'version',
149                  'alias',
150                  'country_id',
151                  'country.name',
152                  'created_by',
153              ])->logOnlyDirty()
154              ->dontSubmitEmptyLogs();
155      }
156  
157      public function vulnerabilities(): BelongsToMany
158      {
159          return $this->belongsToMany(Vulnerability::class, 'toe_vulnerability', 'toe_id');
160      }
161  
162      public function getNotifiableUsers(): Collection
163      {
164          $cto = Role::where('name', 'technical_manager')->first()->users->first();
165          if (!$cto) { // fallback
166              $cto = Role::where('name', 'root')->first()->users->first();
167          }
168  
169          if ($this->dossiers->isEmpty()) {
170              return collect([$cto]);
171          }
172  
173          return $this->dossiers->map(function ($dossier) use ($cto) {
174              if ($dossier->principalCertifier) {
175                  return $dossier->principalCertifier;
176              }
177  
178              return $cto;
179          })->unique();
180      }
181  
182      public function getDataForVulnerabilitiesReport()
183      {
184          return TOEVulnerability
185              ::where('toe_id', $this->id)
186              ->join('vulnerabilities', 'vulnerability_id', '=', 'vulnerabilities.id')
187              ->select([
188                  'applies',
189                  'rationale',
190                  'reviewed_by',
191                  'vid',
192                  'severity',
193                  'description',
194                  'state',
195                  'artifact_name',
196                  'artifact_type',
197                  'artifact_version',
198                  'source'
199              ])
200              ->get()
201              ->toArray();
202      }
203  
204      public function creator()
205      {
206          return $this->belongsTo(User::class, 'created_by');
207      }
208  }