Skip to content

Commit 3a7fb6b

Browse files
author
csavelief
committed
Automatically import basic frameworks on user creation
1 parent 70cbbe5 commit 3a7fb6b

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

app/Listeners/UserInvitationUtilizedListener.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace App\Listeners;
44

5+
use App\Models\Collection;
56
use App\Models\Prompt;
67
use App\Models\Role;
8+
use App\Models\YnhFramework;
9+
use App\Rules\IsValidCollectionName;
710
use App\User;
811
use Illuminate\Support\Facades\Auth;
912
use Illuminate\Support\Facades\File;
13+
use Illuminate\Support\Facades\Log;
1014
use Konekt\User\Events\UserInvitationUtilized;
1115

1216
class UserInvitationUtilizedListener extends AbstractListener
@@ -74,6 +78,23 @@ protected function handle2($event)
7478
$this->importPrompt($created, 'default_chat', 'seeds/prompts/default_chat.txt');
7579
$this->importPrompt($created, 'default_chat_history', 'seeds/prompts/default_chat_history.txt');
7680
$this->importPrompt($created, 'default_debugger', 'seeds/prompts/default_debugger.txt');
81+
82+
// Create shadow collections for some frameworks
83+
$frameworks = \App\Models\YnhFramework::all();
84+
85+
foreach ($frameworks as $framework) {
86+
if ($framework->file === 'seeds/frameworks/anssi/anssi-genai-security-recommendations-1.0.jsonl') {
87+
$this->importFramework($framework, 20);
88+
} else if ($framework->file === 'seeds/frameworks/anssi/anssi-guide-hygiene-detail.jsonl') {
89+
$this->importFramework($framework, 10);
90+
} else if ($framework->file === 'seeds/frameworks/gdpr/gdpr.jsonl') {
91+
$this->importFramework($framework, 30);
92+
} else if ($framework->file === 'seeds/frameworks/dora/dora.jsonl') {
93+
$this->importFramework($framework, 50);
94+
} else if ($framework->file === 'seeds/frameworks/nis2/nis2-directive.jsonl') {
95+
$this->importFramework($framework, 40);
96+
}
97+
}
7798
}
7899
}
79100
}
@@ -97,4 +118,31 @@ private function importPrompt(User $user, string $name, string $root)
97118
]);
98119
}
99120
}
121+
122+
private function importFramework(YnhFramework $framework, int $priority): void
123+
{
124+
$collection = $this->getOrCreateCollection($framework->collectionName(), $priority);
125+
if ($collection && $collection->files()->count() === 0) {
126+
$url = \App\Http\Controllers\CyberBuddyController::saveLocalFile($collection, $framework->path());
127+
}
128+
}
129+
130+
private function getOrCreateCollection(string $collectionName, int $priority): ?Collection
131+
{
132+
/** @var \App\Models\Collection $collection */
133+
$collection = Collection::where('name', $collectionName)
134+
->where('is_deleted', false)
135+
->first();
136+
if (!$collection) {
137+
if (!IsValidCollectionName::test($collectionName)) {
138+
Log::error("Invalid collection name : {$collectionName}");
139+
return null;
140+
}
141+
$collection = Collection::create([
142+
'name' => $collectionName,
143+
'priority' => max($priority, 0),
144+
]);
145+
}
146+
return $collection;
147+
}
100148
}

app/Listeners/UserWasCreatedListener.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace App\Listeners;
44

5+
use App\Models\Collection;
56
use App\Models\Prompt;
7+
use App\Models\YnhFramework;
8+
use App\Rules\IsValidCollectionName;
69
use App\User;
710
use Illuminate\Support\Facades\Auth;
811
use Illuminate\Support\Facades\File;
12+
use Illuminate\Support\Facades\Log;
913
use Konekt\User\Events\UserWasCreated;
1014

1115
class UserWasCreatedListener extends AbstractListener
@@ -25,6 +29,23 @@ protected function handle2($event)
2529
$this->importPrompt($user, 'default_chat', 'seeds/prompts/default_chat.txt');
2630
$this->importPrompt($user, 'default_chat_history', 'seeds/prompts/default_chat_history.txt');
2731
$this->importPrompt($user, 'default_debugger', 'seeds/prompts/default_debugger.txt');
32+
33+
// Create shadow collections for some frameworks
34+
$frameworks = \App\Models\YnhFramework::all();
35+
36+
foreach ($frameworks as $framework) {
37+
if ($framework->file === 'seeds/frameworks/anssi/anssi-genai-security-recommendations-1.0.jsonl') {
38+
$this->importFramework($framework, 20);
39+
} else if ($framework->file === 'seeds/frameworks/anssi/anssi-guide-hygiene-detail.jsonl') {
40+
$this->importFramework($framework, 10);
41+
} else if ($framework->file === 'seeds/frameworks/gdpr/gdpr.jsonl') {
42+
$this->importFramework($framework, 30);
43+
} else if ($framework->file === 'seeds/frameworks/dora/dora.jsonl') {
44+
$this->importFramework($framework, 50);
45+
} else if ($framework->file === 'seeds/frameworks/nis2/nis2-directive.jsonl') {
46+
$this->importFramework($framework, 40);
47+
}
48+
}
2849
}
2950

3051
private function importPrompt(User $user, string $name, string $root)
@@ -50,4 +71,31 @@ private function importPrompt(User $user, string $name, string $root)
5071
]);
5172
}
5273
}
74+
75+
private function importFramework(YnhFramework $framework, int $priority): void
76+
{
77+
$collection = $this->getOrCreateCollection($framework->collectionName(), $priority);
78+
if ($collection && $collection->files()->count() === 0) {
79+
$url = \App\Http\Controllers\CyberBuddyController::saveLocalFile($collection, $framework->path());
80+
}
81+
}
82+
83+
private function getOrCreateCollection(string $collectionName, int $priority): ?Collection
84+
{
85+
/** @var \App\Models\Collection $collection */
86+
$collection = Collection::where('name', $collectionName)
87+
->where('is_deleted', false)
88+
->first();
89+
if (!$collection) {
90+
if (!IsValidCollectionName::test($collectionName)) {
91+
Log::error("Invalid collection name : {$collectionName}");
92+
return null;
93+
}
94+
$collection = Collection::create([
95+
'name' => $collectionName,
96+
'priority' => max($priority, 0),
97+
]);
98+
}
99+
return $collection;
100+
}
53101
}

0 commit comments

Comments
 (0)