22
33namespace App \Listeners ;
44
5+ use App \Models \Collection ;
56use App \Models \Prompt ;
7+ use App \Models \YnhFramework ;
8+ use App \Rules \IsValidCollectionName ;
69use App \User ;
710use Illuminate \Support \Facades \Auth ;
811use Illuminate \Support \Facades \File ;
12+ use Illuminate \Support \Facades \Log ;
913use Konekt \User \Events \UserWasCreated ;
1014
1115class 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