From bf5f8d239292bb8fc5494a556d437fafdbbd8e03 Mon Sep 17 00:00:00 2001 From: Artem Otliaguzov Date: Wed, 12 Jun 2024 14:53:48 +0200 Subject: [PATCH] add comment and make entity store with new xml_document --- app/Console/Commands/DumpFromGit.php | 23 +++++--- app/Http/Controllers/EntityController.php | 6 +++ app/Http/Controllers/FederationController.php | 1 - app/Jobs/SaveMetadataToFolders.php | 42 --------------- app/Services/EntityService.php | 20 +++++-- .../DumpFromGit/EntitiesHelp/UpdateEntity.php | 52 +++++++++++-------- app/Traits/EntityFolderTrait.php | 26 ---------- 7 files changed, 67 insertions(+), 103 deletions(-) delete mode 100644 app/Jobs/SaveMetadataToFolders.php delete mode 100644 app/Traits/EntityFolderTrait.php diff --git a/app/Console/Commands/DumpFromGit.php b/app/Console/Commands/DumpFromGit.php index a52b5cb..4113418 100644 --- a/app/Console/Commands/DumpFromGit.php +++ b/app/Console/Commands/DumpFromGit.php @@ -4,6 +4,7 @@ use App\Facades\EntityFacade; +use App\Models\Membership; use App\Models\User; use App\Traits\DumpFromGit\CreateCategoriesAndGroupsTrait; use App\Traits\DumpFromGit\CreateEntitiesTrait; @@ -11,17 +12,16 @@ use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait; use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; use App\Traits\EdugainTrait; -use App\Traits\EntityFolderTrait; use App\Traits\FederationTrait; use App\Traits\GitTrait; +use Exception; use Illuminate\Console\Command; use App\Traits\ValidatorTrait; -use Illuminate\Support\Facades\Artisan; class DumpFromGit extends Command { - use GitTrait, ValidatorTrait,EntityFolderTrait; + use GitTrait, ValidatorTrait; use CreateFederationTrait,CreateEntitiesTrait,CreateCategoriesAndGroupsTrait; use UpdateEntity,FederationTrait,FixEntityTrait; use EdugainTrait; @@ -38,18 +38,27 @@ class DumpFromGit extends Command * * @var string */ - protected $description = 'Command description'; + protected $description = 'Dump all old information from git'; + + private function createMetadataFiles(): void + { + $this->updateFederationFolders(); + $membership = Membership::select('entity_id','federation_id')->whereApproved(1)->get(); + foreach ($membership as $member) { + EntityFacade::saveMetadataToFederationFolder($member->entity_id, $member->federation_id); + } + } /** * Execute the console command. - * @throws \Exception no amin + * @throws Exception no amin */ public function handle() { $firstAdminId = User::where('admin', 1)->first()->id; if(empty($firstAdminId)) - throw new \Exception('firstAdminId is null'); + throw new Exception('firstAdminId is null'); $this->initializeGit(); @@ -60,7 +69,7 @@ public function handle() $this->updateEntitiesXml(); $this->updateFederationFolders(); $this->fixEntities(); - $this->createAllMetadataFiles(); + $this->createMetadataFiles(); $this->makeEdu2Edugain(); diff --git a/app/Http/Controllers/EntityController.php b/app/Http/Controllers/EntityController.php index 84e1f90..1a08c1d 100644 --- a/app/Http/Controllers/EntityController.php +++ b/app/Http/Controllers/EntityController.php @@ -37,6 +37,8 @@ use App\Notifications\FederationMemberChanged; use App\Notifications\IdpCategoryChanged; use App\Notifications\YourEntityRightsChanged; +use App\Traits\DumpFromGit\EntitiesHelp\DeleteFromEntity; +use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; use App\Traits\GitTrait; use App\Traits\ValidatorTrait; use Illuminate\Http\Request; @@ -50,6 +52,7 @@ class EntityController extends Controller { use ValidatorTrait, GitTrait; + use DeleteFromEntity,UpdateEntity; public function __construct() { @@ -127,6 +130,8 @@ public function store(StoreEntity $request) if ($new_entity['type'] === 'idp') { $new_entity = array_merge($new_entity, ['hfd' => true]); } + $new_entity= array_merge($new_entity, ['xml_file' => $this->deleteTags($new_entity['metadata']) ]); + $entity = Entity::create($new_entity); $entity->operators()->attach(Auth::id()); $entity->federations()->attach($federation, [ @@ -134,6 +139,7 @@ public function store(StoreEntity $request) 'requested_by' => Auth::id(), ]); + $this->updateEntityXml(Entity::where('id', $entity['id'])->first()); return $entity; }); diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index 6468319..38e3221 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -209,7 +209,6 @@ public function update(UpdateFederation $request, Federation $federation) $state = $federation->trashed() ? 'deleted' : 'restored'; $color = $federation->trashed() ? 'red' : 'green'; - //TODO ask about what we want to do with cfg and tag files /* if ($federation->trashed()) { GitDeleteFederation::dispatch($federation, Auth::user()); diff --git a/app/Jobs/SaveMetadataToFolders.php b/app/Jobs/SaveMetadataToFolders.php deleted file mode 100644 index f61599c..0000000 --- a/app/Jobs/SaveMetadataToFolders.php +++ /dev/null @@ -1,42 +0,0 @@ -entity_id = $entity_id; - $this->federation_id = $federation_id; - } - - /** - * Execute the job. - */ - public function handle(): void - { - EntityFacade::SaveEntityMetadataToFile($this->entity_id,$this->federation_id); - - } -} diff --git a/app/Services/EntityService.php b/app/Services/EntityService.php index be6c082..a58ed2e 100644 --- a/app/Services/EntityService.php +++ b/app/Services/EntityService.php @@ -2,26 +2,36 @@ namespace App\Services; use App\Models\Entity; use App\Models\Federation; +use Exception; use Illuminate\Support\Facades\Storage; class EntityService { - public function saveMetadataToFederationFolder($entity_id,$federation_id) + + /** + * Save to federation Folder using federation-id + * @throws Exception if federation doesnt exist in database + */ + + public function saveMetadataToFederationFolder($entity_id,$federation_id): void { $federation = Federation::find($federation_id); if(!$federation){ - throw new \Exception("Federation $federation_id not found"); + throw new Exception("Federation $federation_id not found"); } $this->saveEntityMetadataToFolder($entity_id,$federation->name); } - - public function saveEntityMetadataToFolder($entity_id,$folderName) + /** + * save entity if we know folder name + * @throws Exception if entity doesnt exist in database + */ + public function saveEntityMetadataToFolder($entity_id,$folderName): void { $entity = Entity::find($entity_id); if(!$entity){ - throw new \Exception("Entity not found with id $entity_id"); + throw new Exception("Entity not found with id $entity_id"); } $fileName = $entity->file; if(!Storage::disk('metadata')->exists($folderName)) diff --git a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php index 8314d9c..76e0aa1 100644 --- a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php +++ b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php @@ -180,8 +180,37 @@ private function updateRegistrationInfo(string $xml_document, string $entityId,a return $dom->saveXML(); } + /** + * @param array $timestampDocumentArray for add registration time from git file + * @return void update entity in db and return + */ + public function updateEntityXml($entity,array $timestampDocumentArray = []): void + { + if(empty($entity->xml_file)) + return; + + $xml_document = $entity->xml_file; + $isIdp = false; + if($entity->type == "idp") + $isIdp = true; + if($entity->rs) + { + $xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp); + } + if(!empty($entity->category_id)) + { + $xml_document = $this->updateXmlCategories($xml_document,$entity->category_id); + } + + $xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray); + + + Entity::whereId($entity->id)->update(['xml_file' => $xml_document]); + + } + public function updateEntitiesXml() : void { @@ -196,28 +225,7 @@ public function updateEntitiesXml() : void foreach (Entity::select()->get() as $entity) { - if(empty($entity->xml_file)) - continue; - - $xml_document = $entity->xml_file; - $isIdp = false; - if($entity->type == "idp") - $isIdp = true; - - - if($entity->rs) - { - $xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp); - } - if(!empty($entity->category_id)) - { - $xml_document = $this->updateXmlCategories($xml_document,$entity->category_id); - } - - $xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray); - - - Entity::whereId($entity->id)->update(['xml_file' => $xml_document]); + $this->updateEntityXml($entity,$timestampDocumentArray); } } diff --git a/app/Traits/EntityFolderTrait.php b/app/Traits/EntityFolderTrait.php deleted file mode 100644 index ebf8453..0000000 --- a/app/Traits/EntityFolderTrait.php +++ /dev/null @@ -1,26 +0,0 @@ -updateFederationFolders(); - $membership = Membership::select('entity_id','federation_id')->whereApproved(1)->get(); - foreach ($membership as $member) { - EntityFacade::saveMetadataToFederationFolder($member->entity_id, $member->federation_id); - } - - - } - - - -}